-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket.c
115 lines (90 loc) · 2.75 KB
/
socket.c
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "socket.h"
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include "connection.h"
#include "log.h"
#include "panic.h"
static const int kBacklog = 128;
int make_listener_socket(int port) {
int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if (socket_fd < 0) {
panic_errno();
}
int opt = 1;
if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
panic_errno();
}
struct sockaddr_in serv_addr;
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(port);
if (bind(socket_fd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
panic_errno();
}
if (listen(socket_fd, kBacklog) < 0) {
panic_errno();
}
return socket_fd;
}
void make_socket_non_blocking(int socket_fd) {
int flags = fcntl(socket_fd, F_GETFL, 0);
if (flags == -1) {
panic_errno();
}
if (fcntl(socket_fd, F_SETFL, flags | O_NONBLOCK) == -1) {
panic_errno();
}
}
int accept_new_socket(int listener_fd) {
struct sockaddr peer_address;
socklen_t peer_address_length = sizeof(peer_address);
int new_socket_fd = accept(listener_fd, (struct sockaddr*)&peer_address,
&peer_address_length);
if (new_socket_fd < 0) {
if (errno != EAGAIN && errno != EWOULDBLOCK) {
panic_errno();
}
}
return new_socket_fd;
}
void async_read_some(int fd) {
struct connection* connection = get_connection(fd);
ssize_t read_target_length =
connection->read_buffer_target_length - connection->read_buffer_length;
ssize_t actual_length =
recv(fd, connection->read_buffer + connection->read_buffer_length,
read_target_length * sizeof(char), 0);
if (actual_length == 0) {
LOG_WARNING("[connection %ld] disconnected", connection->number);
connection->state = kClosed;
} else if (actual_length < 0) {
if (errno != EAGAIN && errno != EWOULDBLOCK) {
panic_errno();
}
return;
}
connection->read_buffer_length += actual_length;
}
void async_write_some(int fd) {
struct connection* connection = get_connection(fd);
ssize_t write_target_length =
connection->write_buffer_target_length - connection->write_buffer_length;
ssize_t actual_length =
send(fd, connection->write_buffer + connection->write_buffer_length,
write_target_length * sizeof(char), 0);
if (actual_length == 0) {
LOG_WARNING("[connection %ld] disconnected", connection->number);
connection->state = kClosed;
} else if (actual_length < 0) {
if (errno != EAGAIN && errno != EWOULDBLOCK) {
panic_errno();
}
return;
}
connection->write_buffer_length += actual_length;
}