socket.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package thrift
  20. import (
  21. "net"
  22. "time"
  23. )
  24. type TSocket struct {
  25. conn net.Conn
  26. addr net.Addr
  27. timeout time.Duration
  28. }
  29. // NewTSocket creates a net.Conn-backed TTransport, given a host and port
  30. //
  31. // Example:
  32. // trans, err := thrift.NewTSocket("localhost:9090")
  33. func NewTSocket(hostPort string) (*TSocket, error) {
  34. return NewTSocketTimeout(hostPort, 0)
  35. }
  36. // NewTSocketTimeout creates a net.Conn-backed TTransport, given a host and port
  37. // it also accepts a timeout as a time.Duration
  38. func NewTSocketTimeout(hostPort string, timeout time.Duration) (*TSocket, error) {
  39. //conn, err := net.DialTimeout(network, address, timeout)
  40. addr, err := net.ResolveTCPAddr("tcp", hostPort)
  41. if err != nil {
  42. return nil, err
  43. }
  44. return NewTSocketFromAddrTimeout(addr, timeout), nil
  45. }
  46. // Creates a TSocket from a net.Addr
  47. func NewTSocketFromAddrTimeout(addr net.Addr, timeout time.Duration) *TSocket {
  48. return &TSocket{addr: addr, timeout: timeout}
  49. }
  50. // Creates a TSocket from an existing net.Conn
  51. func NewTSocketFromConnTimeout(conn net.Conn, timeout time.Duration) *TSocket {
  52. return &TSocket{conn: conn, addr: conn.RemoteAddr(), timeout: timeout}
  53. }
  54. // Sets the socket timeout
  55. func (p *TSocket) SetTimeout(timeout time.Duration) error {
  56. p.timeout = timeout
  57. return nil
  58. }
  59. func (p *TSocket) pushDeadline(read, write bool) {
  60. var t time.Time
  61. if p.timeout > 0 {
  62. t = time.Now().Add(time.Duration(p.timeout))
  63. }
  64. if read && write {
  65. p.conn.SetDeadline(t)
  66. } else if read {
  67. p.conn.SetReadDeadline(t)
  68. } else if write {
  69. p.conn.SetWriteDeadline(t)
  70. }
  71. }
  72. // Connects the socket, creating a new socket object if necessary.
  73. func (p *TSocket) Open() error {
  74. if p.IsOpen() {
  75. return NewTTransportException(ALREADY_OPEN, "Socket already connected.")
  76. }
  77. if p.addr == nil {
  78. return NewTTransportException(NOT_OPEN, "Cannot open nil address.")
  79. }
  80. if len(p.addr.Network()) == 0 {
  81. return NewTTransportException(NOT_OPEN, "Cannot open bad network name.")
  82. }
  83. if len(p.addr.String()) == 0 {
  84. return NewTTransportException(NOT_OPEN, "Cannot open bad address.")
  85. }
  86. var err error
  87. if p.conn, err = net.DialTimeout(p.addr.Network(), p.addr.String(), p.timeout); err != nil {
  88. return NewTTransportException(NOT_OPEN, err.Error())
  89. }
  90. return nil
  91. }
  92. // Retrieve the underlying net.Conn
  93. func (p *TSocket) Conn() net.Conn {
  94. return p.conn
  95. }
  96. // Returns true if the connection is open
  97. func (p *TSocket) IsOpen() bool {
  98. if p.conn == nil {
  99. return false
  100. }
  101. return true
  102. }
  103. // Closes the socket.
  104. func (p *TSocket) Close() error {
  105. // Close the socket
  106. if p.conn != nil {
  107. err := p.conn.Close()
  108. if err != nil {
  109. return err
  110. }
  111. p.conn = nil
  112. }
  113. return nil
  114. }
  115. //Returns the remote address of the socket.
  116. func (p *TSocket) Addr() net.Addr {
  117. return p.addr
  118. }
  119. func (p *TSocket) Read(buf []byte) (int, error) {
  120. if !p.IsOpen() {
  121. return 0, NewTTransportException(NOT_OPEN, "Connection not open")
  122. }
  123. p.pushDeadline(true, false)
  124. n, err := p.conn.Read(buf)
  125. return n, NewTTransportExceptionFromError(err)
  126. }
  127. func (p *TSocket) Write(buf []byte) (int, error) {
  128. if !p.IsOpen() {
  129. return 0, NewTTransportException(NOT_OPEN, "Connection not open")
  130. }
  131. p.pushDeadline(false, true)
  132. return p.conn.Write(buf)
  133. }
  134. func (p *TSocket) Flush() error {
  135. return nil
  136. }
  137. func (p *TSocket) Interrupt() error {
  138. if !p.IsOpen() {
  139. return nil
  140. }
  141. return p.conn.Close()
  142. }
  143. func (p *TSocket) RemainingBytes() (num_bytes uint64) {
  144. const maxSize = ^uint64(0)
  145. return maxSize // the thruth is, we just don't know unless framed is used
  146. }