connection.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package hub
  2. import (
  3. "net"
  4. "time"
  5. )
  6. type ConnectionHandler func(*Connection)
  7. type Connection struct {
  8. conn net.Conn
  9. listener *TCPHub
  10. }
  11. func (this *Connection) Read(b []byte) (int, error) {
  12. if this == nil || this.conn == nil {
  13. return 0, ErrorClosedConnection
  14. }
  15. return this.conn.Read(b)
  16. }
  17. func (this *Connection) Write(b []byte) (int, error) {
  18. if this == nil || this.conn == nil {
  19. return 0, ErrorClosedConnection
  20. }
  21. return this.conn.Write(b)
  22. }
  23. func (this *Connection) Close() error {
  24. if this == nil || this.conn == nil {
  25. return ErrorClosedConnection
  26. }
  27. err := this.conn.Close()
  28. return err
  29. }
  30. func (this *Connection) Release() {
  31. if this == nil || this.listener == nil {
  32. return
  33. }
  34. this.Close()
  35. this.conn = nil
  36. this.listener = nil
  37. }
  38. func (this *Connection) LocalAddr() net.Addr {
  39. return this.conn.LocalAddr()
  40. }
  41. func (this *Connection) RemoteAddr() net.Addr {
  42. return this.conn.RemoteAddr()
  43. }
  44. func (this *Connection) SetDeadline(t time.Time) error {
  45. return this.conn.SetDeadline(t)
  46. }
  47. func (this *Connection) SetReadDeadline(t time.Time) error {
  48. return this.conn.SetReadDeadline(t)
  49. }
  50. func (this *Connection) SetWriteDeadline(t time.Time) error {
  51. return this.conn.SetWriteDeadline(t)
  52. }