packet.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package TcpServerLib
  2. // protocol Id
  3. const (
  4. Pkt_ID_Echo_Req = 101
  5. Pkt_ID_Echo_Res = 102
  6. Pkt_ID_LoginReq = 201
  7. Pkt_ID_LoginRes = 202
  8. Pkt_ID_NewRoomReq = 203
  9. Pkt_ID_NewRoomRes = 204
  10. Pkt_ID_EnterRoomReq = 206
  11. Pkt_ID_EnterRoomRes = 207
  12. Pkt_ID_LeaveRoomReq = 209
  13. Pkt_ID_LeaveRoomRes = 210
  14. Pkt_ID_ChatRoomReq = 214
  15. Pkt_ID_ChatRoomRes = 215
  16. Pkt_ID_ChatRoomNtf = 216
  17. )
  18. type PacketHeader struct {
  19. TotalSize int16
  20. Id int16
  21. }
  22. func DecodingPacketHeader(header *PacketHeader, data []byte) {
  23. reader := MakeReader(data, true)
  24. header.TotalSize, _ = reader.ReadS16()
  25. header.Id, _ = reader.ReadS16()
  26. }
  27. func EncodingPacketHeader(writer *RawPacketData, totalSize int16, pktId int16, packetType int8) {
  28. writer.WriteS16(totalSize)
  29. writer.WriteS16(pktId)
  30. }
  31. type RoomChatReqPacket struct {
  32. MsgLength int16
  33. Msgs []byte
  34. }
  35. func (request RoomChatReqPacket) EncodingPacket(packetId int16) ([]byte, int16) {
  36. totalSize := HEADER_SIZE + 2 + int16(request.MsgLength)
  37. sendBuf := make([]byte, totalSize)
  38. writer := MakeWriter(sendBuf, true)
  39. EncodingPacketHeader(&writer, totalSize, packetId, 0)
  40. writer.WriteS16(request.MsgLength)
  41. writer.WriteBytes(request.Msgs)
  42. return sendBuf, totalSize
  43. }
  44. func (request *RoomChatReqPacket) DecodingPacketPre(Data []byte) bool {
  45. bodyLength := len(Data)
  46. if bodyLength < 2 {
  47. return false
  48. }
  49. reader := MakeReader(Data, true)
  50. request.MsgLength, _ = reader.ReadS16()
  51. if bodyLength != int((2 + request.MsgLength)) {
  52. return false
  53. }
  54. request.Msgs = Data[2:]
  55. return true
  56. }