-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket_bytes_handlers.h
85 lines (77 loc) · 2.81 KB
/
socket_bytes_handlers.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#pragma once
#include <stdint.h>
#include <sys/types.h>
/** @struct Listener_Socket
* @brief This structure represents a socket listening for connections
*/
typedef struct {
/** @brief The socket for the listening connection. Note: do not use
* this directly! Take a look at listener_init and
* listener_accept instead!
*/
int fd;
} Listener_Socket;
/** @brief Initializes a listener socket that listens on the provided
* port on all of the interfaces for the host.
*
* @param sock The Listener_Socket to initialize.
*
* @param port The port on which to listen.
*
* @return 0, indicating success, or -1, indicating that it failed to
* listen.
*/
int listener_init(Listener_Socket *sock, int port);
/** @brief Accept a new connection and initialize a 5 second timeout
*
* @param sock The Listener_Socket from which to get the new
* connection.
*
* @return An socket for the new connection, or -1, if there is an
* error. Sets errno according to any errors that occur.
*/
int listener_accept(Listener_Socket *sock);
/** @brief Reads bytes from in into buf until either (1) in has read
* n bytes, (2) in is out of bytes to return, or (3) there is
* an error reading bytes.
*
* @param in The file descriptor or socket from which to read.
*
* @param buf The buffer in which to put read data.
*
* @param n The maximum bytes to read. Must be less than or
* equal to the size of buf.
*
* @return The number of bytes read, or, -1, indicating an error.
* Note: this function treats a timeout as an error. Sets
* errno according to any errors that occur.
*/
ssize_t read_n_bytes(int in, char buf[], size_t n);
/** @brief Writes bytes to out from buf until either (1) it has written
* exactly n bytes or (2) it encounters an error on write.
*
* @param out The file descriptor or socket to write to.
*
* @param buf The buffer containing data to write.
*
* @param n The number of bytes to write. Must be less than or
* equal to the size of buf.
*
* @return The number of bytes written, or, -1, indicating an error.
* Sets errno according to any errors that occur.
*/
ssize_t write_n_bytes(int out, char buf[], size_t n);
/** @brief Reads bytes from src and places them in dest until either
* (1) it has read/written exactly nbytes, (2) read returns 0,
* or (3) it encounters an error on read/write.
*
* @param src The file descriptor or socket from which to read.
*
* @param dst The file descriptor or socket to write to.
*
* @param n The number of bytes to read/write. *
*
* @return The number of bytes written, or, -1, indicating an error.
* Sets errno according to any errors that occur.
*/
ssize_t pass_n_bytes(int src, int dst, size_t n);