How to generate GUID for the unique identifier of an online game server based on the snowflake algorithm

Source: Internet
Author: User
Tags filetime unique id uuid

In a distributed system, there are still many scenarios where the global UID needs to be generated. twitter's snowflake solves this requirement and the implementation is still very simple. Besides the configuration information, the core code is 41 bits in milliseconds, 10 bits in machine IDs, and 12 bits in milliseconds.

There are also a lot of plug-in modules written in PHP on the internet. The core uses network communication to send the generated ID to PHP, without in-depth study of PHP module writing.

If you don't talk much about it, you should just go directly to the code.

Uuid. h

# Ifndef _ UTIL_UUID_H __
# Define _ UTIL_UUID_H __

# Include <stdint. h>

Namespace utils
{
// Twitter snowflake algorithm
// 64 63----------------22---------12-----------0
// Symbol bit | 41-bit time | 10-bit machine code | 12-bit auto-increment code |
Extern uint64_t get_time ();

Class unique_id_t
{
Public:
Unique_id_t ();
~ Unique_id_t ();

Void set_epoch (uint64_t epoch );
Void set_machine (int32_t machine );
Int64_t generate ();

Private:
Uint64_t epoch _;
Uint64_t time _;
Int32_t machine _;
Int32_t sequence _;
};

}

# Endif //! _ UTIL_UUID_H __

Uuid. cpp

# Include "uuid. h"
# If defined (_ GUNC __)
# Include <sys/time. h>
# Include <unistd. h>
# Define EPOCHFILETIME 11644473600000000ULL
# Else
# Include <windows. h>
# Include <time. h>
# Define EPOCHFILETIME 11644473600000000Ui64
# Endif

Namespace utils
{
Uint64_t get_time ()
    {
# Ifdef _ GUNC __
Struct timeval TV;
Gettimeofday (& TV, NULL );
Uint64 time = TV. TV _usec;
Time/= 1000;
Time + = (TV. TV _sec * 1000 );
Return time;
# Else
FILETIME filetime;
Uint64_t time = 0;
GetSystemTimeAsFileTime (& filetime );

Time | = filetime. dwHighDateTime;
Time <= 32;
Time | = filetime. dwLowDateTime;

Time/= 10;
Time-= EPOCHFILETIME;
Returns time/1000;
# Endif
    }

Unique_id_t: unique_id_t ()
    {
Epoch _ = 0;
Time _ = 0;
Machine _ = 0;
Sequence _ = 0;
    }

Unique_id_t ::~ Unique_id_t ()
    {

    }

Void unique_id_t: set_epoch (uint64_t epoch)
    {
Epoch _ = epoch;
    }

Void unique_id_t: set_machine (int32_t machine)
    {
Machine _ = machine;
    }

Int64_t unique_id_t: generate ()
    {
Int64_t value = 0;
Uint64_t time = get_time ()-epoch _;

// The last 41 bits are retained.
Value = time <22;

// The 10 digits in the middle are the machine IDs.
Value | = (machine _ & 0x3FF) <12;

// The last 12 digits are sequenceID
Value | = sequence _ ++ & 0 xFFF;
If (sequence _ = 0x1000)
        {
Sequence _ = 0;
        }

Return value;
    }
}

# Ifdef _ TEST __
# Include <iostream>
Void test ()
{
Utils: unique_id_t * u_id_ptr = new utils: unique_id_t ();
U_id_ptr-> set_epoch (uint64_t (1367505795100 ));
U_id_ptr-> set_machine (int32_t (100 ));
For (int I = 0; I <1024; ++ I)
    {
Std: cout <u_id_ptr-> generate () <std: endl ;;
    }
}
# Endif

This unique ID can be used to indicate the data used in your system, such as the unique ID of an item, the unique ID of a mount, and so on, so that you can easily record and track the data.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.