stream.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package yamux
  2. import (
  3. "bytes"
  4. "net"
  5. "time"
  6. )
  7. type streamState int
  8. const (
  9. streamSYNSent streamState = iota
  10. streamSYNReceived
  11. streamEstablished
  12. streamLocalClose
  13. streamRemoteClose
  14. streamClosed
  15. )
  16. // Stream is used to represent a logical stream
  17. // within a session.
  18. type Stream struct {
  19. id uint32
  20. session *Session
  21. state streamState
  22. recvBuf bytes.Buffer
  23. recvWindow uint32
  24. sendBuf bytes.Buffer
  25. sendWindow uint32
  26. readDeadline time.Time
  27. writeDeadline time.Time
  28. }
  29. // Session returns the associated stream session
  30. func (s *Stream) Session() *Session {
  31. return s.session
  32. }
  33. // StreamID returns the ID of this stream
  34. func (s *Stream) StreamID() uint32 {
  35. return s.id
  36. }
  37. // Read is used to read from the stream
  38. func (s *Stream) Read(b []byte) (int, error) {
  39. return 0, nil
  40. }
  41. // Write is used to write to the stream
  42. func (s *Stream) Write(b []byte) (int, error) {
  43. return 0, nil
  44. }
  45. // Close is used to close the stream
  46. func (s *Stream) Close() error {
  47. return nil
  48. }
  49. // LocalAddr returns the local address
  50. func (s *Stream) LocalAddr() net.Addr {
  51. return s.session.LocalAddr()
  52. }
  53. // LocalAddr returns the remote address
  54. func (s *Stream) RemoteAddr() net.Addr {
  55. return s.session.RemoteAddr()
  56. }
  57. // SetDeadline sets the read and write deadlines
  58. func (s *Stream) SetDeadline(t time.Time) error {
  59. if err := s.SetReadDeadline(t); err != nil {
  60. return err
  61. }
  62. if err := s.SetWriteDeadline(t); err != nil {
  63. return err
  64. }
  65. return nil
  66. }
  67. // SetReadDeadline sets the deadline for future Read calls.
  68. func (s *Stream) SetReadDeadline(t time.Time) error {
  69. s.readDeadline = t
  70. return nil
  71. }
  72. // SetWriteDeadline sets the deadline for future Write calls
  73. func (s *Stream) SetWriteDeadline(t time.Time) error {
  74. s.writeDeadline = t
  75. return nil
  76. }