v2 / vlib / net / tcp.c.v
738 lines · 657 sloc · 21.39 KB · 9e1d3ca05db333f60c4201e1d35db705c319d4dd
Raw
1module net
2
3import io
4import time
5import strings
6
7pub const tcp_default_read_timeout = 30 * time.second
8pub const tcp_default_write_timeout = 30 * time.second
9
10// TCPDialer is a concrete instance of the Dialer interface,
11// for creating tcp connections.
12pub struct TCPDialer {}
13
14// dial will try to create a new abstract connection to the given address.
15// It will return an error, if that is not possible.
16pub fn (t TCPDialer) dial(address string) !Connection {
17 return dial_tcp(address)!
18}
19
20// default_tcp_dialer will give you an instance of Dialer, that is suitable for making new tcp connections.
21pub fn default_tcp_dialer() Dialer {
22 return &TCPDialer{}
23}
24
25@[heap]
26pub struct TcpConn {
27pub mut:
28 sock TcpSocket
29 handle int
30 write_deadline time.Time
31 read_deadline time.Time
32 read_timeout time.Duration
33 write_timeout time.Duration
34 is_blocking bool = true
35}
36
37// dial_tcp will try to create a new TcpConn to the given address.
38pub fn dial_tcp(oaddress string) !&TcpConn {
39 mut address := oaddress
40 $if windows {
41 // resolving 0.0.0.0 to localhost, works on linux and macos, but not on windows, so try to emulate it:
42 if address.starts_with(':::') {
43 address = address.replace_once(':::', 'localhost:')
44 }
45 if address.starts_with('0.0.0.0:') {
46 address = address.replace_once('0.0.0.0:', 'localhost:')
47 }
48 }
49 addrs := resolve_addrs_fuzzy(address, .tcp) or {
50 return error('${err.msg()}; could not resolve address ${address} in dial_tcp')
51 }
52
53 // Keep track of dialing errors that take place
54 mut errs := []IError{}
55
56 // Very simple dialer
57 for addr in addrs {
58 mut s := new_tcp_socket(addr.family()) or {
59 return error('${err.msg()}; could not create new tcp socket in dial_tcp')
60 }
61 s.connect(addr) or {
62 errs << err
63 // Connection failed
64 s.close() or { continue }
65 continue
66 }
67
68 mut conn := &TcpConn{
69 sock: s
70 read_timeout: tcp_default_read_timeout
71 write_timeout: tcp_default_write_timeout
72 }
73 // The blocking / non-blocking mode is determined before the connection is established.
74 $if net_nonblocking_sockets ? {
75 conn.is_blocking = false
76 }
77 return conn
78 }
79
80 // Once we've failed now try and explain why we failed to connect
81 // to any of these addresses
82 mut err_builder := strings.new_builder(1024)
83 err_builder.write_string('dial_tcp failed for address ${address}\n')
84 err_builder.write_string('tried addrs:\n')
85 for i := 0; i < errs.len; i++ {
86 addr := addrs[i]
87 why := errs[i]
88 err_builder.write_string('\t${addr}: ${why}\n')
89 }
90
91 // failed
92 return error(err_builder.str())
93}
94
95// dial_tcp_with_bind will bind the given local address `laddr` and dial.
96pub fn dial_tcp_with_bind(saddr string, laddr string) !&TcpConn {
97 addrs := resolve_addrs_fuzzy(saddr, .tcp) or {
98 return error('${err.msg()}; could not resolve address ${saddr} in dial_tcp_with_bind')
99 }
100
101 // Very simple dialer
102 for addr in addrs {
103 mut s := new_tcp_socket(addr.family()) or {
104 return error('${err.msg()}; could not create new tcp socket in dial_tcp_with_bind')
105 }
106 s.bind(laddr) or {
107 s.close() or { continue }
108 continue
109 }
110 s.connect(addr) or {
111 // Connection failed
112 s.close() or { continue }
113 continue
114 }
115
116 mut conn := &TcpConn{
117 sock: s
118 read_timeout: tcp_default_read_timeout
119 write_timeout: tcp_default_write_timeout
120 }
121 // The blocking / non-blocking mode is determined before the connection is established.
122 $if net_nonblocking_sockets ? {
123 conn.is_blocking = false
124 }
125 return conn
126 }
127 // failed
128 return error('dial_tcp_with_bind failed for address ${saddr}')
129}
130
131// close closes the tcp connection
132pub fn (mut c TcpConn) close() ! {
133 $if trace_tcp ? {
134 eprintln(' TcpConn.close | c.sock.handle: ${c.sock.handle:6}')
135 }
136 c.sock.close()!
137}
138
139// read_ptr reads data from the tcp connection to the given buffer. It reads at most `len` bytes.
140// It returns the number of actually read bytes, which can vary between 0 to `len`.
141pub fn (c TcpConn) read_ptr(buf_ptr &u8, len int) !int {
142 mut res := 0
143 mut ecode := 0
144 $if is_coroutine ? {
145 res = C.photon_recv(c.sock.handle, voidptr(buf_ptr), len, 0, c.read_timeout)
146 ecode = error_code()
147 } $else {
148 if c.is_blocking {
149 // Honor read deadlines/timeouts first, then use a normal blocking recv.
150 // This avoids transient EAGAIN-style reads on newly accepted sockets.
151 c.wait_for_read()!
152 res = C.recv(c.sock.handle, voidptr(buf_ptr), len, 0)
153 } else {
154 res = C.recv(c.sock.handle, voidptr(buf_ptr), len, msg_dontwait)
155 }
156 ecode = error_code()
157 }
158 if res == 0 {
159 return io.Eof{}
160 }
161 if res > 0 {
162 $if trace_tcp ? {
163 eprintln(
164 '<<< TcpConn.read_ptr | c.sock.handle: ${c.sock.handle} | buf_ptr: ${ptr_str(buf_ptr)} | len: ${len} | res: ${res} |\n' +
165 unsafe { buf_ptr.vstring_with_len(len) })
166 }
167 $if trace_tcp_data_read ? {
168 eprintln(
169 '<<< TcpConn.read_ptr | 1 data.len: ${res:6} | hex: ${unsafe { buf_ptr.vbytes(res) }.hex()} | data: ' +
170 unsafe { buf_ptr.vstring_with_len(res) })
171 }
172 return res
173 }
174 if ecode in [int(error_ewouldblock), int(error_eagain), C.EINTR] {
175 c.wait_for_read()!
176 res = $if is_coroutine ? {
177 C.photon_recv(c.sock.handle, voidptr(buf_ptr), len, 0, c.read_timeout)
178 } $else {
179 if c.is_blocking {
180 C.recv(c.sock.handle, voidptr(buf_ptr), len, 0)
181 } else {
182 C.recv(c.sock.handle, voidptr(buf_ptr), len, msg_dontwait)
183 }
184 }
185 if res == 0 {
186 return io.Eof{}
187 }
188 $if trace_tcp ? {
189 eprintln(
190 '<<< TcpConn.read_ptr | c.sock.handle: ${c.sock.handle} | buf_ptr: ${ptr_str(buf_ptr)} | len: ${len} | res: ${res} | code: ${ecode} |\n' +
191 unsafe { buf_ptr.vstring_with_len(len) })
192 }
193 $if trace_tcp_data_read ? {
194 if res > 0 {
195 eprintln(
196 '<<< TcpConn.read_ptr | 2 data.len: ${res:6} | hex: ${unsafe { buf_ptr.vbytes(res) }.hex()} | data: ' +
197 unsafe { buf_ptr.vstring_with_len(res) })
198 }
199 }
200 return socket_error(res)
201 } else {
202 wrap_error(ecode)!
203 }
204 return error('none')
205}
206
207// read reads data from the tcp connection into the mutable buffer `buf`.
208// The number of bytes read is limited to the length of the buffer `buf.len`.
209// The returned value is the number of read bytes (between 0 and `buf.len`).
210pub fn (c TcpConn) read(mut buf []u8) !int {
211 return c.read_ptr(buf.data, buf.len)!
212}
213
214pub fn (mut c TcpConn) read_deadline() !time.Time {
215 if c.read_deadline.unix() == 0 {
216 return c.read_deadline
217 }
218 return error('none')
219}
220
221// write_ptr blocks and attempts to write all data
222pub fn (mut c TcpConn) write_ptr(b &u8, len int) !int {
223 $if trace_tcp_sock_handle ? {
224 eprintln('>>> TcpConn.write_ptr | c: ${ptr_str(c)} | c.sock.handle: ${c.sock.handle} | b: ${ptr_str(b)} | len: ${len}')
225 }
226 $if trace_tcp ? {
227 eprintln(
228 '>>> TcpConn.write_ptr | c.sock.handle: ${c.sock.handle} | b: ${ptr_str(b)} len: ${len} |\n' +
229 unsafe { b.vstring_with_len(len) })
230 }
231 $if trace_tcp_data_write ? {
232 eprintln(
233 '>>> TcpConn.write_ptr | data.len: ${len:6} | hex: ${unsafe { b.vbytes(len) }.hex()} | data: ' +
234 unsafe { b.vstring_with_len(len) })
235 }
236 unsafe {
237 mut ptr_base := &u8(b)
238 mut total_sent := 0
239 for total_sent < len {
240 ptr := ptr_base + total_sent
241 remaining := len - total_sent
242 mut sent := $if is_coroutine ? {
243 C.photon_send(c.sock.handle, ptr, remaining, msg_nosignal, c.write_timeout)
244 } $else {
245 C.send(c.sock.handle, ptr, remaining, msg_nosignal)
246 }
247 code := error_code()
248 $if trace_tcp_data_write ? {
249 eprintln('>>> TcpConn.write_ptr | data chunk, total_sent: ${total_sent:6}, remaining: ${remaining:6}, ptr: ${voidptr(ptr):x} => sent: ${sent:6}')
250 }
251 if sent < 0 {
252 $if trace_tcp_send_failures ? {
253 eprintln('>>> TcpConn.write_ptr | send_failure, data.len: ${len:6}, total_sent: ${total_sent:6}, remaining: ${remaining:6}, ptr: ${voidptr(ptr):x}, c.write_timeout: ${c.write_timeout:3} => sent: ${sent:6}, error code: ${code:3}')
254 }
255 if code in [int(error_ewouldblock), int(error_eagain), C.EINTR] {
256 c.wait_for_write()!
257 continue
258 } else {
259 wrap_error(code)!
260 }
261 }
262 total_sent += sent
263 }
264 return total_sent
265 }
266}
267
268// write blocks and attempts to write all data
269pub fn (mut c TcpConn) write(bytes []u8) !int {
270 return c.write_ptr(bytes.data, bytes.len)
271}
272
273// write_string blocks and attempts to write all data
274pub fn (mut c TcpConn) write_string(s string) !int {
275 return c.write_ptr(s.str, s.len)
276}
277
278pub fn (mut c TcpConn) set_read_deadline(deadline time.Time) {
279 c.read_deadline = deadline
280}
281
282pub fn (mut c TcpConn) write_deadline() !time.Time {
283 if c.write_deadline.unix() == 0 {
284 return c.write_deadline
285 }
286 return error('none')
287}
288
289pub fn (mut c TcpConn) set_write_deadline(deadline time.Time) {
290 c.write_deadline = deadline
291}
292
293pub fn (c &TcpConn) read_timeout() time.Duration {
294 return c.read_timeout
295}
296
297pub fn (mut c TcpConn) set_read_timeout(t time.Duration) {
298 c.read_timeout = t
299}
300
301pub fn (c &TcpConn) write_timeout() time.Duration {
302 return c.write_timeout
303}
304
305pub fn (mut c TcpConn) set_write_timeout(t time.Duration) {
306 c.write_timeout = t
307}
308
309@[inline]
310pub fn (c TcpConn) wait_for_read() ! {
311 return wait_for_read(c.sock.handle, c.read_deadline, c.read_timeout)
312}
313
314@[inline]
315pub fn (mut c TcpConn) wait_for_write() ! {
316 return wait_for_write(c.sock.handle, c.write_deadline, c.write_timeout)
317}
318
319// set_sock initialises the c.sock field. It should be called after `.accept_only()!`.
320// Note: just use `.accept()!`. In most cases it is simpler, and calls `.set_sock()!` for you.
321pub fn (mut c TcpConn) set_sock() ! {
322 c.sock = tcp_socket_from_handle(c.handle)!
323 $if trace_tcp ? {
324 eprintln(' TcpListener.accept | << new_sock.handle: ${c.handle:6}')
325 }
326}
327
328// peer_addr retrieves the ip address and port number used by the peer
329pub fn (c &TcpConn) peer_addr() !Addr {
330 return peer_addr_from_socket_handle(c.sock.handle)
331}
332
333// peer_ip retrieves the ip address used by the peer, and returns it as a string
334pub fn (c &TcpConn) peer_ip() !string {
335 address := c.peer_addr()!.str()
336 if address.contains(']:') {
337 // ipv6 addresses similar to this: '[::1]:46098'
338 ip := address.all_before(']:').all_after('[')
339 return ip
340 }
341 // ipv4 addresses similar to '127.0.0.1:7346'
342 ip := address.all_before(':')
343 return ip
344}
345
346pub fn (c &TcpConn) addr() !Addr {
347 return c.sock.address()
348}
349
350pub fn (c TcpConn) str() string {
351 s := c.sock.str().replace('\n', ' ').replace(' ', ' ')
352 return 'TcpConn{ write_deadline: ${c.write_deadline}, read_deadline: ${c.read_deadline}, read_timeout: ${c.read_timeout}, write_timeout: ${c.write_timeout}, sock: ${s} }'
353}
354
355pub struct TcpListener {
356pub mut:
357 sock TcpSocket
358 accept_timeout time.Duration
359 accept_deadline time.Time
360 is_blocking bool = true
361}
362
363@[params]
364pub struct ListenOptions {
365pub:
366 dualstack bool = true
367 backlog int = 128
368}
369
370pub fn listen_tcp(family AddrFamily, saddr string, options ListenOptions) !&TcpListener {
371 if family !in [.ip, .ip6] {
372 return error('listen_tcp only supports ip and ip6')
373 }
374 return listen_tcp_with_family(family, saddr, options) or {
375 if should_fallback_to_ipv4_listener(family, saddr, options, err.code()) {
376 fallback_saddr := ipv4_fallback_listen_addr(saddr) or { return err }
377 return listen_tcp_with_family(.ip, fallback_saddr, options)
378 }
379 return err
380 }
381}
382
383fn should_fallback_to_ipv4_listener(family AddrFamily, saddr string, options ListenOptions, err_code int) bool {
384 // Treat an unspecified IPv6 listener as "dual stack if available, otherwise IPv4 only".
385 if family != .ip6 || !options.dualstack {
386 return false
387 }
388 if !is_unspecified_ip6_listen_addr(saddr) {
389 return false
390 }
391 return is_ipv6_unavailable_error(err_code)
392}
393
394fn is_unspecified_ip6_listen_addr(saddr string) bool {
395 address, _ := split_address(saddr) or { return false }
396 return address in ['', '::']
397}
398
399fn is_ipv6_unavailable_error(err_code int) bool {
400 $if windows {
401 return err_code in [int(WsaError.wsaeafnosupport), int(WsaError.wsaeprotonosupport),
402 int(WsaError.wsaeaddrnotavail)]
403 } $else {
404 return err_code in [C.EAFNOSUPPORT, C.EPROTONOSUPPORT, C.EADDRNOTAVAIL]
405 }
406}
407
408fn ipv4_fallback_listen_addr(saddr string) !string {
409 _, port := split_address(saddr)!
410 return ':${port}'
411}
412
413fn listen_tcp_with_family(family AddrFamily, saddr string, options ListenOptions) !&TcpListener {
414 mut s := new_tcp_socket(family) or { return error('${err.msg()}; could not create new socket') }
415 s.set_dualstack(options.dualstack) or {}
416
417 addrs := resolve_addrs(saddr, family, .tcp) or {
418 return error('${err.msg()}; could not resolve address ${saddr}')
419 }
420 // TODO(logic to pick here)
421 addr := addrs[0]
422
423 // cast to the correct type
424 alen := addr.len()
425 socket_error_message(C.bind(s.handle, voidptr(&addr), alen), 'binding to ${saddr} failed')!
426 mut res := C.listen(s.handle, options.backlog)
427 if res == 0 {
428 mut listener := &TcpListener{
429 sock: s
430 accept_deadline: no_deadline
431 accept_timeout: infinite_timeout
432 }
433 // The blocking / non-blocking mode is determined before the connection is established.
434 $if net_nonblocking_sockets ? {
435 listener.is_blocking = false
436 }
437 return listener
438 }
439
440 $if !net_nonblocking_sockets ? {
441 socket_error_message(res,
442 'listening on ${saddr} with maximum backlog pending queue of ${options.backlog}, failed')!
443 return &TcpListener(unsafe { nil }) // for compiler passed
444 } $else {
445 // non-blocking sockets may also not succeed immediately when they listen() and need to check the status and take action accordingly.
446 for {
447 code := error_code()
448 if code in [int(error_einprogress), int(error_ewouldblock), int(error_eagain), C.EINTR] {
449 select(s.handle, .read, connect_timeout)!
450 res = C.listen(s.handle, options.backlog)
451 if res == 0 {
452 break
453 }
454 } else {
455 socket_error_message(res,
456 'listening on ${saddr} with maximum backlog pending queue of ${options.backlog}, failed')!
457 break // for compiler passed
458 }
459 }
460 mut listener := &TcpListener{
461 sock: s
462 accept_deadline: no_deadline
463 accept_timeout: infinite_timeout
464 }
465 // The blocking / non-blocking mode is determined before the connection is established.
466 $if net_nonblocking_sockets ? {
467 listener.is_blocking = false
468 }
469 return listener
470 }
471}
472
473// accept a tcp connection from an external source to the listener `l`.
474pub fn (mut l TcpListener) accept() !&TcpConn {
475 mut res := l.accept_only()!
476 res.set_sock()!
477 return res
478}
479
480// accept_only accepts a tcp connection from an external source to the listener `l`.
481// Unlike `accept`, `accept_only` *will not call* `.set_sock()!` on the result,
482// and is thus faster.
483//
484// Note: you *need* to call `.set_sock()!` manually, before using the
485// connection after calling `.accept_only()!`, but that does not have to happen
486// in the same thread that called `.accept_only()!`.
487// The intention of this API, is to have a more efficient way to accept
488// connections, that are later processed by a thread pool, while the main
489// thread remains active, so that it can accept other connections.
490// See also vlib/veb/veb.v .
491//
492// If you do not need that, just call `.accept()!` instead, which will call
493// `.set_sock()!` for you.
494pub fn (mut l TcpListener) accept_only() !&TcpConn {
495 $if trace_tcp ? {
496 eprintln(' TcpListener.accept | l.sock.handle: ${l.sock.handle:6}')
497 }
498
499 // The blocking mode `accept()` does not support a timeout option, so `select` is used instead.
500 $if !is_coroutine ? {
501 if l.is_blocking {
502 l.wait_for_accept()!
503 }
504 }
505
506 mut new_handle := $if is_coroutine ? {
507 C.photon_accept(l.sock.handle, 0, 0, tcp_default_read_timeout)
508 } $else {
509 C.accept(l.sock.handle, 0, 0)
510 }
511 code := error_code()
512 if !l.is_blocking && new_handle <= 0 {
513 if code in [int(error_einprogress), int(error_ewouldblock), int(error_eagain), C.EINTR] {
514 l.wait_for_accept()!
515 new_handle = $if is_coroutine ? {
516 C.photon_accept(l.sock.handle, 0, 0, tcp_default_read_timeout)
517 } $else {
518 C.accept(l.sock.handle, 0, 0)
519 }
520 }
521 }
522 if new_handle <= 0 {
523 return error('accept failed')
524 }
525
526 return &TcpConn{
527 handle: new_handle
528 read_timeout: tcp_default_read_timeout
529 write_timeout: tcp_default_write_timeout
530 is_blocking: l.is_blocking
531 }
532}
533
534pub fn (c &TcpListener) accept_deadline() !time.Time {
535 if c.accept_deadline.unix() != 0 {
536 return c.accept_deadline
537 }
538 return error('invalid deadline')
539}
540
541pub fn (mut c TcpListener) set_accept_deadline(deadline time.Time) {
542 c.accept_deadline = deadline
543}
544
545pub fn (c &TcpListener) accept_timeout() time.Duration {
546 return c.accept_timeout
547}
548
549pub fn (mut c TcpListener) set_accept_timeout(t time.Duration) {
550 c.accept_timeout = t
551}
552
553pub fn (mut c TcpListener) wait_for_accept() ! {
554 return wait_for_read(c.sock.handle, c.accept_deadline, c.accept_timeout)
555}
556
557pub fn (mut c TcpListener) close() ! {
558 c.sock.close()!
559}
560
561pub fn (c &TcpListener) addr() !Addr {
562 return c.sock.address()
563}
564
565pub struct TcpSocket {
566 Socket
567}
568
569// This is a workaround for issue https://github.com/vlang/v/issues/20858
570// `noline` ensure that in `-prod` mode(CFLAG = `-O3 -flto`), gcc does not generate wrong instruction sequence
571@[noinline]
572pub fn new_tcp_socket(family AddrFamily) !TcpSocket {
573 handle := $if is_coroutine ? {
574 socket_error(C.photon_socket(i32(family), i32(SocketType.tcp), 0))!
575 } $else {
576 socket_error(C.socket(i32(family), i32(SocketType.tcp), 0))!
577 }
578 mut s := TcpSocket{
579 handle: handle
580 }
581 $if trace_tcp ? {
582 eprintln(' new_tcp_socket | s.handle: ${s.handle:6}')
583 }
584
585 // TODO(emily):
586 // we shouldn't be using ioctlsocket in the 21st century
587 // use the non-blocking socket option instead please :)
588
589 // Some options need to be set before the connection is established, otherwise they will not work.
590 s.set_default_options()!
591
592 // Set the desired "blocking/non-blocking" mode before the connection is established,
593 // and do not change it once the connection is successful.
594 $if net_nonblocking_sockets ? {
595 set_blocking(handle, false)!
596 }
597 return s
598}
599
600fn tcp_socket_from_handle(sockfd int) !TcpSocket {
601 mut s := TcpSocket{
602 handle: sockfd
603 }
604 $if trace_tcp ? {
605 eprintln(' tcp_socket_from_handle | s.handle: ${s.handle:6}')
606 }
607
608 s.set_dualstack(true) or {
609 // Not ipv6, we dont care
610 }
611 s.set_default_options()!
612
613 return s
614}
615
616// tcp_socket_from_handle_raw is similar to tcp_socket_from_handle, but it does not modify any socket options
617pub fn tcp_socket_from_handle_raw(sockfd int) TcpSocket {
618 mut s := TcpSocket{
619 handle: sockfd
620 }
621 $if trace_tcp ? {
622 eprintln(' tcp_socket_from_handle_raw | s.handle: ${s.handle:6}')
623 }
624 return s
625}
626
627fn (mut s TcpSocket) set_option(level int, opt int, value int) ! {
628 socket_error(C.setsockopt(s.handle, level, opt, &value, sizeof(int)))!
629}
630
631pub fn (mut s TcpSocket) set_option_bool(opt SocketOption, value bool) ! {
632 // TODO: reenable when this `in` operation works again
633 // if opt !in opts_can_set {
634 // return err_option_not_settable
635 // }
636 // if opt !in opts_bool {
637 // return err_option_wrong_type
638 // }
639 x := int(value)
640 s.set_option(C.SOL_SOCKET, int(opt), x)!
641}
642
643pub fn (mut s TcpSocket) set_option_int(opt SocketOption, value int) ! {
644 s.set_option(C.SOL_SOCKET, int(opt), value)!
645}
646
647pub fn (mut s TcpSocket) set_dualstack(on bool) ! {
648 x := int(!on)
649 s.set_option(C.IPPROTO_IPV6, int(SocketOption.ipv6_only), x)!
650}
651
652fn (mut s TcpSocket) set_default_options() ! {
653 s.set_option_int(.reuse_addr, 1)!
654
655 // At the socket level to ignore the exception signal (usually SIGNPIPE).
656 // In Linux, instead of using set_option(), specify the C.MSG_NOSIGNAL flag in c.send().
657 // In Windows, there is no need to process this signal.
658 $if macos {
659 s.set_option(C.SOL_SOCKET, C.SO_NOSIGPIPE, 1)!
660 }
661
662 // Enable the NODELAY option by default.
663 s.set_option(C.IPPROTO_TCP, C.TCP_NODELAY, 1)!
664}
665
666// bind a local rddress for TcpSocket
667pub fn (mut s TcpSocket) bind(addr string) ! {
668 addrs := resolve_addrs(addr, AddrFamily.ip, .tcp) or {
669 return error('${err.msg()}; could not resolve address ${addr}')
670 }
671
672 // TODO(logic to pick here)
673 a := addrs[0]
674
675 // cast to the correct type
676 alen := a.len()
677 socket_error_message(C.bind(s.handle, voidptr(&a), alen), 'binding to ${addr} failed') or {
678 return err
679 }
680}
681
682fn (mut s TcpSocket) close() ! {
683 shutdown(s.handle)
684 return close(s.handle)
685}
686
687const connect_timeout = 5 * time.second
688
689fn (mut s TcpSocket) connect(a Addr) ! {
690 $if net_nonblocking_sockets ? {
691 res := $if is_coroutine ? {
692 C.photon_connect(s.handle, voidptr(&a), a.len(), tcp_default_read_timeout)
693 } $else {
694 C.connect(s.handle, voidptr(&a), a.len())
695 }
696 ecode := error_code()
697 if res == 0 {
698 return
699 }
700 // On nix non-blocking sockets we expect einprogress
701 // On windows we expect res == -1 && error_code() == ewouldblock
702 if (is_windows && ecode == int(error_ewouldblock)) || (!is_windows && res == -1
703 && ecode in [int(error_einprogress), int(error_eagain), C.EINTR]) {
704 // The socket is nonblocking and the connection cannot be completed
705 // immediately. (UNIX domain sockets failed with EAGAIN instead.)
706 // It is possible to select(2) or poll(2) for completion by selecting
707 // the socket for writing. After select(2) indicates writability,
708 // use getsockopt(2) to read the SO_ERROR option at level SOL_SOCKET to
709 // determine whether connect() completed successfully (SO_ERROR is zero) or
710 // unsuccessfully (SO_ERROR is one of the usual error codes listed here,
711 // ex‐ plaining the reason for the failure).
712 write_result := select(s.handle, .write, connect_timeout)!
713 err := 0
714 len := sizeof(err)
715 xyz := C.getsockopt(s.handle, C.SOL_SOCKET, C.SO_ERROR, &err, &len)
716 if xyz == 0 && err == 0 {
717 return
718 }
719 if write_result {
720 if xyz == 0 {
721 wrap_error(err)!
722 return
723 }
724 return
725 }
726 return err_timed_out
727 }
728 wrap_error(ecode)!
729 return
730 } $else {
731 x := $if is_coroutine ? {
732 C.photon_connect(s.handle, voidptr(&a), a.len(), tcp_default_read_timeout)
733 } $else {
734 C.connect(s.handle, voidptr(&a), a.len())
735 }
736 socket_error(x)!
737 }
738}
739