/vlib/x/net/tcp.v

https://github.com/vlang/v · V · 356 lines · 267 code · 71 blank · 18 comment · 24 complexity · 1c7ab8a269a6dda28adbb6b41069f9ed MD5 · raw file

  1. module net
  2. import time
  3. pub struct TcpConn {
  4. pub:
  5. sock TcpSocket
  6. mut:
  7. write_deadline time.Time
  8. read_deadline time.Time
  9. read_timeout time.Duration
  10. write_timeout time.Duration
  11. }
  12. pub fn dial_tcp(address string) ?TcpConn {
  13. s := new_tcp_socket()?
  14. s.connect(address)?
  15. return TcpConn {
  16. sock: s
  17. read_timeout: -1
  18. write_timeout: -1
  19. }
  20. }
  21. pub fn (c TcpConn) close() ? {
  22. c.sock.close()?
  23. return none
  24. }
  25. // write_ptr blocks and attempts to write all data
  26. pub fn (c TcpConn) write_ptr(b byteptr, len int) ? {
  27. unsafe {
  28. mut ptr_base := byteptr(b)
  29. mut total_sent := 0
  30. for total_sent < len {
  31. ptr := ptr_base + total_sent
  32. remaining := len - total_sent
  33. mut sent := C.send(c.sock.handle, ptr, remaining, msg_nosignal)
  34. if sent < 0 {
  35. code := error_code()
  36. match code {
  37. error_ewouldblock {
  38. c.wait_for_write()
  39. continue
  40. }
  41. else {
  42. wrap_error(code)?
  43. }
  44. }
  45. }
  46. total_sent += sent
  47. }
  48. }
  49. return none
  50. }
  51. // write blocks and attempts to write all data
  52. pub fn (c TcpConn) write(bytes []byte) ? {
  53. return c.write_ptr(bytes.data, bytes.len)
  54. }
  55. // write_string blocks and attempts to write all data
  56. pub fn (c TcpConn) write_string(s string) ? {
  57. return c.write_ptr(s.str, s.len)
  58. }
  59. pub fn (c TcpConn) read_into_ptr(buf_ptr byteptr, len int) ?int {
  60. res := C.recv(c.sock.handle, buf_ptr, len, 0)
  61. if res >= 0 {
  62. return res
  63. }
  64. code := error_code()
  65. match code {
  66. error_ewouldblock {
  67. c.wait_for_read()?
  68. return socket_error(C.recv(c.sock.handle, buf_ptr, len, 0))
  69. }
  70. else {
  71. wrap_error(code)?
  72. }
  73. }
  74. }
  75. pub fn (c TcpConn) read_into(mut buf []byte) ?int {
  76. res := C.recv(c.sock.handle, buf.data, buf.len, 0)
  77. if res >= 0 {
  78. return res
  79. }
  80. code := error_code()
  81. match code {
  82. error_ewouldblock {
  83. c.wait_for_read()?
  84. return socket_error(C.recv(c.sock.handle, buf.data, buf.len, 0))
  85. }
  86. else {
  87. wrap_error(code)?
  88. }
  89. }
  90. }
  91. pub fn (c TcpConn) read() ?[]byte {
  92. mut buf := []byte { len: 1024 }
  93. read := c.read_into(mut buf)?
  94. return buf[..read]
  95. }
  96. pub fn (c TcpConn) read_deadline() ?time.Time {
  97. if c.read_deadline.unix == 0 {
  98. return c.read_deadline
  99. }
  100. return none
  101. }
  102. pub fn (mut c TcpConn) set_read_deadline(deadline time.Time) {
  103. c.read_deadline = deadline
  104. }
  105. pub fn (c TcpConn) write_deadline() ?time.Time {
  106. if c.write_deadline.unix == 0 {
  107. return c.write_deadline
  108. }
  109. return none
  110. }
  111. pub fn (mut c TcpConn) set_write_deadline(deadline time.Time) {
  112. c.write_deadline = deadline
  113. }
  114. pub fn (c TcpConn) read_timeout() time.Duration {
  115. return c.read_timeout
  116. }
  117. pub fn(mut c TcpConn) set_read_timeout(t time.Duration) {
  118. c.read_timeout = t
  119. }
  120. pub fn (c TcpConn) write_timeout() time.Duration {
  121. return c.write_timeout
  122. }
  123. pub fn (mut c TcpConn) set_write_timeout(t time.Duration) {
  124. c.write_timeout = t
  125. }
  126. [inline]
  127. pub fn (c TcpConn) wait_for_read() ? {
  128. return wait_for_read(c.sock.handle, c.read_deadline, c.read_timeout)
  129. }
  130. [inline]
  131. pub fn (c TcpConn) wait_for_write() ? {
  132. return wait_for_write(c.sock.handle, c.write_deadline, c.write_timeout)
  133. }
  134. pub fn (c TcpConn) str() string {
  135. // TODO
  136. return 'TcpConn'
  137. }
  138. pub struct TcpListener {
  139. sock TcpSocket
  140. mut:
  141. accept_timeout time.Duration
  142. accept_deadline time.Time
  143. }
  144. pub fn listen_tcp(port int) ?TcpListener {
  145. s := new_tcp_socket()?
  146. validate_port(port)?
  147. mut addr := C.sockaddr_in{}
  148. addr.sin_family = SocketFamily.inet
  149. addr.sin_port = C.htons(port)
  150. addr.sin_addr.s_addr = C.htonl(C.INADDR_ANY)
  151. size := sizeof(C.sockaddr_in)
  152. // cast to the correct type
  153. sockaddr := &C.sockaddr(&addr)
  154. socket_error(C.bind(s.handle, sockaddr, size))?
  155. socket_error(C.listen(s.handle, 128))?
  156. return TcpListener {
  157. sock: s
  158. accept_timeout: -1
  159. accept_deadline: no_deadline
  160. }
  161. }
  162. pub fn (l TcpListener) accept() ?TcpConn {
  163. addr := C.sockaddr_storage{}
  164. unsafe {
  165. C.memset(&addr, 0, sizeof(C.sockaddr_storage))
  166. }
  167. size := sizeof(C.sockaddr_storage)
  168. // cast to correct type
  169. sock_addr := &C.sockaddr(&addr)
  170. mut new_handle := C.accept(l.sock.handle, sock_addr, &size)
  171. if new_handle <= 0 {
  172. l.wait_for_accept()?
  173. new_handle = C.accept(l.sock.handle, sock_addr, &size)
  174. if new_handle == -1 || new_handle == 0 {
  175. return none
  176. }
  177. }
  178. new_sock := TcpSocket {
  179. handle: new_handle
  180. }
  181. return TcpConn{
  182. sock: new_sock
  183. read_timeout: -1
  184. write_timeout: -1
  185. }
  186. }
  187. pub fn (c TcpListener) accept_deadline() ?time.Time {
  188. if c.accept_deadline.unix != 0 {
  189. return c.accept_deadline
  190. }
  191. return none
  192. }
  193. pub fn (mut c TcpListener) set_accept_deadline(deadline time.Time) {
  194. c.accept_deadline = deadline
  195. }
  196. pub fn (c TcpListener) accept_timeout() time.Duration {
  197. return c.accept_timeout
  198. }
  199. pub fn(mut c TcpListener) set_accept_timeout(t time.Duration) {
  200. c.accept_timeout = t
  201. }
  202. pub fn (c TcpListener) wait_for_accept() ? {
  203. return wait_for_read(c.sock.handle, c.accept_deadline, c.accept_timeout)
  204. }
  205. pub fn (c TcpListener) close() ? {
  206. c.sock.close()?
  207. return none
  208. }
  209. pub fn (c TcpListener) address() ?Addr {
  210. return c.sock.address()
  211. }
  212. struct TcpSocket {
  213. pub:
  214. handle int
  215. }
  216. fn new_tcp_socket() ?TcpSocket {
  217. sockfd := socket_error(C.socket(SocketFamily.inet, SocketType.tcp, 0))?
  218. s := TcpSocket {
  219. handle: sockfd
  220. }
  221. //s.set_option_bool(.reuse_addr, true)?
  222. s.set_option_int(.reuse_addr, 1)?
  223. $if windows {
  224. t := true
  225. socket_error(C.ioctlsocket(sockfd, fionbio, &t))?
  226. } $else {
  227. socket_error(C.fcntl(sockfd, C.F_SETFD, C.O_NONBLOCK))
  228. }
  229. return s
  230. }
  231. pub fn (s TcpSocket) set_option_bool(opt SocketOption, value bool) ? {
  232. // TODO reenable when this `in` operation works again
  233. // if opt !in opts_can_set {
  234. // return err_option_not_settable
  235. // }
  236. // if opt !in opts_bool {
  237. // return err_option_wrong_type
  238. // }
  239. socket_error(C.setsockopt(s.handle, C.SOL_SOCKET, int(opt), &value, sizeof(bool)))?
  240. return none
  241. }
  242. pub fn (s TcpSocket) set_option_int(opt SocketOption, value int) ? {
  243. socket_error(C.setsockopt(s.handle, C.SOL_SOCKET, int(opt), &value, sizeof(int)))?
  244. return none
  245. }
  246. fn (s TcpSocket) close() ? {
  247. return shutdown(s.handle)
  248. }
  249. fn (s TcpSocket) @select(test Select, timeout time.Duration) ?bool {
  250. return @select(s.handle, test, timeout)
  251. }
  252. const (
  253. connect_timeout = 5 * time.second
  254. )
  255. fn (s TcpSocket) connect(a string) ? {
  256. addr := resolve_addr(a, .inet, .tcp)?
  257. res := C.connect(s.handle, &addr.addr, addr.len)
  258. if res == 0 {
  259. return none
  260. }
  261. errcode := error_code()
  262. if errcode == error_ewouldblock {
  263. write_result := s.@select(.write, connect_timeout)?
  264. if write_result {
  265. // succeeded
  266. return none
  267. }
  268. except_result := s.@select(.except, connect_timeout)?
  269. if except_result {
  270. return err_connect_failed
  271. }
  272. // otherwise we timed out
  273. return err_connect_timed_out
  274. }
  275. return wrap_error(errcode)
  276. }
  277. // address gets the address of a socket
  278. pub fn (s TcpSocket) address() ?Addr {
  279. mut addr := C.sockaddr_in{}
  280. size := sizeof(C.sockaddr_in)
  281. // cast to the correct type
  282. sockaddr := &C.sockaddr(&addr)
  283. C.getsockname(s.handle, sockaddr, &size)
  284. return new_addr(sockaddr, '', 0)
  285. }