本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 satır
3.5KB

  1. package mssql
  2. import (
  3. "fmt"
  4. "net"
  5. "time"
  6. )
  7. type timeoutConn struct {
  8. c net.Conn
  9. timeout time.Duration
  10. }
  11. func newTimeoutConn(conn net.Conn, timeout time.Duration) *timeoutConn {
  12. return &timeoutConn{
  13. c: conn,
  14. timeout: timeout,
  15. }
  16. }
  17. func (c *timeoutConn) Read(b []byte) (n int, err error) {
  18. if c.timeout > 0 {
  19. err = c.c.SetDeadline(time.Now().Add(c.timeout))
  20. if err != nil {
  21. return
  22. }
  23. }
  24. return c.c.Read(b)
  25. }
  26. func (c *timeoutConn) Write(b []byte) (n int, err error) {
  27. if c.timeout > 0 {
  28. err = c.c.SetDeadline(time.Now().Add(c.timeout))
  29. if err != nil {
  30. return
  31. }
  32. }
  33. return c.c.Write(b)
  34. }
  35. func (c timeoutConn) Close() error {
  36. return c.c.Close()
  37. }
  38. func (c timeoutConn) LocalAddr() net.Addr {
  39. return c.c.LocalAddr()
  40. }
  41. func (c timeoutConn) RemoteAddr() net.Addr {
  42. return c.c.RemoteAddr()
  43. }
  44. func (c timeoutConn) SetDeadline(t time.Time) error {
  45. panic("Not implemented")
  46. }
  47. func (c timeoutConn) SetReadDeadline(t time.Time) error {
  48. panic("Not implemented")
  49. }
  50. func (c timeoutConn) SetWriteDeadline(t time.Time) error {
  51. panic("Not implemented")
  52. }
  53. // this connection is used during TLS Handshake
  54. // TDS protocol requires TLS handshake messages to be sent inside TDS packets
  55. type tlsHandshakeConn struct {
  56. buf *tdsBuffer
  57. packetPending bool
  58. continueRead bool
  59. }
  60. func (c *tlsHandshakeConn) Read(b []byte) (n int, err error) {
  61. if c.packetPending {
  62. c.packetPending = false
  63. err = c.buf.FinishPacket()
  64. if err != nil {
  65. err = fmt.Errorf("Cannot send handshake packet: %s", err.Error())
  66. return
  67. }
  68. c.continueRead = false
  69. }
  70. if !c.continueRead {
  71. var packet packetType
  72. packet, err = c.buf.BeginRead()
  73. if err != nil {
  74. err = fmt.Errorf("Cannot read handshake packet: %s", err.Error())
  75. return
  76. }
  77. if packet != packPrelogin {
  78. err = fmt.Errorf("unexpected packet %d, expecting prelogin", packet)
  79. return
  80. }
  81. c.continueRead = true
  82. }
  83. return c.buf.Read(b)
  84. }
  85. func (c *tlsHandshakeConn) Write(b []byte) (n int, err error) {
  86. if !c.packetPending {
  87. c.buf.BeginPacket(packPrelogin, false)
  88. c.packetPending = true
  89. }
  90. return c.buf.Write(b)
  91. }
  92. func (c *tlsHandshakeConn) Close() error {
  93. panic("Not implemented")
  94. }
  95. func (c *tlsHandshakeConn) LocalAddr() net.Addr {
  96. panic("Not implemented")
  97. }
  98. func (c *tlsHandshakeConn) RemoteAddr() net.Addr {
  99. panic("Not implemented")
  100. }
  101. func (c *tlsHandshakeConn) SetDeadline(t time.Time) error {
  102. panic("Not implemented")
  103. }
  104. func (c *tlsHandshakeConn) SetReadDeadline(t time.Time) error {
  105. panic("Not implemented")
  106. }
  107. func (c *tlsHandshakeConn) SetWriteDeadline(t time.Time) error {
  108. panic("Not implemented")
  109. }
  110. // this connection just delegates all methods to it's wrapped connection
  111. // it also allows switching underlying connection on the fly
  112. // it is needed because tls.Conn does not allow switching underlying connection
  113. type passthroughConn struct {
  114. c net.Conn
  115. }
  116. func (c passthroughConn) Read(b []byte) (n int, err error) {
  117. return c.c.Read(b)
  118. }
  119. func (c passthroughConn) Write(b []byte) (n int, err error) {
  120. return c.c.Write(b)
  121. }
  122. func (c passthroughConn) Close() error {
  123. return c.c.Close()
  124. }
  125. func (c passthroughConn) LocalAddr() net.Addr {
  126. panic("Not implemented")
  127. }
  128. func (c passthroughConn) RemoteAddr() net.Addr {
  129. panic("Not implemented")
  130. }
  131. func (c passthroughConn) SetDeadline(t time.Time) error {
  132. panic("Not implemented")
  133. }
  134. func (c passthroughConn) SetReadDeadline(t time.Time) error {
  135. panic("Not implemented")
  136. }
  137. func (c passthroughConn) SetWriteDeadline(t time.Time) error {
  138. panic("Not implemented")
  139. }
上海开阖软件有限公司 沪ICP备12045867号-1