int client_recv_msg_static_socket(int fd, ldcs_message_t *msg, ldcs_read_block_t block) { char help[41]; int rc=0; int n, connfd; if ((fd<0) || (fd>MAX_FD) ) _error("wrong fd"); connfd=ldcs_socket_fdlist[fd].fd; n = _ldcs_read_socket(connfd,&msg->header,sizeof(msg->header), block); if (n == 0) return(rc); if (n < 0) _error("ERROR reading header from socket"); if(msg->header.len>0) { msg->data = (char *) spindle_malloc(msg->header.len); if (!msg) _error("could not allocate memory for message data"); n = _ldcs_read_socket(connfd,msg->data,msg->header.len, LDCS_READ_BLOCK); if (n == 0) return(rc); if (n < 0) _error("ERROR reading message data from socket"); if (n != msg->header.len) _error("received different number of bytes for message data"); } else { msg->data = NULL; } bzero(help,41);if(msg->data) strncpy(help,msg->data,40); debug_printf3("received message of type: %s len=%d data=%s ...\n", _message_type_to_str(msg->header.type), msg->header.len,help ); return(0); }
ldcs_message_t * ldcs_recv_msg_pipe(int fd, ldcs_read_block_t block ) { ldcs_message_t *msg; int n; if ((fd<0) || (fd>fdlist_pipe_size) ) _error("wrong fd"); msg = (ldcs_message_t *) malloc(sizeof(ldcs_message_t)); if (!msg) _error("could not allocate memory for message"); n = _ldcs_read_pipe(fdlist_pipe[fd].in_fd,&msg->header,sizeof(msg->header), block); if (n == 0) { free(msg); return(NULL); } if (n < 0) _error("ERROR reading header from connection"); if(msg->header.len>0) { msg->data = (char *) malloc(msg->header.len); if (!msg) _error("could not allocate memory for message data"); n = _ldcs_read_pipe(fdlist_pipe[fd].in_fd,msg->data,msg->header.len, LDCS_READ_BLOCK); if (n < 0) _error("ERROR reading message data from socket"); if (n != msg->header.len) _error("received different number of bytes for message data"); } else { msg->data = NULL; } debug_printf3("received message of type: %s len=%d data=%s ...\n", _message_type_to_str(msg->header.type), msg->header.len, msg->data ); return(msg); }
int ldcs_recv_msg_static_pipe(int fd, ldcs_message_t *msg, ldcs_read_block_t block) { int n; int rc=0; msg->header.type=LDCS_MSG_UNKNOWN; msg->header.len=0; if ((fd<0) || (fd>fdlist_pipe_size) ) _error("wrong fd"); n = _ldcs_read_pipe(fdlist_pipe[fd].in_fd,&msg->header,sizeof(msg->header), block); if (n == 0) { /* Disconnect. Return an artificial client end message */ debug_printf2("Client disconnected. Returning END message\n"); msg->header.type = LDCS_MSG_END; msg->header.len = 0; msg->data = NULL; return(rc); } if (n < 0) _error("ERROR reading header from connection"); if(msg->header.len>0) { n = _ldcs_read_pipe(fdlist_pipe[fd].in_fd,msg->data,msg->header.len, LDCS_READ_BLOCK); if (n == 0) return(rc); if (n < 0) { int error = errno; err_printf("Error during read of pipe: %s (%d)\n", strerror(error), error); return -1; } if (n != msg->header.len) { int error = errno; err_printf("Partial read on pipe. Got %u / %u: %s (%d)\n", (unsigned) n, (unsigned) msg->header.len, strerror(error), error); return -1; } } else { *msg->data = '\0'; } debug_printf3("received message of type: %s len=%d data=%s ...\n", _message_type_to_str(msg->header.type), msg->header.len, msg->data ); return(rc); }
/* ************************************************************** */ int ldcs_send_msg_pipe(int fd, ldcs_message_t * msg) { int n; if ((fd<0) || (fd>fdlist_pipe_size) ) _error("wrong fd"); debug_printf3("sending message of type: %s len=%d data=%s ...\n", _message_type_to_str(msg->header.type), msg->header.len,msg->data ); n = _ldcs_write_pipe(fdlist_pipe[fd].out_fd,&msg->header,sizeof(msg->header)); if (n < 0) _error("ERROR writing header to pipe"); if(msg->header.len>0) { n = _ldcs_write_pipe(fdlist_pipe[fd].out_fd,(void *) msg->data,msg->header.len); if (n < 0) _error("ERROR writing data to pipe"); if (n != msg->header.len) _error("sent different number of bytes for message data"); } return(0); }
int client_send_msg_socket(int fd, ldcs_message_t * msg) { char help[41]; int n, connfd; if ((fd<0) || (fd>MAX_FD) ) _error("wrong fd"); connfd=ldcs_socket_fdlist[fd].fd; bzero(help,41);if(msg->data) strncpy(help,msg->data,40); debug_printf3("sending message of type: %s len=%d data=%s ...\n", _message_type_to_str(msg->header.type), msg->header.len,help ); n = _ldcs_write_socket(connfd,&msg->header,sizeof(msg->header)); if (n < 0) _error("ERROR writing header to socket"); if(msg->header.len>0) { n = _ldcs_write_socket(connfd,(void *) msg->data,msg->header.len); if (n < 0) _error("ERROR writing data to socket"); } return(0); }