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#include "roc_netio/io_error.h"
20
21namespace roc {
22namespace netio {
23
24//! Socket type.
26 SocketType_Tcp, //!< TCP socket.
27 SocketType_Udp //!< UDP socket.
28};
29
30//! Platform-specific socket handle.
31typedef int SocketHandle;
32
33//! Invalid socket handle.
35
36//! Create non-blocking socket.
39
40//! Accept incoming connection.
42 SocketHandle& new_sock,
43 address::SocketAddr& remote_address);
44
45//! Set socket options.
47
48//! Bind socket to local address.
50 address::SocketAddr& local_address);
51
52//! Start listening for incoming connections.
54
55//! Initiate connecting to remote peer.
56//! @returns true if connection was successfully initiated.
57//! Sets @p completed_immediately to true if connection was established
58//! immediately and there is no need to wait for it.
60 const address::SocketAddr& remote_address,
61 bool& completed_immediately);
62
63//! Finish connecting to remote peer.
64//! @returns true if connection was successfully established.
66
67//! Try to read bytes from socket without blocking.
68//! @returns number of bytes read (>= 0) or IOError (< 0).
69ROC_ATTR_NODISCARD ssize_t socket_try_recv(SocketHandle sock, void* buf, size_t bufsz);
70
71//! Try to write bytes to socket without blocking.
72//! @returns number of bytes written (>= 0) or IOError (< 0).
74 const void* buf,
75 size_t bufsz);
76
77//! Try to send datagram via socket to given address, without blocking.
78//! @returns number of bytes written (>= 0) or IOError (< 0).
80 const void* buf,
81 size_t bufsz,
82 const address::SocketAddr& remote_address);
83
84//! Gracefully shutdown connection.
86
87//! Close socket.
89
90//! Close socket and send reset to remote peer.
91//! Remote peer will get error when reading from connection.
93
94} // namespace netio
95} // namespace roc
96
97#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
I/O error codes.
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:31
ROC_ATTR_NODISCARD bool socket_create(address::AddrFamily family, SocketType type, SocketHandle &new_sock)
Create non-blocking socket.
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.
ROC_ATTR_NODISCARD bool socket_setup(SocketHandle sock, const SocketOptions &options)
Set socket options.
const SocketHandle SocketInvalid
Invalid socket handle.
Definition: socket_ops.h:34
SocketType
Socket type.
Definition: socket_ops.h:25
@ SocketType_Tcp
TCP socket.
Definition: socket_ops.h:26
@ SocketType_Udp
UDP socket.
Definition: socket_ops.h:27
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.
Root namespace.
Socket address.
Socket options.
Commonly used types and functions.