-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_fstack.cpp
More file actions
299 lines (252 loc) · 8.24 KB
/
server_fstack.cpp
File metadata and controls
299 lines (252 loc) · 8.24 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// server_fstack.cpp
#include <array>
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "common.h"
#include <ff_api.h>
constexpr int LISTEN_PORT = 8080;
constexpr int BACKLOG = 1024;
constexpr int MAX_CLIENTS = 1024;
// Must match [port0].addr in config.ini
//static const char *g_bind_ip = "192.168.5.220";
static int g_listenfd = -1;
// Per-connection state
struct ClientState {
int fd = -1;
std::vector<char> recv_buffer;
size_t recv_bytes = 0;
size_t expected_size = sizeof(Msg);
std::vector<char> send_buffer;
size_t send_bytes = 0;
bool has_full_msg = false;
};
static std::array<ClientState, MAX_CLIENTS> g_client_states{};
static int g_client_count = 0;
// Helper: remove fd from the array
static void remove_client_at(int idx)
{
if (idx < 0 || idx >= g_client_count)
return;
ClientState& state = g_client_states[idx];
if (state.fd >= 0) {
ff_close(state.fd);
}
// Replace removed entry with the last one
if (idx < g_client_count - 1) {
g_client_states[idx] = std::move(g_client_states[g_client_count - 1]);
}
// Reset the final slot state
g_client_states[g_client_count - 1] = ClientState{};
g_client_count--;
}
// Receive a complete message (non-blocking)
// Returns: -1=error, 0=need more data, 1=got full message
static int recv_message(ClientState& state, int idx)
{
// Already have a full message buffered
if (state.has_full_msg) {
return 1;
}
if (state.recv_buffer.size() < state.expected_size) {
state.recv_buffer.resize(state.expected_size);
}
while (state.recv_bytes < state.expected_size) {
ssize_t n = ff_recv(state.fd,
state.recv_buffer.data() + state.recv_bytes,
state.expected_size - state.recv_bytes,
0);
if (n > 0) {
state.recv_bytes += n;
if (state.recv_bytes == sizeof(Msg) &&
state.expected_size == sizeof(Msg)) {
auto* header = reinterpret_cast<Msg*>(state.recv_buffer.data());
if (header->payload_size < sizeof(Msg)) {
std::fprintf(stderr,
"client fd=%d payload_size=%" PRIu32 " below header size %zu\n",
state.fd, header->payload_size, sizeof(Msg));
remove_client_at(idx);
return -1;
}
state.expected_size = header->payload_size;
state.recv_buffer.resize(state.expected_size);
}
} else if (n == 0) {
std::fprintf(stderr, "client fd=%d closed (recv)\n", state.fd);
remove_client_at(idx);
return -1;
} else {
if (errno == EINTR) {
continue;
}
if (errno == EAGAIN || errno == EPERM) {
return 0;
}
perror("ff_recv");
remove_client_at(idx);
return -1;
}
}
state.send_buffer = state.recv_buffer;
state.send_bytes = 0;
state.has_full_msg = true;
state.recv_buffer.assign(sizeof(Msg), 0);
state.expected_size = sizeof(Msg);
state.recv_bytes = 0;
return 1;
}
// Send a complete message (non-blocking)
// Returns: -1=error, 0=in progress, 1=done
static int send_message(ClientState& state, int idx)
{
if (!state.has_full_msg) {
return 1; // Nothing to send
}
if (state.send_buffer.empty()) {
state.has_full_msg = false;
return 1;
}
while (state.send_bytes < state.send_buffer.size()) {
ssize_t n = ff_send(state.fd,
state.send_buffer.data() + state.send_bytes,
state.send_buffer.size() - state.send_bytes,
0);
if (n > 0) {
state.send_bytes += n;
} else if (n == 0) {
// Peer closed the connection
std::fprintf(stderr, "client fd=%d closed (send)\n", state.fd);
remove_client_at(idx);
return -1;
} else {
// n < 0
if (errno == EINTR) {
continue;
}
if (errno == EAGAIN || errno == EPERM) {
// Can't send now; retry on next loop
return 0;
}
perror("ff_send");
remove_client_at(idx);
return -1;
}
}
// Send complete; reset state
state.send_bytes = 0;
state.has_full_msg = false;
state.send_buffer.clear();
return 1;
}
// Run one non-blocking recv+echo attempt for a single client
static void process_one_client(int idx)
{
ClientState& state = g_client_states[idx];
// 1. Attempt to receive a complete message
int recv_result = recv_message(state, idx);
if (recv_result < 0) {
return; // Error or need more data
}
// 2. Attempt to send the message
int send_result = send_message(state, idx);
if (send_result < 0) {
return; // Error
}
// Continue next loop if still sending
}
static int server_loop(void *arg)
{
(void)arg;
// Initialize the listen fd on first call
if (g_listenfd < 0) {
g_listenfd = ff_socket(AF_INET, SOCK_STREAM, 0);
if (g_listenfd < 0) {
perror("ff_socket");
return -1;
}
const int yes = 1;
if (ff_setsockopt(g_listenfd, SOL_SOCKET, SO_REUSEADDR,
&yes, sizeof(yes)) < 0) {
perror("ff_setsockopt");
return -1;
}
sockaddr_in addr{};
addr.sin_family = AF_INET;
addr.sin_port = htons(LISTEN_PORT);
// if (inet_pton(AF_INET, g_bind_ip, &addr.sin_addr) != 1) {
// perror("inet_pton g_bind_ip");
// return -1;
// }
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (ff_bind(g_listenfd,
reinterpret_cast<struct linux_sockaddr *>(&addr),
sizeof(addr)) < 0) {
perror("ff_bind");
return -1;
}
if (ff_listen(g_listenfd, BACKLOG) < 0) {
perror("ff_listen");
return -1;
}
std::printf("F-Stack simple echo server listening on %d\n",
LISTEN_PORT);
std::fprintf(stdout, "Msg header size: %zu bytes\n", sizeof(Msg));
}
// 1) Accept as many new connections as possible per loop
for (;;) {
int cfd = ff_accept(g_listenfd, nullptr, nullptr);
if (cfd < 0) {
if (errno == EAGAIN || errno == EINTR || errno == EPERM) {
// No more pending connections
break;
}
perror("ff_accept");
break;
}
if (g_client_count >= MAX_CLIENTS) {
std::fprintf(stderr, "too many clients, closing fd=%d\n", cfd);
ff_close(cfd);
continue;
}
// Initialize new client state
ClientState& state = g_client_states[g_client_count];
state.fd = cfd;
state.recv_bytes = 0;
state.expected_size = sizeof(Msg);
state.recv_buffer.assign(sizeof(Msg), 0);
state.send_bytes = 0;
state.send_buffer.clear();
state.has_full_msg = false;
g_client_count++;
// printf("new client fd=%d, total=%d\n", cfd, g_client_count);
}
// 2) Walk every connected client in this loop
int idx = 0;
while (idx < g_client_count) {
int current_client_count = g_client_count; // Save current value
process_one_client(idx);
// If a client was removed, g_client_count shrinks
// If the current client remains, move to the next one
if (g_client_count == current_client_count) {
// Client not removed
idx++;
}
// If a client was removed, idx stays because next client moved here
}
return 0;
}
int main(int argc, char *argv[])
{
int ret = ff_init(argc, argv);
if (ret < 0) {
std::fprintf(stderr, "ff_init failed\n");
return 1;
}
ff_run(server_loop, nullptr);
return 0;
}