major refactor
This commit is contained in:
130
packet.go
130
packet.go
@@ -1,85 +1,77 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"io"
|
||||
)
|
||||
|
||||
type CartMessage uint32
|
||||
type PackageVersion uint32
|
||||
|
||||
const (
|
||||
RemoteGetState = CartMessage(0x01)
|
||||
RemoteHandleMutation = CartMessage(0x02)
|
||||
ResponseBody = CartMessage(0x03)
|
||||
RemoteGetStateReply = CartMessage(0x04)
|
||||
RemoteHandleMutationReply = CartMessage(0x05)
|
||||
RemoteGetState = FrameType(0x01)
|
||||
RemoteHandleMutation = FrameType(0x02)
|
||||
ResponseBody = FrameType(0x03)
|
||||
RemoteGetStateReply = FrameType(0x04)
|
||||
RemoteHandleMutationReply = FrameType(0x05)
|
||||
)
|
||||
|
||||
type CartPacket struct {
|
||||
Version PackageVersion
|
||||
MessageType CartMessage
|
||||
DataLength uint32
|
||||
StatusCode uint32
|
||||
Id CartId
|
||||
}
|
||||
// type CartPacket struct {
|
||||
// Version PackageVersion
|
||||
// MessageType CartMessage
|
||||
// DataLength uint32
|
||||
// StatusCode uint32
|
||||
// Id CartId
|
||||
// }
|
||||
|
||||
type Packet struct {
|
||||
Version PackageVersion
|
||||
MessageType PoolMessage
|
||||
DataLength uint32
|
||||
StatusCode uint32
|
||||
}
|
||||
// type Packet struct {
|
||||
// Version PackageVersion
|
||||
// MessageType PoolMessage
|
||||
// DataLength uint32
|
||||
// StatusCode uint32
|
||||
// }
|
||||
|
||||
var headerData = make([]byte, 4)
|
||||
// var headerData = make([]byte, 4)
|
||||
|
||||
func matchHeader(conn io.Reader) error {
|
||||
// func matchHeader(conn io.Reader) error {
|
||||
|
||||
pos := 0
|
||||
for pos < 4 {
|
||||
// pos := 0
|
||||
// for pos < 4 {
|
||||
|
||||
l, err := conn.Read(headerData)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for i := 0; i < l; i++ {
|
||||
if headerData[i] == header[pos] {
|
||||
pos++
|
||||
if pos == 4 {
|
||||
return nil
|
||||
}
|
||||
} else {
|
||||
pos = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
// l, err := conn.Read(headerData)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// for i := 0; i < l; i++ {
|
||||
// if headerData[i] == header[pos] {
|
||||
// pos++
|
||||
// if pos == 4 {
|
||||
// return nil
|
||||
// }
|
||||
// } else {
|
||||
// pos = 0
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return nil
|
||||
// }
|
||||
|
||||
func ReadPacket(conn io.Reader, packet *Packet) error {
|
||||
err := matchHeader(conn)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return binary.Read(conn, binary.LittleEndian, packet)
|
||||
}
|
||||
// func ReadPacket(conn io.Reader, packet *Packet) error {
|
||||
// err := matchHeader(conn)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// return binary.Read(conn, binary.LittleEndian, packet)
|
||||
// }
|
||||
|
||||
func ReadCartPacket(conn io.Reader, packet *CartPacket) error {
|
||||
err := matchHeader(conn)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return binary.Read(conn, binary.LittleEndian, packet)
|
||||
}
|
||||
// func ReadCartPacket(conn io.Reader, packet *CartPacket) error {
|
||||
// err := matchHeader(conn)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// return binary.Read(conn, binary.LittleEndian, packet)
|
||||
// }
|
||||
|
||||
func GetPacketData(conn io.Reader, len uint32) ([]byte, error) {
|
||||
if len == 0 {
|
||||
return []byte{}, nil
|
||||
}
|
||||
data := make([]byte, len)
|
||||
_, err := conn.Read(data)
|
||||
return data, err
|
||||
}
|
||||
// func GetPacketData(conn io.Reader, len uint32) ([]byte, error) {
|
||||
// if len == 0 {
|
||||
// return []byte{}, nil
|
||||
// }
|
||||
// data := make([]byte, len)
|
||||
// _, err := conn.Read(data)
|
||||
// return data, err
|
||||
// }
|
||||
|
||||
// func ReceivePacket(conn io.Reader) (uint32, []byte, error) {
|
||||
// var packet Packet
|
||||
|
||||
Reference in New Issue
Block a user