HeaderSize Size of the packet header
const HeaderSize int = 10
StatusBad response status
const StatusBad uint8 = 0x01
StatusOk response status
const StatusOk uint8 = 0x00
StatusPush response status of push
const StatusPush uint8 = 0xff
UDPProtoHeaderSize Size of the UDP packet protocol header
const UDPProtoHeaderSize int = 4
P2PHeader is the header for P2P messages. This is not expected to be received by the server
var P2PHeader = [4]byte{0xd, 0xe, 0xa, 0xf}
P2PHolePunchHeader deprecated
var P2PHolePunchHeader = [4]byte{0xc, 0xa, 0xf, 0xe}
P2PInternalHeader is the header for system messages mainly for hole punching
var P2PInternalHeader = [4]byte{0xb, 0xe, 0xe, 0xd}
P2PPingReplyHeader is the header for P2P connectivity check reply
var P2PPingReplyHeader = [4]byte{0xf, 0xf, 0xc, 0xc}
P2PPingTryHeader is the header for P2P connectivity check both from clients and servers
var P2PPingTryHeader = [4]byte{0x0, 0x0, 0xc, 0xc}
P2PProfile P2P packet protocol header flag for P2P profile
var P2PProfile uint8 = 0x6
P2PRUDPProtoAck P2P packet protocol header flag for P2P RUDP ack
var P2PRUDPProtoAck uint8 = 0x3
P2PRUDPProtoAckOrder P2P packet protocol header flag for P2P RUDP ack order
var P2PRUDPProtoAckOrder uint8 = 0x5
P2PRUDPProtoDat P2P packet protocol header flag for P2P RUDP data
var P2PRUDPProtoDat uint8 = 0x2
P2PRUDPProtoDatOrder P2P packet protocol header flag for P2P RUDP data order
var P2PRUDPProtoDatOrder uint8 = 0x4
P2PUDPProto P2P packet protocol header flag for P2P UDP
var P2PUDPProto uint8 = 0x1
RUDPProtoAck RUDP packet protocol header flag for RUDP ack this flag indicates that the packet is to be treated as the ack packet of dat packet reception
var RUDPProtoAck uint8 = 0x4
RUDPProtoDat RUDP packet protocol header flag for RUDP data this flag indicates that the packet is to be treated as the data packet carrying the actual data payload after syn packet
var RUDPProtoDat uint8 = 0x3
RUDPProtoEack RUDP packet protocol header flag for RUDP eack this flag indicates that the packet is to be treated as the eack of rst packet
var RUDPProtoEack uint8 = 0x6
RUDPProtoFin UDP packet protocol header flag for RUDP fin this flag indicates that the packet is to be treated as the fin packet of RUDP to end RUDP communication
var RUDPProtoFin uint8 = 0x7
RUDPProtoRst RUDP packet protocol header flag for RUDP rst this flag indicates that the packet is to be treated as the retry of dat packet
var RUDPProtoRst uint8 = 0x5
RUDPProtoSyn RUDP packet protocol header flag for RUDP initial syn this flag indicates that the packet is to be treated as the initial RUDP seq synchronization packet
var RUDPProtoSyn uint8 = 0x2
UDPProto UDP packet protocol header flag for UDP this flag indicates the packet is to be treated as a plain UDP packet
var UDPProto uint8 = 0x1
func BoolToBytes(val bool) []byte
BoolToBytes converts bool to bytes
func BytesListToBytes(list [][]byte) []byte
BytesListToBytes converts an array of byte array to byte array
func BytesToBool(bytes []byte) bool
BytesToBool converts bytes to bool
func BytesToBytesList(bytes []byte) [][]byte
BytesToBytesList converts a byte array to an array of byte array
func BytesToBytesListMin(bytes []byte) [][]byte
BytesToBytesListMin converts a byte array to an array of byte array(used by state sync)
func BytesToFloat64(bytes []byte) float64
BytesToFloat64 converts a byte array to a float64
func BytesToFloat64Map(bytes []byte) map[string]float64
BytesToFloat64Map converts a byte array to a map[string]float64
func BytesToStringList(bytes []byte) []string
BytesToStringList Converts a byte array to an array of strings
func BytesToStringListWithLength(bytes []byte, headerSize int, length int) ([]string, int, error)
BytesToStringListWithLength Converts a byte array to an array of strings with a specified header size and array length bytes do not need to be exact size, it is allowed to have extra bytes after the list
It takes three parameters:
And it returns three values:
▹ Example
func BytesToStringListWithSize(bytes []byte, headerSize int) ([]string, error)
BytesToStringListWithSize Converts a byte array to an array of strings with a specified header size bytes need to be exact size for the parsed list;
It takes two parameters:
And it returns two values:
▹ Example (Size2)
▹ Example (Size3)
func CreateHolePunchingPacket(uid string, syn bool, ack bool, hook func([]byte) []byte) ([]byte, error)
CreateHolePunchingPacket creates a P2P packet for hole punching The hook function is used to modify the payload before sending. (primarily for encryption)
func CreateHolePunchingPayload(uid string) []byte
CreateHolePunchingPayload returns the UID from a hole punching packet
func CreateP2PInternalPacket(flags *P2PInternalFlags, payload []byte, hook func([]byte) []byte) ([]byte, error)
CreateP2PInternalPacket creates a P2P packet which is not handled by Application
func CreateP2PPacket(flag uint8, seq uint32, payload []byte) ([]byte, error)
CreateP2PPacket creates a P2P packet
func CreatePingReplyPacket(sid []byte) ([]byte, error)
CreatePingReplyPacket creates a P2P packet for connectivity check reply
func CreatePingTryPacket(sid []byte) ([]byte, error)
CreatePingTryPacket creates a P2P packet for connectivity check
func CreatePingTryPayload(sid []byte) []byte
CreatePingTryPayload returns byte array of the SID
func CreatePushPacket(version uint8, commandID uint16, payload []byte) []byte
CreatePushPacket Creates a push packet
func CreateReconnectPayload(addr string) []byte
CreateReconnectPayload Creates a payload to instruct the client to reconnect
func CreateRequestPacket(version uint8, commandID uint16, payload []byte) []byte
CreateRequestPacket Creates a request packet
func CreateResponsePacket(version uint8, commandID uint16, status uint8, payload []byte) []byte
CreateResponsePacket Creates a response packet - by giving StatusPush as status, it creates a push packet
func CreateSecureRequestPayload(sid []byte, key []byte, iv []byte, mackey []byte, payload []byte) ([]byte, error)
CreateSecureRequestPayload Creates an encrypted request packet payload
func CreateSecureResponsePayload(key []byte, iv []byte, mackey []byte, payload []byte) ([]byte, error)
CreateSecureResponsePayload Creates an encrypted response packet payload
func CreateSplitPacket(id uint16, bytes []byte, splitSize int) [][]byte
CreateSplitPacket creates an array of split packets
func CreateUDPPacket(flag uint8, seq uint32, packet []byte) ([]byte, error)
CreateUDPPacket Creates a UDP packet from the byte array created by
CreateRequestPacket(), CreateResponsePacket(), and CreatePushPacket()
func Float64MapToBytes(m map[string]float64) []byte
Float64MapToBytes converts a map[string]float64 to a byte array
func Float64ToBytes(v float64) []byte
Float64ToBytes converts a float64 to a byte array
func GetSidFromPayload(payload []byte) ([]byte, []byte)
GetSidFromPayload Returns sid (session ID) and encrypted payload from the given payload
func GetSplitPacketID(bytes []byte) uint16
GetSplitPacketID returns the ID of split packet
func IsExpectedP2PPacket(symbol [4]byte, bytes []byte) bool
IsExpectedP2PPacket checks if the byte array is a P2P packet with the expected symbol
func IsInvalidPacketErr(err error) bool
IsInvalidPacketErr Returns true if the given error is an invalid packet error
func IsP2PInternalPacket(bytes []byte) bool
IsP2PInternalPacket checks if the byte array is a P2P internal packet
func IsP2PMessagePacket(bytes []byte) bool
IsP2PMessagePacket checks if the byte array is a P2P packet
func IsP2PPacket(bytes []byte) bool
IsP2PPacket checks if the byte array is a P2P packet
func IsP2PPingReplyPacket(bytes []byte) bool
IsP2PPingReplyPacket checks if the byte array is a P2P connectivity check reply packet
func IsP2PPingTryPacket(bytes []byte) bool
IsP2PPingTryPacket checks if the byte array is a P2P connectivity check packet
func IsPushPacket(status uint8) bool
IsPushPacket Returns true if the given packet status is a push packet
func IsSplitPacket(bytes []byte) bool
IsSplitPacket returns true if the evaluated bytes is a split packet chunk
func PackChatHistory(packedChatHistory [][]byte) []byte
PackChatHistory creates a byte array of list of chat data packed by PackChatMessage
func PackChatMessage(senderID string, timestamp int64, message string) []byte
PackChatMessage creates a chat message data byte array
func PackMMAdd(mmID, uniqueID string, tag string, maxMembers uint16, reserveOnly bool, props map[string]int, metadata []byte, ttl uint16) []byte
PackMMAdd packs command data into data byte array
func PackMMClaim(roomID string, message []byte) []byte
PackMMClaim packs the command data into data byte array
func PackMMIssueTicket(mmIDs []string, props map[string]int) []byte
PackMMIssueTicket packs the command data into data byte array
func PackMMRemove(mmID string, haltFlag bool, uniqueIDs []string, msg []byte) []byte
PackMMRemove packs the command data into data byte array
func PackMMSearch(howmany uint16, joinFlag bool, mmIDs []string, tag string, props map[string]int, msg []byte) []byte
PackMMSearch packs the command data into data byte array
func ParseHolePunchingPayload(payload []byte) (string, error)
ParseHolePunchingPayload returns the UID from a hole punching packet
func ParsePingTryPayload(payload []byte) (string, error)
ParsePingTryPayload returns the SID from a ping try packet
func ParseReconnectPayload(payload []byte) string
ParseReconnectPayload converts the given payload to reconnect address
func ParseUDPPacket(packet []byte) (uint8, uint32, []byte, error)
ParseUDPPacket Parses a packet created by CreateUDPPacket
func StringListToBytes(list []string) []byte
StringListToBytes Converts an array of strings to byte array
func StringListToBytesWithSize(list []string, headerSize int) ([]byte, error)
StringListToBytesWithSize Converts an array of strings to byte array with a specified header size
It takes two parameters:
And it returns two values:
▹ Example (Size2)
▹ Example (Size3)
MMAdd data structure of MatchMaker's matchmaking add and wait command
BigEndian +-------------+--------------+--------+-------------+-----+-------------+-----+-----+ | Max Members | Reserve Flag | TTL | Size of (1) | (1) | Size of (2) | (2) | (3) | +-------------+--------------+--------+-------------+-----+-------------+-----+-----+ | 2 bytes | 1 byte | 2 byte | 2 byte | (1) | 2 byte | (2) | (3) | +-------------+--------------+--------+-------------+-----+-------------+-----+-----+ (1) - Matchmaking profile ID, UID, and Tag +------------------------+------------------------+-------------+-------------+-------------+-------------+ | Size of matchmaking ID | Matchmaking ID | Size of UID | UID | Size of Tag | Tag | +------------------------+------------------------+-------------+-------------+-------------+-------------+ | 4 byte | Size of matchmaking ID | 4 byte | Size of UID | 4 byte | Size of Tag | +------------------------+------------------------+-------------+-------------+-------------+-------------+ (2) - Properties For multiple propery values, value, size of name, and name repeats as a data set +---------------+-----------------------+-----------------------+ | Propety Value | Size of property name | Propery name | ... +---------------+-----------------------+-----------------------+ | 4 byte | 2 byte | Size of property name | ... +---------------+-----------------------+-----------------------+ (3) - Metadata +--------------------------------------------------------------+ | Metadata | +--------------------------------------------------------------+ | From the end of properties to the end of the data byte array | +--------------------------------------------------------------+
type MMAdd struct { TTL int64 ID string UID string Tag string Props map[string]int Metadata []byte MaxMembers int ReserveOnly bool }
func UnpackMMAdd(bytes []byte) *MMAdd
UnpackMMAdd unpacks the data byte array to data structure
MMClaim MatchMaker's reservation claim command data structure
BigEndian +-----------------+---------+----------------------------------------------+ | Size of room ID | Room ID | Message | +-----------------+---------+----------------------------------------------+ | 2 byte | | end of room ID to the end of data byte array | +-----------------+---------+----------------------------------------------+
type MMClaim struct { RoomID string Message []byte }
func UnpackMMClaim(bytes []byte) *MMClaim
UnpackMMClaim unpacks command data byte array
MMIssueTicket data structure of MatchMaker's issue ticket command
BigEndian +-------------------------+-----------------+--------------------+-----+-----+ | Size of matchmaking IDs | Matchmaking IDs | Size of properties | (1) | (2) | +-------------------------+-----------------+--------------------+-----------+ | 2 byte | | 2 byte | (1) | (2) | +-------------------------+-----------------+--------------------+-----+-----+ (1) - Properties For multiple property values, value, size of name, and name repeats as a data set +----------------+-----------------------+-----------------------+ | Property Value | Size of property name | Property name | ... +----------------+-----------------------+-----------------------+ | 4 byte | 2 byte | Size of property name | ... +----------------+-----------------------+-----------------------+
type MMIssueTicket struct { IDs []string Props map[string]int }
func UnpackMMIssueTicket(bytes []byte) *MMIssueTicket
UnpackMMIssueTicket unpacks the command data array
MMRemove data structure of MatchMaker's remove and abort of matchmaking command
BigEndian +-----------+------------------------+----------------+-------------+-----+-----------------------------------------------+ | Halt Flag | Size of matchmaking ID | Matchmaking ID | Size of UID | UID | Message | +-----------+------------------------+----------------+-------------+-----+-----------------------------------------------+ | 1 byte | 4 byte | | 4 byte | | From end of UID to the end of data byte array | +-----------+------------------------+----------------+-------------+-----+-----------------------------------------------+
type MMRemove struct { HaltFlag bool ID string UIDs []string Message []byte }
func UnpackMMRemove(bytes []byte) *MMRemove
UnpackMMRemove unpacks the data byte array to data struct
MMSearch data structure of MatchMaker's search command
BigEndian +------------------+-----------+-------------------------+-----------------+-------------+-----+--------------------+-----+-----+ | How many results | Join Flag | Size of matchmaking IDs | Matchmaking IDs | Size of Tag | Tag | Size of properties | (1) | (2) | +------------------+-----------+-------------------------+-----------------+-------------+------+--------------------+-----------+ | 2 byte | 1 byte | 2 byte | | 2 byte | | 2 byte | (1) | (2) | +------------------+-----------+-------------------------+-----------------+-------------+------+--------------------+-----+-----+ (1) - Properties For multiple propery values, value, size of name, and name repeats as a data set +---------------+-----------------------+-----------------------+ | Propety Value | Size of property name | Propery name | ... +---------------+-----------------------+-----------------------+ | 4 byte | 2 byte | Size of property name | ... +---------------+-----------------------+-----------------------+ (2) Message +------------------------------------------------------+ | Message | +------------------------------------------------------+ | From end of properties to the end of data byte array | +------------------------------------------------------+
type MMSearch struct { HowMany int Join bool IDs []string Tag string Props map[string]int Message []byte }
func UnpackMMSearch(bytes []byte) *MMSearch
UnpackMMSearch unpacks data byte array to data structure
P2PInternalFlags is the flags for P2PInternalPacket mainly for hole punching
type P2PInternalFlags struct { HolePunchAck bool HolePunchSyn bool Fin bool KeepAlive bool }
func ParseP2PInternalFlags(packet []byte) *P2PInternalFlags
ParseP2PInternalFlags parses the flags from a P2P internal packet
func (flags *P2PInternalFlags) String() string
P2PPacket is a parsed structure of a P2P packet
type P2PPacket struct { IsInternal bool IsPingTry bool IsPingReply bool IsSplit bool UDPHeader *UDPHeader Flags *P2PInternalFlags Payload []byte }
func ParseP2PPacket(packet []byte, hook func([]byte) []byte) (*P2PPacket, bool)
ParseP2PPacket checks if the byte array is a P2P packet. If it is, it parses a P2P packet into the flags and the payload The hook function is used to modify the payload before parsing. (primarily for decryption) We need to decrypt at this point because internal flags are inside the encrypted payload We cannot decrypt split packets here as the split is done after encryption so we need to try decrypting in the later process again
RequestHeader Request header data structure
type RequestHeader struct { Version uint8 CommandID uint16 PayloadSize uint32 }
RequestPacket Request packet data structure
type RequestPacket struct { Header *RequestHeader Payload []byte SeqForDup uint32 }
func ParseRequestPacket(packet []byte) (*RequestPacket, int, error)
ParseRequestPacket Parses a request packet
ResponseHeader Response header data structure
type ResponseHeader struct { Version uint8 CommandID uint16 PayloadSize uint32 Status uint8 }
ResponsePacket Response packet data structure
type ResponsePacket struct { Header *ResponseHeader Payload []byte Push bool }
func ParseResponsePacket(packet []byte) (*ResponsePacket, int, error)
ParseResponsePacket Parses a response packet
SplitPacket a packet that are split into smaller chunks
type SplitPacket struct { ID uint16 sync.RWMutex // contains filtered or unexported fields }
func NewSplitPacket(bytes []byte) *SplitPacket
NewSplitPacket creates a split packet receiver
func (sp *SplitPacket) Add(bytes []byte) error
Add adds split packet chunk to split packet receiver
func (sp *SplitPacket) ConsumeBytes() ([]byte, bool)
ConsumeBytes returns the reconstructed split bytes
UDPHeader UDP packet protocol header
type UDPHeader struct { Flag uint8 Seq uint32 }