Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
socket_ops.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Roc Streaming authors
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
8 
9 //! @file roc_netio/target_posix/roc_netio/socket_ops.h
10 //! @brief Socket operations.
11 
12 #ifndef ROC_NETIO_SOCKET_OPS_H_
13 #define ROC_NETIO_SOCKET_OPS_H_
14 
16 #include "roc_core/attributes.h"
17 #include "roc_core/stddefs.h"
18 
19 namespace roc {
20 namespace netio {
21 
22 //! Socket type.
23 enum SocketType {
24  SocketType_Tcp, //!< TCP socket.
25  SocketType_Udp //!< UDP socket.
26 };
27 
28 //! Socket options.
29 struct SocketOpts {
30  //! Disable Nagle's algorithm.
32 
33  SocketOpts()
34  : disable_nagle(true) {
35  }
36 };
37 
38 //! I/O error codes.
40  //! Operation can't be performed without blocking, try later.
42 
43  //! End of stream, no more data.
45 
46  //! Failure.
47  SockErr_Failure = -3
48 };
49 
50 //! Platform-specific socket handle.
51 typedef int SocketHandle;
52 
53 //! Invalid socket handle.
55 
56 //! Create non-blocking socket.
59 
60 //! Accept incoming connection.
62  SocketHandle& new_sock,
63  address::SocketAddr& remote_address);
64 
65 //! Set socket options.
67 
68 //! Bind socket to local address.
70  address::SocketAddr& local_address);
71 
72 //! Start listening for incoming connections.
73 ROC_ATTR_NODISCARD bool socket_listen(SocketHandle sock, size_t backlog);
74 
75 //! Initiate connecting to remote peer.
76 //! @returns true if connection was successfully initiated.
77 //! Sets @p completed_immediately to true if connection was established
78 //! immediately and there is no need to wait for it.
80  const address::SocketAddr& remote_address,
81  bool& completed_immediately);
82 
83 //! Finish connecting to remote peer.
84 //! @returns true if connection was successfully established.
86 
87 //! Try to read bytes from socket without blocking.
88 //! @returns number of bytes read (>= 0) or SocketError (< 0).
89 ROC_ATTR_NODISCARD ssize_t socket_try_recv(SocketHandle sock, void* buf, size_t bufsz);
90 
91 //! Try to write bytes to socket without blocking.
92 //! @returns number of bytes written (>= 0) or SocketError (< 0).
94  const void* buf,
95  size_t bufsz);
96 
97 //! Try to send datagram via socket to given address, without blocking.
98 //! @returns number of bytes written (>= 0) or SocketError (< 0).
100  const void* buf,
101  size_t bufsz,
102  const address::SocketAddr& remote_address);
103 
104 //! Gracefully shutdown connection.
106 
107 //! Close socket.
109 
110 //! Close socket and send reset to remote peer.
111 //! Remote peer will get error when reading from connection.
113 
114 } // namespace netio
115 } // namespace roc
116 
117 #endif // ROC_NETIO_SOCKET_OPS_H_
Compiler attributes.
#define ROC_ATTR_NODISCARD
Emit warning if function result is not checked.
Definition: attributes.h:31
Socket address.
Definition: socket_addr.h:26
AddrFamily
Address family.
Definition: addr_family.h:19
ROC_ATTR_NODISCARD bool socket_close(SocketHandle sock)
Close socket.
int SocketHandle
Platform-specific socket handle.
Definition: socket_ops.h:51
ROC_ATTR_NODISCARD bool socket_create(address::AddrFamily family, SocketType type, SocketHandle &new_sock)
Create non-blocking socket.
SocketError
I/O error codes.
Definition: socket_ops.h:39
@ SockErr_Failure
Failure.
Definition: socket_ops.h:47
@ SockErr_WouldBlock
Operation can't be performed without blocking, try later.
Definition: socket_ops.h:41
@ SockErr_StreamEnd
End of stream, no more data.
Definition: socket_ops.h:44
ROC_ATTR_NODISCARD bool socket_shutdown(SocketHandle sock)
Gracefully shutdown connection.
ROC_ATTR_NODISCARD bool socket_listen(SocketHandle sock, size_t backlog)
Start listening for incoming connections.
ROC_ATTR_NODISCARD bool socket_end_connect(SocketHandle sock)
Finish connecting to remote peer.
ROC_ATTR_NODISCARD bool socket_accept(SocketHandle sock, SocketHandle &new_sock, address::SocketAddr &remote_address)
Accept incoming connection.
ROC_ATTR_NODISCARD ssize_t socket_try_send_to(SocketHandle sock, const void *buf, size_t bufsz, const address::SocketAddr &remote_address)
Try to send datagram via socket to given address, without blocking.
ROC_ATTR_NODISCARD bool socket_close_with_reset(SocketHandle sock)
Close socket and send reset to remote peer. Remote peer will get error when reading from connection.
const SocketHandle SocketInvalid
Invalid socket handle.
Definition: socket_ops.h:54
SocketType
Socket type.
Definition: socket_ops.h:23
@ SocketType_Tcp
TCP socket.
Definition: socket_ops.h:24
@ SocketType_Udp
UDP socket.
Definition: socket_ops.h:25
ROC_ATTR_NODISCARD bool socket_begin_connect(SocketHandle sock, const address::SocketAddr &remote_address, bool &completed_immediately)
Initiate connecting to remote peer.
ROC_ATTR_NODISCARD bool socket_bind(SocketHandle sock, address::SocketAddr &local_address)
Bind socket to local address.
ROC_ATTR_NODISCARD ssize_t socket_try_recv(SocketHandle sock, void *buf, size_t bufsz)
Try to read bytes from socket without blocking.
ROC_ATTR_NODISCARD ssize_t socket_try_send(SocketHandle sock, const void *buf, size_t bufsz)
Try to write bytes to socket without blocking.
ROC_ATTR_NODISCARD bool socket_setup(SocketHandle sock, const SocketOpts &options)
Set socket options.
Root namespace.
Socket address.
Commonly used types and functions.
Socket options.
Definition: socket_ops.h:29
bool disable_nagle
Disable Nagle's algorithm.
Definition: socket_ops.h:31