@@ -22,6 +22,9 @@ void addSock(int fd);
2222void epoll_cycle ();
2323int backlog = 64 ;
2424
25+ //epoll fd
26+ int epollfd ;
27+
2528//forward declare structs
2629typedef uint32_t refcnt ;
2730struct conn ;
@@ -76,6 +79,8 @@ typedef enum{
7679 service_status_stopped //service stopped
7780} servicestate ;
7881
82+ void close_conn (struct conn * c );
83+
7984//get a string representing a state
8085char * statestr (servicestate state ) {
8186 switch (state ) {
@@ -130,6 +135,16 @@ enum{ notify_fail, notify_success, notify_gone } notify(struct conn *c, struct s
130135 memcpy (ndat + c -> tx .len , msg .dat , msg .len );
131136 c -> tx .dat = ndat ;
132137 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+ }
133148 return notify_success ;
134149}
135150//run on a state change to notify all associated conns of the change
@@ -218,8 +233,10 @@ void rmService(char* name) {
218233bool readBuf (struct buf * b , int fd ) { //read data from fd into the buffer
219234 byte dat [1024 ];
220235 size_t n = read (fd , dat , 1024 );
221- if (n < 1 ) {
236+ if (n < 0 ) {
222237 return false;
238+ } else if (n == 0 ) {
239+ return true;
223240 }
224241 byte * nd = arralloc (byte , n + b -> len );
225242 if (nd == NULL ) {
@@ -264,8 +281,7 @@ void set_noblock(int fd) {
264281 exit (65 );
265282}
266283
267- //epoll fd
268- int epollfd ;
284+
269285//epoll-associated data
270286void addSock (int fd ) { //add a socket to epoll
271287 printf ("Sock: %d\n" , fd );
@@ -631,6 +647,7 @@ void cmd_state(struct conn *c, char *args) {
631647 servicestate s = service_status_null ;
632648 if (streq (states , "running" )) {
633649 s = service_status_running ;
650+ printf ("[INFO] Finished %s\n" , sname );
634651 } else if (streq (states , "stopped" )) {
635652 s = service_status_stopped ;
636653 } else {
0 commit comments