static stomp_frame *handle_send_frame(stomp *stp, stomp_frame *f) { stomp_frame *rf = NULL; char *dest_name = get_header(f, "destination"); if(dest_name == NULL) { rf = create_error_frame("header missed", "Send frame should contain a destination header."); } else { stomp_frame *frame = create_message_frame(dest_name, get_body(f)); send_response_frame_to_destination(dest_name, frame); stomp_frame_free(frame); } return rf; }
struct message * recv_message(int sockfd){ /* * Caller is required to de-allocate the pointer to the message, and the message data * Returns NULL if the remote host closed the connection. */ struct message * received_message = create_message_frame(0,(enum message_type)0,0); int bytes_received; //printf("Attempting to receive message length data (%d bytes)...\n", (int)sizeof(int)); bytes_received = recv(sockfd, &(received_message->length), sizeof(int), 0); if (bytes_received <= 0) { // node on the other end terminated, return here to indicate that the connection should close destroy_message_frame(received_message); return 0; } /* Probably never going to happen, but make sure we got the whole int */ assert(bytes_received == sizeof(int)); received_message->length = ntohl(received_message->length); /* The received_message data should be a bunch of ints, otherwise how do we convert to host order? */ assert(received_message->length % sizeof(int) == 0); /* Allocate space for the rest of the received_message */ received_message->data = (int *) malloc(received_message->length); //printf("Attempting to receive message type data (%d bytes)...\n", (int)sizeof(int)); bytes_received = recv(sockfd, &(received_message->type), sizeof(int), 0); if (bytes_received <= 0) { // node on the other end terminated, return here to indicate that the connection should close destroy_message_frame(received_message); return 0; } if (bytes_received == -1) perror("Error in recv_message getting type."); assert(bytes_received != 0 && "Connection was closed by remote host."); /* Probably never going to happen, but make sure we got the whole int */ assert(bytes_received == sizeof(int)); received_message->type = (enum message_type)ntohl(received_message->type); if(received_message->length > 0){ bytes_received = recv(sockfd, received_message->data, received_message->length, 0); if (bytes_received <= 0) { // node on the other end terminated, return here to indicate that the connection should close destroy_message_frame(received_message); return 0; } if (bytes_received != received_message->length) { destroy_message_frame(received_message); return 0; } u_int i; for (i = 0; i < received_message->length % sizeof(int); i++) { received_message->data[i] = ntohl(received_message->data[i]); } } return received_message; }