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; }
static int proto_client_session_lost_default_hdlr(Proto_Session *s) { fprintf(stderr, "Session lost...:\n"); proto_session_dump(s); return -1; }
// rc < 0 on comm failures // rc == 1 indicates comm success extern int proto_session_send_msg(Proto_Session *s, int reset) { s->shdr.blen = htonl(s->slen); // write request if (net_writen(s->fd, &(s->shdr), sizeof(s->shdr)) != sizeof(s->shdr)) return -1; if (s->slen) { if (net_writen(s->fd, s->sbuf, s->slen) != s->slen) { return -2; } } if (proto_debug()) { fprintf(stderr, "%p: proto_session_send_msg: SENT:\n", pthread_self()); proto_session_dump(s); } // communication was successfull if (reset) proto_session_reset_send(s); return 1; }
static int proto_client_event_null_handler(Proto_Session *s) { fprintf(stderr, "proto_client_event_null_handler: invoked for session:\n"); 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_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; }
// rc < 0 on comm failures // rc == 1 indicates comm success // session passed to us contains header and body information, // need to send that information to fd extern int proto_session_send_msg(Proto_Session *s, int reset){ s->shdr.blen = htonl(s->slen); if(net_writen(s->fd, &(s->shdr), sizeof(Proto_Msg_Hdr)) < 0) return -1; net_writen(s->fd, s->sbuf, s->slen); if (proto_debug()) { fprintf(stderr, "%p: proto_session_send_msg: SENT:\n", pthread_self()); proto_session_dump(s); } if (reset) proto_session_reset_send(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; }
// rc < 0 on comm failures // rc == 1 indicates comm success extern int proto_session_send_msg(Proto_Session *s, int reset) { s->shdr.blen = htonl(s->slen); // write request ADD CODE if (proto_debug()) { fprintf(stderr, "%p: proto_session_send_msg: SENT:\n", pthread_self()); proto_session_dump(s); } // communication was successfull if (reset) proto_session_reset_send(s); 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; }
// rc < 0 on comm failures // rc == 1 indicates comm success extern int proto_session_send_msg(Proto_Session *s, int reset) { s->shdr.blen = htonl(s->slen); // write request // ADD CODE net_writen(s->fd, &s->shdr, (int)sizeof(Proto_Msg_Hdr)); if (s->slen>0) { net_writen(s->fd, &s->sbuf, (int)s->slen); if (PROTO_PRINT_DUMPS==1) fprintf(stderr, "Sending extra bytes: %d\n", s->slen); } if (proto_debug()) { fprintf(stderr, "%p: proto_session_send_msg: SENT:\n", pthread_self()); proto_session_dump(s); } if (PROTO_PRINT_DUMPS==1) fprintf(stderr, "Sent message...\n"); // //////////// // struct sockaddr_in sinadd; // socklen_t len = sizeof(sinadd); // if (getsockname(s->fd, (struct sockaddr *)&sinadd, &len) == -1) // perror("getsockname"); // else // printf("port number %d\n", ntohs(sinadd.sin_port)); // char hostname[128]; // gethostname(hostname, sizeof hostname); // printf("My hostname: %s\n", hostname); // //////////// // communication was successfull if (reset) proto_session_reset_send(s); return 1; }
static int proto_server_mt_null_handler(Proto_Session *s){ int rc=1; Proto_Msg_Hdr h; fprintf(stderr, "proto_server_mt_null_handler: invoked for session:\n"); proto_session_dump(s); // setup dummy reply header : set correct reply message type and // everything else empty bzero(&h, sizeof(h)); h.type = proto_session_hdr_unmarshall_type(s); h.type += PROTO_MT_REP_BASE_RESERVED_FIRST; proto_session_hdr_marshall(s, &h); // setup a dummy body that just has a return code proto_session_body_marshall_int(s, 0xdeadbeef); rc=proto_session_send_msg(s,1); return rc; }
extern int proto_session_send_msg(Proto_Session *s, int reset) { //int n; s->shdr.blen = sizeof (s->sbuf); s->slen = sizeof (s->shdr); net_writen(s->fd, &(s->shdr), s->slen); net_writen(s->fd, &(s->sbuf), s->shdr.blen); // write request // ADD CODE if (proto_debug()) { fprintf(stderr, "%p: proto_session_send_msg: SENT:\n", pthread_self()); proto_session_dump(s); } // communication was successfull if (reset) proto_session_reset_send(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; }