...

Package packet

import "github.com/Diarkis/diarkis/packet"
Overview
Index
Examples

Overview ▾

Index ▾

Constants
Variables
func BoolToBytes(val bool) []byte
func BytesListToBytes(list [][]byte) []byte
func BytesToBool(bytes []byte) bool
func BytesToBytesList(bytes []byte) [][]byte
func BytesToBytesListMin(bytes []byte) [][]byte
func BytesToFloat64(bytes []byte) float64
func BytesToFloat64Map(bytes []byte) map[string]float64
func BytesToStringList(bytes []byte) []string
func BytesToStringListWithLength(bytes []byte, headerSize int, length int) ([]string, int, error)
func BytesToStringListWithSize(bytes []byte, headerSize int) ([]string, error)
func CreateHolePunchingPacket(uid string, syn bool, ack bool, hook func([]byte) []byte) ([]byte, error)
func CreateHolePunchingPayload(uid string) []byte
func CreateP2PInternalPacket(flags *P2PInternalFlags, payload []byte, hook func([]byte) []byte) ([]byte, error)
func CreateP2PPacket(flag uint8, seq uint32, payload []byte) ([]byte, error)
func CreatePingReplyPacket(sid []byte) ([]byte, error)
func CreatePingTryPacket(sid []byte) ([]byte, error)
func CreatePingTryPayload(sid []byte) []byte
func CreatePushPacket(version uint8, commandID uint16, payload []byte) []byte
func CreateReconnectPayload(addr string) []byte
func CreateRequestPacket(version uint8, commandID uint16, payload []byte) []byte
func CreateResponsePacket(version uint8, commandID uint16, status uint8, payload []byte) []byte
func CreateSecureRequestPayload(sid []byte, key []byte, iv []byte, mackey []byte, payload []byte) ([]byte, error)
func CreateSecureResponsePayload(key []byte, iv []byte, mackey []byte, payload []byte) ([]byte, error)
func CreateSplitPacket(id uint16, bytes []byte, splitSize int) [][]byte
func CreateUDPPacket(flag uint8, seq uint32, packet []byte) ([]byte, error)
func Float64MapToBytes(m map[string]float64) []byte
func Float64ToBytes(v float64) []byte
func GetSidFromPayload(payload []byte) ([]byte, []byte)
func GetSplitPacketID(bytes []byte) uint16
func IsExpectedP2PPacket(symbol [4]byte, bytes []byte) bool
func IsInvalidPacketErr(err error) bool
func IsP2PInternalPacket(bytes []byte) bool
func IsP2PMessagePacket(bytes []byte) bool
func IsP2PPacket(bytes []byte) bool
func IsP2PPingReplyPacket(bytes []byte) bool
func IsP2PPingTryPacket(bytes []byte) bool
func IsPushPacket(status uint8) bool
func IsSplitPacket(bytes []byte) bool
func PackChatHistory(packedChatHistory [][]byte) []byte
func PackChatMessage(senderID string, timestamp int64, message string) []byte
func PackMMAdd(mmID, uniqueID string, tag string, maxMembers uint16, reserveOnly bool, props map[string]int, metadata []byte, ttl uint16) []byte
func PackMMClaim(roomID string, message []byte) []byte
func PackMMIssueTicket(mmIDs []string, props map[string]int) []byte
func PackMMRemove(mmID string, haltFlag bool, uniqueIDs []string, msg []byte) []byte
func PackMMSearch(howmany uint16, joinFlag bool, mmIDs []string, tag string, props map[string]int, msg []byte) []byte
func ParseHolePunchingPayload(payload []byte) (string, error)
func ParsePingTryPayload(payload []byte) (string, error)
func ParseReconnectPayload(payload []byte) string
func ParseUDPPacket(packet []byte) (uint8, uint32, []byte, error)
func StringListToBytes(list []string) []byte
func StringListToBytesWithSize(list []string, headerSize int) ([]byte, error)
type MMAdd
    func UnpackMMAdd(bytes []byte) *MMAdd
type MMClaim
    func UnpackMMClaim(bytes []byte) *MMClaim
type MMIssueTicket
    func UnpackMMIssueTicket(bytes []byte) *MMIssueTicket
type MMRemove
    func UnpackMMRemove(bytes []byte) *MMRemove
type MMSearch
    func UnpackMMSearch(bytes []byte) *MMSearch
type P2PInternalFlags
    func ParseP2PInternalFlags(packet []byte) *P2PInternalFlags
    func (flags *P2PInternalFlags) String() string
type P2PPacket
    func ParseP2PPacket(packet []byte, hook func([]byte) []byte) (*P2PPacket, bool)
type RequestHeader
type RequestPacket
    func ParseRequestPacket(packet []byte) (*RequestPacket, int, error)
type ResponseHeader
type ResponsePacket
    func ParseResponsePacket(packet []byte) (*ResponsePacket, int, error)
type SplitPacket
    func NewSplitPacket(bytes []byte) *SplitPacket
    func (sp *SplitPacket) Add(bytes []byte) error
    func (sp *SplitPacket) ConsumeBytes() ([]byte, bool)
type UDPHeader

Constants

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

Variables

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

func BoolToBytes(val bool) []byte

BoolToBytes converts bool to bytes

func BytesListToBytes

func BytesListToBytes(list [][]byte) []byte

BytesListToBytes converts an array of byte array to byte array

func BytesToBool

func BytesToBool(bytes []byte) bool

BytesToBool converts bytes to bool

func BytesToBytesList

func BytesToBytesList(bytes []byte) [][]byte

BytesToBytesList converts a byte array to an array of byte array

func BytesToBytesListMin

func BytesToBytesListMin(bytes []byte) [][]byte

BytesToBytesListMin converts a byte array to an array of byte array(used by state sync)

func BytesToFloat64

func BytesToFloat64(bytes []byte) float64

BytesToFloat64 converts a byte array to a float64

func BytesToFloat64Map

func BytesToFloat64Map(bytes []byte) map[string]float64

BytesToFloat64Map converts a byte array to a map[string]float64

func BytesToStringList

func BytesToStringList(bytes []byte) []string

BytesToStringList Converts a byte array to an array of strings

func BytesToStringListWithLength

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:

  1. bytes, byte list that includes header byte for each which indicates its size;
  2. headerSize, how many bytes it uses for its header; and
  3. length, recurring time.

And it returns three values:

  1. parsed string list;
  2. total size after parsing ; and
  3. error if it fails to convert.

Example

Code:

headerSize := 1
bytes := []byte{1, 65, 2, 66, 67, 3, 68, 69, 70}
length := 2
list, size, err := packet.BytesToStringListWithLength(bytes, headerSize, length)
fmt.Println(list, size, err)

Output:

[A BC] 5 <nil>

func BytesToStringListWithSize

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:

  1. bytes, byte list that includes header byte for each which indicates its size; and
  2. headerSize, how many bytes it uses for its header.

And it returns two values:

  1. parsed string list; and
  2. error if it fails to convert.

Example (Size2)

Code:

headerSize := 2
bytes := []byte{0, 1, 65, 0, 2, 66, 67, 0, 3, 68, 69, 70}
ret, err := packet.BytesToStringListWithSize(bytes, headerSize)
fmt.Println(ret, err)

Output:

[A BC DEF] <nil>

Example (Size3)

Code:

headerSize := 3
bytes := []byte{0, 0, 1, 65, 0, 0, 2, 66, 67}
ret, err := packet.BytesToStringListWithSize(bytes, headerSize)
fmt.Println(ret, err)

Output:

[A BC] <nil>

func CreateHolePunchingPacket

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

func CreateHolePunchingPayload(uid string) []byte

CreateHolePunchingPayload returns the UID from a hole punching packet

func CreateP2PInternalPacket

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

func CreateP2PPacket(flag uint8, seq uint32, payload []byte) ([]byte, error)

CreateP2PPacket creates a P2P packet

func CreatePingReplyPacket

func CreatePingReplyPacket(sid []byte) ([]byte, error)

CreatePingReplyPacket creates a P2P packet for connectivity check reply

func CreatePingTryPacket

func CreatePingTryPacket(sid []byte) ([]byte, error)

CreatePingTryPacket creates a P2P packet for connectivity check

func CreatePingTryPayload

func CreatePingTryPayload(sid []byte) []byte

CreatePingTryPayload returns byte array of the SID

func CreatePushPacket

func CreatePushPacket(version uint8, commandID uint16, payload []byte) []byte

CreatePushPacket Creates a push packet

func CreateReconnectPayload

func CreateReconnectPayload(addr string) []byte

CreateReconnectPayload Creates a payload to instruct the client to reconnect

func CreateRequestPacket

func CreateRequestPacket(version uint8, commandID uint16, payload []byte) []byte

CreateRequestPacket Creates a request packet

func CreateResponsePacket

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

func CreateSecureRequestPayload(sid []byte, key []byte, iv []byte, mackey []byte, payload []byte) ([]byte, error)

CreateSecureRequestPayload Creates an encrypted request packet payload

func CreateSecureResponsePayload

func CreateSecureResponsePayload(key []byte, iv []byte, mackey []byte, payload []byte) ([]byte, error)

CreateSecureResponsePayload Creates an encrypted response packet payload

func CreateSplitPacket

func CreateSplitPacket(id uint16, bytes []byte, splitSize int) [][]byte

CreateSplitPacket creates an array of split packets

func CreateUDPPacket

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

func Float64MapToBytes(m map[string]float64) []byte

Float64MapToBytes converts a map[string]float64 to a byte array

func Float64ToBytes

func Float64ToBytes(v float64) []byte

Float64ToBytes converts a float64 to a byte array

func GetSidFromPayload

func GetSidFromPayload(payload []byte) ([]byte, []byte)

GetSidFromPayload Returns sid (session ID) and encrypted payload from the given payload

func GetSplitPacketID

func GetSplitPacketID(bytes []byte) uint16

GetSplitPacketID returns the ID of split packet

func IsExpectedP2PPacket

func IsExpectedP2PPacket(symbol [4]byte, bytes []byte) bool

IsExpectedP2PPacket checks if the byte array is a P2P packet with the expected symbol

func IsInvalidPacketErr

func IsInvalidPacketErr(err error) bool

IsInvalidPacketErr Returns true if the given error is an invalid packet error

func IsP2PInternalPacket

func IsP2PInternalPacket(bytes []byte) bool

IsP2PInternalPacket checks if the byte array is a P2P internal packet

func IsP2PMessagePacket

func IsP2PMessagePacket(bytes []byte) bool

IsP2PMessagePacket checks if the byte array is a P2P packet

func IsP2PPacket

func IsP2PPacket(bytes []byte) bool

IsP2PPacket checks if the byte array is a P2P packet

func IsP2PPingReplyPacket

func IsP2PPingReplyPacket(bytes []byte) bool

IsP2PPingReplyPacket checks if the byte array is a P2P connectivity check reply packet

func IsP2PPingTryPacket

func IsP2PPingTryPacket(bytes []byte) bool

IsP2PPingTryPacket checks if the byte array is a P2P connectivity check packet

func IsPushPacket

func IsPushPacket(status uint8) bool

IsPushPacket Returns true if the given packet status is a push packet

func IsSplitPacket

func IsSplitPacket(bytes []byte) bool

IsSplitPacket returns true if the evaluated bytes is a split packet chunk

func PackChatHistory

func PackChatHistory(packedChatHistory [][]byte) []byte

PackChatHistory creates a byte array of list of chat data packed by PackChatMessage

func PackChatMessage

func PackChatMessage(senderID string, timestamp int64, message string) []byte

PackChatMessage creates a chat message data byte array

func PackMMAdd

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

func PackMMClaim(roomID string, message []byte) []byte

PackMMClaim packs the command data into data byte array

func PackMMIssueTicket

func PackMMIssueTicket(mmIDs []string, props map[string]int) []byte

PackMMIssueTicket packs the command data into data byte array

func PackMMRemove

func PackMMRemove(mmID string, haltFlag bool, uniqueIDs []string, msg []byte) []byte

PackMMRemove packs the command data into data byte array

func PackMMSearch

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

func ParseHolePunchingPayload(payload []byte) (string, error)

ParseHolePunchingPayload returns the UID from a hole punching packet

func ParsePingTryPayload

func ParsePingTryPayload(payload []byte) (string, error)

ParsePingTryPayload returns the SID from a ping try packet

func ParseReconnectPayload

func ParseReconnectPayload(payload []byte) string

ParseReconnectPayload converts the given payload to reconnect address

func ParseUDPPacket

func ParseUDPPacket(packet []byte) (uint8, uint32, []byte, error)

ParseUDPPacket Parses a packet created by CreateUDPPacket

func StringListToBytes

func StringListToBytes(list []string) []byte

StringListToBytes Converts an array of strings to byte array

func StringListToBytesWithSize

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:

  1. list, slice of string that you want to convert to bytes; and
  2. headerSize, how many bytes it uses for its header;

And it returns two values:

  1. converted byte list;
  2. error if it fails to convert.

Example (Size2)

Code:

list := []string{"A", "BC", "DEF"}
headerSize := 2
ret, err := packet.StringListToBytesWithSize(list, headerSize)
fmt.Println(ret, err)

Output:

[0 1 65 0 2 66 67 0 3 68 69 70] <nil>

Example (Size3)

Code:

list := []string{"A", "BC"}
headerSize := 3
ret, err := packet.StringListToBytesWithSize(list, headerSize)
fmt.Println(ret, err)

Output:

[0 0 1 65 0 0 2 66 67] <nil>

type MMAdd

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

func UnpackMMAdd(bytes []byte) *MMAdd

UnpackMMAdd unpacks the data byte array to data structure

type MMClaim

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

func UnpackMMClaim(bytes []byte) *MMClaim

UnpackMMClaim unpacks command data byte array

type MMIssueTicket

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

func UnpackMMIssueTicket(bytes []byte) *MMIssueTicket

UnpackMMIssueTicket unpacks the command data array

type MMRemove

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

func UnpackMMRemove(bytes []byte) *MMRemove

UnpackMMRemove unpacks the data byte array to data struct

type MMSearch

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

func UnpackMMSearch(bytes []byte) *MMSearch

UnpackMMSearch unpacks data byte array to data structure

type P2PInternalFlags

P2PInternalFlags is the flags for P2PInternalPacket mainly for hole punching

type P2PInternalFlags struct {
    HolePunchAck bool
    HolePunchSyn bool
    Fin          bool
    KeepAlive    bool
}

func ParseP2PInternalFlags

func ParseP2PInternalFlags(packet []byte) *P2PInternalFlags

ParseP2PInternalFlags parses the flags from a P2P internal packet

func (*P2PInternalFlags) String

func (flags *P2PInternalFlags) String() string

type P2PPacket

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

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

type RequestHeader

RequestHeader Request header data structure

type RequestHeader struct {
    Version     uint8
    CommandID   uint16
    PayloadSize uint32
}

type RequestPacket

RequestPacket Request packet data structure

type RequestPacket struct {
    Header    *RequestHeader
    Payload   []byte
    SeqForDup uint32
}

func ParseRequestPacket

func ParseRequestPacket(packet []byte) (*RequestPacket, int, error)

ParseRequestPacket Parses a request packet

type ResponseHeader

ResponseHeader Response header data structure

type ResponseHeader struct {
    Version     uint8
    CommandID   uint16
    PayloadSize uint32
    Status      uint8
}

type ResponsePacket

ResponsePacket Response packet data structure

type ResponsePacket struct {
    Header  *ResponseHeader
    Payload []byte
    Push    bool
}

func ParseResponsePacket

func ParseResponsePacket(packet []byte) (*ResponsePacket, int, error)

ParseResponsePacket Parses a response packet

type SplitPacket

SplitPacket a packet that are split into smaller chunks

type SplitPacket struct {
    ID uint16

    sync.RWMutex
    // contains filtered or unexported fields
}

func NewSplitPacket

func NewSplitPacket(bytes []byte) *SplitPacket

NewSplitPacket creates a split packet receiver

func (*SplitPacket) Add

func (sp *SplitPacket) Add(bytes []byte) error

Add adds split packet chunk to split packet receiver

func (*SplitPacket) ConsumeBytes

func (sp *SplitPacket) ConsumeBytes() ([]byte, bool)

ConsumeBytes returns the reconstructed split bytes

type UDPHeader

UDPHeader UDP packet protocol header

type UDPHeader struct {
    Flag uint8
    Seq  uint32
}