conn.go 1020 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package gateway
  2. import (
  3. "net"
  4. "time"
  5. )
  6. type Conn struct {
  7. buf []byte
  8. conn net.Conn
  9. }
  10. func (c *Conn) Read(b []byte) (n int, err error) {
  11. var m int
  12. if len(c.buf) > 0 {
  13. if len(b) >= len(c.buf) {
  14. m = copy(b[:], c.buf[:])
  15. c.buf = c.buf[m:]
  16. }
  17. }
  18. n, err = c.conn.Read(b[m:])
  19. n += m
  20. return
  21. }
  22. func (c *Conn) Write(b []byte) (n int, err error) {
  23. return c.conn.Write(b)
  24. }
  25. func (c *Conn) Close() error {
  26. return c.conn.Close()
  27. }
  28. func (c *Conn) LocalAddr() net.Addr {
  29. return c.conn.LocalAddr()
  30. }
  31. func (c *Conn) RemoteAddr() net.Addr {
  32. return c.conn.RemoteAddr()
  33. }
  34. func (c *Conn) SetDeadline(t time.Time) error {
  35. return c.conn.SetDeadline(t)
  36. }
  37. func (c *Conn) SetReadDeadline(t time.Time) error {
  38. return c.conn.SetReadDeadline(t)
  39. }
  40. func (c *Conn) SetWriteDeadline(t time.Time) error {
  41. return c.conn.SetWriteDeadline(t)
  42. }
  43. func wrapConn(c net.Conn, buf []byte) net.Conn {
  44. conn := &Conn{conn: c, buf: buf}
  45. if buf != nil {
  46. conn.buf = make([]byte, len(buf))
  47. copy(conn.buf, buf)
  48. }
  49. return conn
  50. }