packet.go 960 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package net
  2. import (
  3. "github.com/v2ray/v2ray-core/common"
  4. "github.com/v2ray/v2ray-core/common/alloc"
  5. )
  6. // Packet is a network packet to be sent to destination.
  7. type Packet interface {
  8. common.Releasable
  9. Destination() Destination
  10. Chunk() *alloc.Buffer // First chunk of this commnunication
  11. MoreChunks() bool
  12. }
  13. // NewPacket creates a new Packet with given destination and payload.
  14. func NewPacket(dest Destination, firstChunk *alloc.Buffer, moreChunks bool) Packet {
  15. return &packetImpl{
  16. dest: dest,
  17. data: firstChunk,
  18. moreData: moreChunks,
  19. }
  20. }
  21. type packetImpl struct {
  22. dest Destination
  23. data *alloc.Buffer
  24. moreData bool
  25. }
  26. func (packet *packetImpl) Destination() Destination {
  27. return packet.dest
  28. }
  29. func (packet *packetImpl) Chunk() *alloc.Buffer {
  30. return packet.data
  31. }
  32. func (packet *packetImpl) MoreChunks() bool {
  33. return packet.moreData
  34. }
  35. func (packet *packetImpl) Release() {
  36. packet.data.Release()
  37. packet.data = nil
  38. }