@@ -22,6 +22,9 @@ void addSock(int fd);
22
22
void epoll_cycle ();
23
23
int backlog = 64 ;
24
24
25
+ //epoll fd
26
+ int epollfd ;
27
+
25
28
//forward declare structs
26
29
typedef uint32_t refcnt ;
27
30
struct conn ;
@@ -76,6 +79,8 @@ typedef enum{
76
79
service_status_stopped //service stopped
77
80
} servicestate ;
78
81
82
+ void close_conn (struct conn * c );
83
+
79
84
//get a string representing a state
80
85
char * statestr (servicestate state ) {
81
86
switch (state ) {
@@ -130,6 +135,16 @@ enum{ notify_fail, notify_success, notify_gone } notify(struct conn *c, struct s
130
135
memcpy (ndat + c -> tx .len , msg .dat , msg .len );
131
136
c -> tx .dat = ndat ;
132
137
c -> tx .len += msg .len ;
138
+ if (c -> txoff && (c -> tx .len > 0 )) { //tx buffer was just populated
139
+ //enable write in epoll
140
+ struct epoll_event ev = {.events = EPOLLIN | EPOLLOUT , .data = {.ptr = & c -> e }};
141
+ if (epoll_ctl (epollfd , EPOLL_CTL_MOD , c -> fd , & ev ) == -1 ) { //failed to enable - close and forget
142
+ fprintf (stderr , "[ERROR] Failed to enable writing on conn %d\n" , c -> fd );
143
+ close_conn (c );
144
+ } else {
145
+ c -> txoff = false;
146
+ }
147
+ }
133
148
return notify_success ;
134
149
}
135
150
//run on a state change to notify all associated conns of the change
@@ -218,8 +233,10 @@ void rmService(char* name) {
218
233
bool readBuf (struct buf * b , int fd ) { //read data from fd into the buffer
219
234
byte dat [1024 ];
220
235
size_t n = read (fd , dat , 1024 );
221
- if (n < 1 ) {
236
+ if (n < 0 ) {
222
237
return false;
238
+ } else if (n == 0 ) {
239
+ return true;
223
240
}
224
241
byte * nd = arralloc (byte , n + b -> len );
225
242
if (nd == NULL ) {
@@ -264,8 +281,7 @@ void set_noblock(int fd) {
264
281
exit (65 );
265
282
}
266
283
267
- //epoll fd
268
- int epollfd ;
284
+
269
285
//epoll-associated data
270
286
void addSock (int fd ) { //add a socket to epoll
271
287
printf ("Sock: %d\n" , fd );
@@ -631,6 +647,7 @@ void cmd_state(struct conn *c, char *args) {
631
647
servicestate s = service_status_null ;
632
648
if (streq (states , "running" )) {
633
649
s = service_status_running ;
650
+ printf ("[INFO] Finished %s\n" , sname );
634
651
} else if (streq (states , "stopped" )) {
635
652
s = service_status_stopped ;
636
653
} else {
0 commit comments