Commit 6e48e39f authored by Lubomir Bulej's avatar Lubomir Bulej

Drop message structure from message channel, use only byte buffer.

parent 7462078f
...@@ -5,54 +5,33 @@ ...@@ -5,54 +5,33 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/types.h>
#ifndef MINGW #ifndef MINGW
#include <sys/uio.h> #include <sys/uio.h>
#endif #endif
#include <jni.h>
// //
#ifdef MINGW #ifdef MINGW
static inline ssize_t static inline ssize_t
__send (struct connection * conn, struct message * msg, void * header, const size_t header_size) { __send (struct connection * conn, void * header, const size_t header_size, void * body, const size_t body_size) {
ssize_t sent = connection_send (conn, header, header_size); ssize_t sent = connection_send_full (conn, header, header_size);
sent += connection_send (conn, msg->control, msg->control_size); sent += connection_send_full (conn, body, body_size);
sent += connection_send (conn, msg->classcode, msg->classcode_size);
return sent; return sent;
} }
static inline void
__recv (struct connection * conn, void * control, const size_t control_size, void * classcode, const size_t classcode_size) {
connection_recv (conn, control, control_size);
connection_recv (conn, classcode, classcode_size);
}
#else #else
static inline ssize_t static inline ssize_t
__send (struct connection * conn, struct message * msg, void * header, const size_t header_size) { __send (struct connection * conn, void * header, const size_t header_size, void * body, const size_t body_size) {
struct iovec iovs [] = { struct iovec iovs [] = {
{ .iov_base = header, .iov_len = header_size }, { .iov_base = header, .iov_len = header_size },
{ .iov_base = (void *) msg->control, .iov_len = msg->control_size }, { .iov_base = body, .iov_len = body_size },
{ .iov_base = (void *) msg->classcode, .iov_len = msg->classcode_size }
};
return connection_send_iov (conn, &iovs [0], sizeof_array (iovs));
}
static inline void
__recv (struct connection * conn, void * control, const size_t control_size, void * classcode, const size_t classcode_size) {
struct iovec iovs [2] = {
{ .iov_base = control, .iov_len = control_size },
{ .iov_base = classcode, .iov_len = classcode_size }
}; };
connection_recv_iov (conn, &iovs [0], sizeof_array (iovs)); return connection_send_iov_full (conn, &iovs [0], sizeof_array (iovs));
} }
#endif /* !MINGW */ #endif /* !MINGW */
...@@ -63,29 +42,22 @@ __recv (struct connection * conn, void * control, const size_t control_size, voi ...@@ -63,29 +42,22 @@ __recv (struct connection * conn, void * control, const size_t control_size, voi
* via the given connection. Returns the number of bytes sent. * via the given connection. Returns the number of bytes sent.
*/ */
ssize_t ssize_t
message_send (struct connection * conn, struct message * msg) { message_send (struct connection * conn, void * message, const size_t message_size) {
assert (conn != NULL); assert (conn != NULL);
assert (msg != NULL); assert (message != NULL);
ldebug ( ldebug ("sending message: %lu bytes", message_size);
"sending message: flags %08x, control %d, code %d ... ",
msg->message_flags, msg->control_size, msg->classcode_size
);
// //
jint ints [] = { uint32_t message_size_data = htonl (message_size);
htonl (msg->message_flags), size_t header_size = sizeof (message_size_data);
htonl (msg->control_size), ssize_t sent = __send (conn, &message_size_data, header_size, message, message_size);
htonl (msg->classcode_size) assert (sent == (ssize_t) (header_size + message_size));
};
ssize_t sent = __send (conn, msg, &ints [0], sizeof (ints));
assert (sent == (ssize_t) (sizeof (ints) + msg->control_size + msg->classcode_size));
// //
debug ("sent %ld bytes ... done\n", sent); debug (", sent %ld bytes\n", sent);
return sent; return sent;
} }
...@@ -114,45 +86,35 @@ __alloc_buffer (size_t len) { ...@@ -114,45 +86,35 @@ __alloc_buffer (size_t len) {
/** /**
* Receives a message from the remote instrumentation server. * Receives a message from the remote instrumentation server.
*/ */
void ssize_t
message_recv (struct connection * conn, struct message * msg) { message_recv (struct connection * conn, void ** message_ptr) {
assert (conn != NULL); assert (conn != NULL);
assert (msg != NULL); assert (message_ptr != NULL);
ldebug ("receiving message: "); ldebug ("receiving message: ");
// //
// First, receive the flags, the control and class code sizes. // First receive the size of the message.
// Second, receive the control and class code data. // Then receive the message itself.
// The ordering of receive calls is determined by the protocol.
// //
jint ints [3]; uint32_t message_size_data;
connection_recv (conn, &ints [0], sizeof (ints)); connection_recv_full (conn, &message_size_data, sizeof (message_size_data));
jint response_flags = ntohl (ints [0]); size_t message_size = ntohl (message_size_data);
debug ("expecting %ld bytes", message_size);
// //
// The message body can be completely empty. In this case,
jint control_size = ntohl (ints [1]); // the buffer allocator will just return NULL and we avoid
uint8_t * control = __alloc_buffer (control_size); // reading from the socket. We will set the message pointer
// to NULL and return zero as message size.
jint classcode_size = ntohl (ints [2]);
uint8_t * classcode = __alloc_buffer (classcode_size);
__recv (conn, control, control_size, classcode, classcode_size);
//
// Update message fields only after the whole message was read.
// //
msg->message_flags = response_flags; uint8_t * message = __alloc_buffer (message_size);
msg->control_size = control_size; connection_recv_full (conn, message, message_size);
msg->classcode_size = classcode_size;
msg->control = control;
msg->classcode = classcode;
// //
// Return the message only after the whole message was read.
debug ( //
"flags %08x, control %d, code %d ... done\n", debug ("... done\n");
response_flags, control_size, classcode_size * message_ptr = (void *) message;
); return message_size;
} }
#ifndef _MSGCHANNEL_H_ #ifndef _MSGCHANNEL_H_
#define _MSGCHANNEL_H_ #define _MSGCHANNEL_H_
#include <jni.h>
#include "connection.h" #include "connection.h"
ssize_t message_send (struct connection * conn, void * message, const size_t message_size);
struct message { ssize_t message_recv (struct connection * conn, void ** message_ptr);
jint message_flags;
jint control_size;
jint classcode_size;
const uint8_t * control;
const uint8_t * classcode;
};
ssize_t message_send (struct connection * conn, struct message * msg);
void message_recv (struct connection * conn, struct message * msg);
#endif /* _MSGCHANNEL_H_ */ #endif /* _MSGCHANNEL_H_ */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment