void str_echo(int sockfd) { int n; int len; char *buf; while (1) { //COMPLETED THE MISSING AND IMPORTANT LINE HERE n = net_readn(sockfd, &len, sizeof(int)); if (n != sizeof(int)) { fprintf(stderr, "%s: ERROR failed to read len: %d!=%d" " ... closing connection\n", __func__, n, (int)sizeof(int)); break; } //WHAT DOES THE NEXT LINE DO? len = ntohl(len); if (len) { buf = (char *)malloc(len); n = net_readn(sockfd, buf, len); if ( n != len ) { fprintf(stderr, "%s: ERROR failed to read msg: %d!=%d" " .. closing connection\n" , __func__, n, len); break; } VPRINTF("got: %d '%s'\n", len, buf); net_writen(sockfd, buf, len); free(buf); } } close(sockfd); return; }
extern int proto_session_rcv_msg(Proto_Session *s) { proto_session_reset_receive(s); int ret = net_readn(s->fd, &(s->rhdr), sizeof(Proto_Msg_Hdr)); if (ret == -1) { return -1; } int newblen = ntohl(s->rhdr.blen); printf("newblen = %d\n", newblen); if (newblen != 0) { if (net_readn(s->fd, &(s->rbuf), newblen) == -1) { printf("Failed to read from buffer.\n"); return -1; } s->rlen = newblen; } proto_session_dump(s); if (proto_debug()) { fprintf(stderr, "%p: proto_session_rcv_msg: RCVED:\n", pthread_self()); proto_session_dump(s); } return 1; }
//Assuming that the calling function is responsible for unmarshalling //session passed to us is empty, need to fill it out, return < 0 when //nothing read/to read extern int proto_session_rcv_msg(Proto_Session *s){ proto_session_reset_receive(s); if(net_readn(s->fd, &(s->rhdr), sizeof(Proto_Msg_Hdr)) < 0) return -1; net_readn(s->fd, s->rbuf, ntohl(s->rhdr.blen)); if (proto_debug()){ fprintf(stderr, "%p: proto_session_rcv_msg: RCVED:\n", pthread_self()); proto_session_dump(s); } return 1; }
extern int proto_session_rcv_msg(Proto_Session *s) { proto_session_reset_receive(s); // read reply ////////// ADD CODE ////////// if (PROTO_PRINT_DUMPS==1) fprintf(stderr, "Waiting to receive message\n"); int bytesRead = net_readn(s->fd, &s->rhdr, sizeof(Proto_Msg_Hdr)); // Read the reply header from received message if (PROTO_PRINT_DUMPS==1) fprintf(stderr, "Received %d bytes\n", bytesRead); // Make sure we read the # of bytes we expect if (bytesRead<(int)sizeof(Proto_Msg_Hdr)) { fprintf(stderr, "%s: ERROR failed to read len: %d!=%d" " ... closing connection\n", __func__, bytesRead, (int)sizeof(Proto_Msg_Hdr)); close(s->fd); return -1; } // Get the number of extra bytes in the blen field proto_session_hdr_unmarshall_blen(s); if (PROTO_PRINT_DUMPS==1) fprintf(stderr, "Number of bytes read: %d extraBytes=%d\n", bytesRead, s-> rhdr.blen); // Now read the read into the reply buffer bytesRead = net_readn(s->fd, &s->rbuf, s->rhdr.blen); if ( bytesRead != s->rhdr.blen) { fprintf(stderr, "%s: ERROR failed to read msg: %d!=%d" " .. closing connection\n" , __func__, bytesRead, s->rhdr.blen); close(s->fd); return -1; } ///////////////////// if (proto_debug()) { if(PROTO_PRINT_DUMPS==1) fprintf(stderr, "%p: proto_session_rcv_msg: RCVED:\n", pthread_self()); proto_session_dump(s); } if (PROTO_PRINT_DUMPS==1) fprintf(stderr, "Successfully received message\n" ); return 1; }
extern int proto_session_rcv_msg(Proto_Session *s) { proto_session_reset_receive(s); // read reply // ADD CODE s->rhdr.blen = sizeof (s->sbuf); s->rlen = sizeof (s->rhdr); net_readn(s->fd, &(s->rhdr), s->rlen); net_readn(s->fd, &(s->rbuf), s->rhdr.blen); if (proto_debug()) { fprintf(stderr, "%p: proto_session_rcv_msg: RCVED:\n", pthread_self()); proto_session_dump(s); } return 1; }
extern int proto_session_rcv_msg(Proto_Session *s) { proto_session_reset_receive(s); // read reply if (net_readn(s->fd, &(s->rhdr), sizeof(s->rhdr)) != sizeof(s->rhdr)) return -3; s->rlen = ntohl(s->rhdr.blen); if (s->rlen) { if (s->rlen > PROTO_SESSION_BUF_SIZE || net_readn(s->fd, s->rbuf, s->rlen)!=s->rlen) { return -4; } } if (proto_debug()) { fprintf(stderr, "%p: proto_session_rcv_msg: RCVED:\n", pthread_self()); proto_session_dump(s); } return 1; }