Skip to content

Commit 3a1f84a

Browse files
committed
fix IPC for pub/sub protocol - buffer size info missing + minor stuff
1 parent b422dee commit 3a1f84a

File tree

9 files changed

+396
-251
lines changed

9 files changed

+396
-251
lines changed

examples/chat.c

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,21 @@ Callbacks and object used by main()
2525
***************************************************************************** */
2626

2727
/** Called when a new connection is created and login process starts. */
28-
FIO_SFUNC void on_login_start(fio_s *io);
28+
FIO_SFUNC void on_login_start(fio_io_s *io);
2929
/** Called there's incoming data (from STDIN / the client socket. */
30-
FIO_SFUNC void on_data_login(fio_s *io);
31-
FIO_SFUNC void on_data_chat(fio_s *io);
30+
FIO_SFUNC void on_data_login(fio_io_s *io);
31+
FIO_SFUNC void on_data_chat(fio_io_s *io);
3232
/** Called when a login process should be performed. */
33-
FIO_SFUNC void on_shutdown(fio_s *io);
33+
FIO_SFUNC void on_shutdown(fio_io_s *io);
3434
/** Called when the monitored IO is closed or has a fatal error. */
35-
FIO_SFUNC void on_close(void *udata);
35+
FIO_SFUNC void on_close(void *buf, void *udata);
3636

37-
static fio_protocol_s CHAT_PROTOCOL_LOGIN = {
37+
static fio_io_protocol_s CHAT_PROTOCOL_LOGIN = {
3838
.on_attach = on_login_start,
3939
.on_data = on_data_login,
4040
.on_close = on_close,
4141
};
42-
static fio_protocol_s CHAT_PROTOCOL_CHAT = {
42+
static fio_io_protocol_s CHAT_PROTOCOL_CHAT = {
4343
.on_data = on_data_chat,
4444
.on_close = on_close,
4545
.on_shutdown = on_shutdown,
@@ -70,8 +70,8 @@ FIO_IFUNC void client_free(client_s *c) {
7070
}
7171

7272
/** Called when a new connection is created and login process starts. */
73-
FIO_SFUNC void on_login_start(fio_s *io) {
74-
fio_udata_set(io, client_new());
73+
FIO_SFUNC void on_login_start(fio_io_s *io) {
74+
fio_io_udata_set(io, client_new());
7575
FIO_STR_INFO_TMP_VAR(node_msg, 1024);
7676
fio_string_write2(
7777
&node_msg,
@@ -80,13 +80,13 @@ FIO_SFUNC void on_login_start(fio_s *io) {
8080
FIO_STRING_WRITE_UNUM(getpid()),
8181
FIO_STRING_WRITE_STR1(
8282
"\nPlease enter a login handle (up to 30 characters long)\n"));
83-
fio_write(io, node_msg.buf, node_msg.len);
83+
fio_io_write(io, node_msg.buf, node_msg.len);
8484
if (fio_cli_get_bool("-v"))
8585
FIO_LOG_INFO("(%d) %p connected", getpid(), (void *)io);
8686
}
8787

8888
/** Called when the monitored IO is closed or has a fatal error. */
89-
FIO_SFUNC void on_close(void *udata) {
89+
FIO_SFUNC void on_close(void *buf, void *udata) {
9090
FIO_STR_INFO_TMP_VAR(s, CHAT_MAX_HANDLE_LEN + 32);
9191
client_s *c = udata;
9292
fio_string_write2(&s,
@@ -95,10 +95,11 @@ FIO_SFUNC void on_close(void *udata) {
9595
FIO_STRING_WRITE_STR1(" left the chat.\n"));
9696
fio_publish(.message = FIO_STR2BUF_INFO(s));
9797
client_free(c);
98+
(void)buf;
9899
}
99100

100101
/** Performs "login" logic (saves user handle) */
101-
FIO_SFUNC void on_data_first_line(fio_s *io, char *name, size_t len) {
102+
FIO_SFUNC void on_data_first_line(fio_io_s *io, char *name, size_t len) {
102103
if (!len)
103104
goto error_name_too_short;
104105
do
@@ -110,7 +111,7 @@ FIO_SFUNC void on_data_first_line(fio_s *io, char *name, size_t len) {
110111
goto error_name_too_short;
111112
if (len > 30)
112113
goto error_name_too_long;
113-
client_s *c = fio_udata(io);
114+
client_s *c = fio_io_udata(io);
114115
memcpy(c->name, name, len);
115116
c->name[len] = 0;
116117
c->name[31] = (char)len;
@@ -122,23 +123,23 @@ FIO_SFUNC void on_data_first_line(fio_s *io, char *name, size_t len) {
122123
FIO_STRING_WRITE_STR1("You are connected to node: "),
123124
FIO_STRING_WRITE_UNUM(getpid()),
124125
FIO_STRING_WRITE_STR1("\n"));
125-
fio_write2(io,
126-
.buf = welcome,
127-
.len = fio_bstr_len(welcome),
128-
.dealloc = (void (*)(void *))fio_bstr_free);
126+
fio_io_write2(io,
127+
.buf = welcome,
128+
.len = fio_bstr_len(welcome),
129+
.dealloc = (void (*)(void *))fio_bstr_free);
129130
return;
130131
error_name_too_long:
131-
fio_write(io, "ERROR! login handle too long. Goodbye.\n", 39);
132-
fio_close(io);
132+
fio_io_write(io, "ERROR! login handle too long. Goodbye.\n", 39);
133+
fio_io_close(io);
133134
return;
134135
error_name_too_short:
135-
fio_write(io, "ERROR! login handle too short (empty?). Goodbye.\n", 49);
136-
fio_close(io);
136+
fio_io_write(io, "ERROR! login handle too short (empty?). Goodbye.\n", 49);
137+
fio_io_close(io);
137138
}
138139

139140
/** Manages chat messages */
140-
FIO_SFUNC void on_data_message_line(fio_s *io, char *msg, size_t len) {
141-
client_s *c = fio_udata(io);
141+
FIO_SFUNC void on_data_message_line(fio_io_s *io, char *msg, size_t len) {
142+
client_s *c = fio_io_udata(io);
142143
char *buf = fio_bstr_write2(NULL,
143144
FIO_STRING_WRITE_STR2(c->name, c->name[31]),
144145
FIO_STRING_WRITE_STR2(": ", 2),
@@ -153,27 +154,27 @@ FIO_SFUNC void on_data_message_line(fio_s *io, char *msg, size_t len) {
153154
((msg[2] | 32) == 'o') & ((msg[3] | 32) == 'd') &
154155
((msg[4] | 32) == 'b') & ((msg[5] | 32) == 'y') &
155156
((msg[6] | 32) == 'e')))) {
156-
fio_write(io, "Goodbye.\n", 9);
157-
fio_close(io);
157+
fio_io_write(io, "Goodbye.\n", 9);
158+
fio_io_close(io);
158159
}
159160
}
160161

161-
FIO_IFUNC int on_data_read(fio_s *io) {
162+
FIO_IFUNC int on_data_read(fio_io_s *io) {
162163
char buf[CHAT_MAX_MESSAGE_LEN];
163-
size_t r = fio_read(io, buf, CHAT_MAX_MESSAGE_LEN);
164+
size_t r = fio_io_read(io, buf, CHAT_MAX_MESSAGE_LEN);
164165
if (!r)
165166
return -1;
166-
client_s *c = fio_udata(io);
167+
client_s *c = fio_io_udata(io);
167168
fio_stream_add(&c->input, fio_stream_pack_data(buf, r, 0, 1, NULL));
168169
return 0;
169170
}
170171

171-
FIO_IFUNC int on_data_process_line(fio_s *io,
172-
void(task)(fio_s *, char *, size_t)) {
172+
FIO_IFUNC int on_data_process_line(fio_io_s *io,
173+
void(task)(fio_io_s *, char *, size_t)) {
173174
char tmp[CHAT_MAX_MESSAGE_LEN];
174175
char *buf = tmp;
175176
size_t len = CHAT_MAX_MESSAGE_LEN;
176-
client_s *c = fio_udata(io);
177+
client_s *c = fio_io_udata(io);
177178
fio_stream_read(&c->input, &buf, &len);
178179
if (!len)
179180
return -1;
@@ -187,27 +188,27 @@ FIO_IFUNC int on_data_process_line(fio_s *io,
187188
}
188189

189190
/** for the first input line of the Chat protocol. */
190-
FIO_SFUNC void on_data_login(fio_s *io) {
191+
FIO_SFUNC void on_data_login(fio_io_s *io) {
191192
if (on_data_read(io))
192193
return;
193194
if (on_data_process_line(io, on_data_first_line))
194195
return;
195-
fio_protocol_set(io, &CHAT_PROTOCOL_CHAT);
196+
fio_io_protocol_set(io, &CHAT_PROTOCOL_CHAT);
196197
fio_subscribe(.io = io);
197198
on_data_chat(io);
198199
}
199200

200201
/** for each subsequent message / line in the Chat protocol. */
201-
FIO_SFUNC void on_data_chat(fio_s *io) {
202+
FIO_SFUNC void on_data_chat(fio_io_s *io) {
202203
if (on_data_read(io))
203204
return;
204205
while (!on_data_process_line(io, on_data_message_line))
205206
;
206207
}
207208

208209
/** Called when a login process should be performed. */
209-
FIO_SFUNC void on_shutdown(fio_s *io) {
210-
fio_write(io, "Server shutting down, goodbye...\n", 33);
210+
FIO_SFUNC void on_shutdown(fio_io_s *io) {
211+
fio_io_write(io, "Server shutting down, goodbye...\n", 33);
211212
}
212213

213214
/* *****************************************************************************
@@ -248,14 +249,14 @@ int main(int argc, char const *argv[]) {
248249
fio_subscribe(.on_message = print_chat, .master_only = 1);
249250

250251
/* review CLI connection address (in URL format) */
251-
FIO_ASSERT(fio_srv_listen(.url = fio_cli_unnamed(0),
252-
.protocol = &CHAT_PROTOCOL_LOGIN),
252+
FIO_ASSERT(fio_io_listen(.url = fio_cli_unnamed(0),
253+
.protocol = &CHAT_PROTOCOL_LOGIN),
253254
"Could not open listening socket as requested.");
254255
FIO_LOG_INFO("\n\tStarting plain text Chat server example app."
255256
"\n\tEngine: " FIO_POLL_ENGINE_STR "\n\tWorkers: %d"
256257
"\n\tPress ^C to exit.",
257-
fio_srv_workers(fio_cli_get_i("-w")));
258-
fio_srv_start(fio_cli_get_i("-w"));
258+
fio_io_workers(fio_cli_get_i("-w")));
259+
fio_io_start(fio_cli_get_i("-w"));
259260
FIO_LOG_INFO("Shutdown complete.");
260261
fio_cli_end();
261262
return 0;

examples/client.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ FIO_SFUNC void on_input_closed(void *buf, void *udata) {
199199
}
200200

201201
/* Debug messages for STDIN round-trip */
202-
void debug_subscriber(fio_msg_s *msg) {
202+
FIO_SFUNC void debug_subscriber(fio_msg_s *msg) {
203203
FIO_LOG_DEBUG2("Subscriber received: %.*s",
204204
msg->message.len,
205205
msg->message.buf);

0 commit comments

Comments
 (0)