packet.go 880 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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) *UDPPacket {
  13. return &UDPPacket{
  14. basePacket: basePacket{destination: dest},
  15. data: data,
  16. }
  17. }
  18. type basePacket struct {
  19. destination Destination
  20. }
  21. func (base basePacket) Destination() Destination {
  22. return base.destination
  23. }
  24. type TCPPacket struct {
  25. basePacket
  26. }
  27. func (packet *TCPPacket) Chunk() []byte {
  28. return nil
  29. }
  30. func (packet *TCPPacket) MoreChunks() bool {
  31. return true
  32. }
  33. type UDPPacket struct {
  34. basePacket
  35. data []byte
  36. }
  37. func (packet *UDPPacket) Chunk() []byte {
  38. return packet.data
  39. }
  40. func (packet *UDPPacket) MoreChunks() bool {
  41. return false
  42. }