int so_tcp_recv(struct usock *up,struct mbuf **bpp,struct sockaddr *from, int *fromlen) { long cnt; struct tcb *tcb; while((tcb = up->cb.tcb) != NULL && tcb->r_upcall != trdiscard && (cnt = recv_tcp(tcb,bpp,0)) == -1){ if(up->noblock){ errno = EWOULDBLOCK; return -1; } else if((errno = kwait(up)) != 0){ return -1; } } if(tcb == NULL){ /* Connection went away */ errno = ENOTCONN; return -1; } else if(tcb->r_upcall == trdiscard){ /* Receive shutdown has been done */ errno = ENOTCONN; /* CHANGE */ return -1; } return cnt; }
/* TCP discard server */ static void disc_server( struct tcb *tcb, int32 cnt) { struct mbuf *bp; if (recv_tcp(tcb, &bp, 0) > 0) free_p(&bp); }
void recv_call_handler(unsigned long long uniqueSockID, int threads, unsigned char *buf, ssize_t len) { int index; int datalen; int flags; u_char *pt; PRINT_DEBUG(); pt = buf; datalen = *(size_t *) pt; pt += sizeof(size_t); flags = *(int *) pt; pt += sizeof(int); if (pt - buf != len) { PRINT_DEBUG("READING ERROR! CRASH, diff=%d len=%d", pt - buf, len); nack_send(uniqueSockID, recv_call); return; } /** Notice that send is only used with tcp connections since * the receiver is already known */ index = findjinniSocket(uniqueSockID); if (index == -1) { PRINT_DEBUG("CRASH !!socket descriptor not found into jinni sockets"); nack_send(uniqueSockID, recv_call); return; } if (jinniSockets[index].type == SOCK_DGRAM) { /** Whenever we need to implement non_blocking mode using * threads. We will call the function below using thread_create */ recv_udp(uniqueSockID, datalen, flags); } else if (jinniSockets[index].type == SOCK_STREAM) { recv_tcp(uniqueSockID, datalen, flags); } else { PRINT_DEBUG("This socket is of unknown type"); nack_send(uniqueSockID, recv_call); } } // end of recv_call_handler()
/* TCP echo server * Copies only as much will fit on the transmit queue */ static void echo_server( struct tcb *tcb, int32 cnt) { int32 acnt; struct mbuf *bp; acnt = tcb->window - tcb->sndcnt; if (acnt > tcb->rcvcnt) acnt = tcb->rcvcnt; if (acnt > 0) { if (recv_tcp(tcb, &bp, acnt) > 0) send_tcp(tcb, &bp); } }
static void tcp_receive(struct tcb *tcb, int32 cnt) { char buffer[1024]; struct mbuf *bp; if (tcb->user > 0) { recv_tcp(tcb, &bp, 0); while ((cnt = pullup(&bp, buffer, sizeof(buffer))) > 0) if (write(tcb->user, buffer, (unsigned) cnt) != cnt) { free_p(&bp); close_tcp(tcb); return; } } }
void cb_rw(int fd, short event, void *args) { connection *c = (connection *)args; if(event == EV_READ) { if(c->transport == SOCK_STREAM) recv_tcp(c); else recv_udp(c); } if(event == EV_WRITE) { if(c->transport == SOCK_STREAM) send_tcp(c); else send_udp(c); } }