socketoption.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package socket
  2. import (
  3. "github.com/gorilla/websocket"
  4. "math"
  5. "net"
  6. "time"
  7. )
  8. type SocketOption interface {
  9. MaxMsgLen() int
  10. SocketReadTimeout(c net.Conn, callback func())
  11. SocketWriteTimeout(c net.Conn, callback func())
  12. CopyOpt(opt *NetTCPSocketOption)
  13. GetSocketDeadline() (time.Duration, time.Duration)
  14. }
  15. type NetTCPSocketOption struct {
  16. readBufferSize int
  17. writeBufferSize int
  18. readTimeout time.Duration
  19. writeTimeout time.Duration
  20. noDelay bool
  21. maxMsgLen int
  22. }
  23. func (this *NetTCPSocketOption) Init() {
  24. //todo...
  25. this.maxMsgLen = 1024 * 4 * 10 //40k(发送和接受字节最大数量)
  26. }
  27. func (this *NetTCPSocketOption) WriteTimeout() time.Duration {
  28. return this.writeTimeout
  29. }
  30. func (this *NetTCPSocketOption) ReadTimeout() time.Duration {
  31. return this.readTimeout
  32. }
  33. func (op *NetTCPSocketOption) SocketOpt(c net.Conn) {
  34. if conn, ok := c.(*net.TCPConn); ok {
  35. conn.SetNoDelay(op.noDelay)
  36. conn.SetReadBuffer(op.readBufferSize)
  37. conn.SetWriteBuffer(op.writeBufferSize)
  38. //reuse addr
  39. //todo...
  40. }
  41. }
  42. func (op *NetTCPSocketOption) SocketOptWebSocket(c *websocket.Conn) {
  43. if conn, ok := c.UnderlyingConn().(*net.TCPConn); ok {
  44. conn.SetNoDelay(op.noDelay)
  45. conn.SetReadBuffer(op.readBufferSize)
  46. conn.SetWriteBuffer(op.writeBufferSize)
  47. //reuse addr
  48. //todo...
  49. }
  50. }
  51. func (op *NetTCPSocketOption) MaxMsgLen() int {
  52. return op.maxMsgLen
  53. }
  54. func (op *NetTCPSocketOption) SetMaxMsgLen(size int) {
  55. op.maxMsgLen = size
  56. }
  57. //http://blog.sina.com.cn/s/blog_9be3b8f10101lhiq.html
  58. func (op *NetTCPSocketOption) SocketReadTimeout(c net.Conn, callback func()) {
  59. if op.readTimeout > 0 {
  60. c.SetReadDeadline(time.Now().Add(op.readTimeout))
  61. callback()
  62. c.SetReadDeadline(time.Time{})
  63. } else {
  64. callback()
  65. }
  66. }
  67. func (op *NetTCPSocketOption) SocketWriteTimeout(c net.Conn, callback func()) {
  68. if op.writeTimeout > 0 {
  69. c.SetWriteDeadline(time.Now().Add(op.writeTimeout))
  70. callback()
  71. c.SetWriteDeadline(time.Time{})
  72. } else {
  73. callback()
  74. }
  75. }
  76. func (op *NetTCPSocketOption) SetSocketBuff(read, write int, noDelay bool) {
  77. op.readBufferSize = read
  78. op.writeBufferSize = write
  79. op.noDelay = noDelay
  80. if read > 0 {
  81. op.maxMsgLen = read
  82. }
  83. if op.maxMsgLen >= math.MaxUint16 {
  84. op.maxMsgLen = math.MaxUint16
  85. }
  86. }
  87. func (op *NetTCPSocketOption) SetSocketDeadline(read, write time.Duration) {
  88. op.readTimeout = read
  89. op.writeTimeout = write
  90. }
  91. func (op *NetTCPSocketOption) GetSocketDeadline() (time.Duration, time.Duration) {
  92. return op.readTimeout, op.writeTimeout
  93. }
  94. func (op *NetTCPSocketOption) CopyOpt(opt *NetTCPSocketOption) {
  95. //opt.writeTimeout = op.writeTimeout
  96. //opt.readTimeout = op.readTimeout
  97. opt.maxMsgLen = op.maxMsgLen
  98. opt.noDelay = op.noDelay
  99. opt.readBufferSize = op.readBufferSize
  100. opt.writeBufferSize = op.writeBufferSize
  101. }