packet.go 982 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package net
  2. type Packet interface {
  3. Destination() Destination
  4. Chunk() []byte // First chunk of this commnunication
  5. MoreChunks() bool
  6. }
  7. func NewTCPPacket(dest Destination) *TCPPacket {
  8. return &TCPPacket{
  9. basePacket: basePacket{destination: dest},
  10. }
  11. }
  12. func NewUDPPacket(dest Destination, data []byte, id uint16) *UDPPacket {
  13. return &UDPPacket{
  14. basePacket: basePacket{destination: dest},
  15. data: data,
  16. id: id,
  17. }
  18. }
  19. type basePacket struct {
  20. destination Destination
  21. }
  22. func (base basePacket) Destination() Destination {
  23. return base.destination
  24. }
  25. type TCPPacket struct {
  26. basePacket
  27. }
  28. func (packet *TCPPacket) Chunk() []byte {
  29. return nil
  30. }
  31. func (packet *TCPPacket) MoreChunks() bool {
  32. return true
  33. }
  34. type UDPPacket struct {
  35. basePacket
  36. data []byte
  37. id uint16
  38. }
  39. func (packet *UDPPacket) ID() uint16 {
  40. return packet.id
  41. }
  42. func (packet *UDPPacket) Chunk() []byte {
  43. return packet.data
  44. }
  45. func (packet *UDPPacket) MoreChunks() bool {
  46. return false
  47. }