Commit c4de22c34838c29abc6dcc5fdd31b350f4c1fd9f

Authored by Georg Hopp
1 parent 42d139f3

Make the raw protocol to a real protocol. It sends the message size now before t…

…he message. This was neccessary because after I activated optimization in gcc the testclient was always only reading 2 bytes in the initial successfull read.
@@ -24,6 +24,7 @@ @@ -24,6 +24,7 @@
24 #define __TR_PROTOCOL_MESSAGE_RAW_H__ 24 #define __TR_PROTOCOL_MESSAGE_RAW_H__
25 25
26 #include <sys/types.h> 26 #include <sys/types.h>
  27 +#include <stdint.h>
27 28
28 #include "trbase.h" 29 #include "trbase.h"
29 #include "trio.h" 30 #include "trio.h"
@@ -33,7 +34,9 @@ @@ -33,7 +34,9 @@
33 TR_CLASS(TR_ProtoMessageRaw) { 34 TR_CLASS(TR_ProtoMessageRaw) {
34 TR_EXTENDS(TR_ProtoMessage); 35 TR_EXTENDS(TR_ProtoMessage);
35 36
36 - TR_RemoteData data; 37 + uint16_t size;
  38 + uint16_t size_done;
  39 + char * data;
37 }; 40 };
38 TR_INSTANCE_INIT(TR_ProtoMessageRaw); 41 TR_INSTANCE_INIT(TR_ProtoMessageRaw);
39 TR_CLASSVARS_DECL(TR_ProtoMessageRaw) {}; 42 TR_CLASSVARS_DECL(TR_ProtoMessageRaw) {};
@@ -35,7 +35,9 @@ protoMessageRawCtor(void * _this, va_list * params) @@ -35,7 +35,9 @@ protoMessageRawCtor(void * _this, va_list * params)
35 return 0; 35 return 0;
36 } 36 }
37 37
38 -static void protoMessageRawDtor(void * _this) {} 38 +static void protoMessageRawDtor(void * _this) {
  39 + TR_MEM_FREE(((TR_ProtoMessageRaw)_this)->data);
  40 +}
39 41
40 TR_INIT_IFACE(TR_Class, protoMessageRawCtor, protoMessageRawDtor, NULL); 42 TR_INIT_IFACE(TR_Class, protoMessageRawCtor, protoMessageRawDtor, NULL);
41 TR_CREATE_CLASS(TR_ProtoMessageRaw, TR_ProtoMessage, NULL, TR_IF(TR_Class)); 43 TR_CREATE_CLASS(TR_ProtoMessageRaw, TR_ProtoMessage, NULL, TR_IF(TR_Class));
@@ -22,7 +22,10 @@ @@ -22,7 +22,10 @@
22 22
23 #include <stdarg.h> 23 #include <stdarg.h>
24 24
  25 +#include <arpa/inet.h>
25 #include <sys/types.h> 26 #include <sys/types.h>
  27 +#include <stdint.h>
  28 +#include <string.h>
26 29
27 #include "trbase.h" 30 #include "trbase.h"
28 #include "trio.h" 31 #include "trio.h"
@@ -49,21 +52,69 @@ protocolRawCreateMessage(void * _this, TR_Socket remote) @@ -49,21 +52,69 @@ protocolRawCreateMessage(void * _this, TR_Socket remote)
49 52
50 static 53 static
51 TR_RemoteData 54 TR_RemoteData
52 -protocolRawParse(void * _this, TR_ProtoMessage _message, TR_RemoteData data) 55 +protocolRawParse(void * _this, TR_ProtoMessage _message, TR_RemoteData _data)
53 { 56 {
54 - TR_ProtoMessageRaw message = (TR_ProtoMessageRaw)_message; 57 + TR_ProtoMessageRaw message = (TR_ProtoMessageRaw)_message;
  58 + size_t size = ((TR_SizedData)_data)->size;
  59 + char * data = ((TR_SizedData)_data)->data;
  60 + TR_RemoteData retval = NULL;
  61 +
  62 + if (! message->data) {
  63 + if (message->size == 0) {
  64 + if (size == 1) {
  65 + message->size = *(char *)data << 8;
  66 + TR_delete(_data);
  67 + return NULL;
  68 + } else {
  69 + message->size = *(uint16_t *)data;
  70 + size -= 2;
  71 + data += 2;
  72 + }
  73 + } else {
  74 + message->size &= *(char *)data;
  75 + size--;
  76 + data++;
  77 + }
  78 +
  79 + message->size = ntohs(message->size);
  80 + message->data = TR_malloc(message->size);
  81 + }
  82 +
  83 + if (size >= message->size - message->size_done) {
  84 + memcpy(message->data, data, message->size - message->size_done);
  85 + size -= message->size - message->size_done;
  86 + data += message->size - message->size_done;
  87 + message->size_done = message->size;
  88 + _message->ready = 1;
55 89
56 - message->data = data;  
57 - _message->ready = 1; 90 + if (size) {
  91 + retval = TR_new(TR_RemoteData, data, size, _data->remote);
  92 + }
  93 + } else {
  94 + memcpy(message->data, data, size);
  95 + message->size_done = size;
  96 + }
58 97
59 - return NULL; 98 + TR_delete(_data);
  99 +
  100 + return retval;
60 } 101 }
61 102
62 static 103 static
63 TR_RemoteData 104 TR_RemoteData
64 protocolRawCompose(void * _this, TR_ProtoMessage _message) 105 protocolRawCompose(void * _this, TR_ProtoMessage _message)
65 { 106 {
66 - return ((TR_ProtoMessageRaw)_message)->data; 107 + TR_ProtoMessageRaw message = (TR_ProtoMessageRaw)_message;
  108 + TR_SizedData data;
  109 +
  110 + data = (TR_SizedData)TR_new(TR_RemoteData, NULL, 0, _message->remote);
  111 +
  112 + data->size = message->size + 2;
  113 + data->data = TR_malloc(data->size);
  114 + *(uint16_t *)data->data = htons(message->size);
  115 + memcpy(data->data+2, message->data, message->size);
  116 +
  117 + return (TR_RemoteData)data;
67 } 118 }
68 119
69 TR_INIT_IFACE(TR_Class, protocolRawCtor, protocolRawDtor, NULL); 120 TR_INIT_IFACE(TR_Class, protocolRawCtor, protocolRawDtor, NULL);
@@ -45,7 +45,7 @@ testHandlerNewMessage(TR_EventHandler this, TR_Event event) @@ -45,7 +45,7 @@ testHandlerNewMessage(TR_EventHandler this, TR_Event event)
45 TR_ProtoMessageRaw message = event->data; 45 TR_ProtoMessageRaw message = event->data;
46 46
47 ((TestHandler)this)->handled++; 47 ((TestHandler)this)->handled++;
48 - ((TestHandler)this)->size += ((TR_SizedData)message->data)->size; 48 + ((TestHandler)this)->size += message->size + 2;
49 49
50 _event = TR_eventSubjectEmit( 50 _event = TR_eventSubjectEmit(
51 event->subject, 51 event->subject,
@@ -20,7 +20,7 @@ main (int argc, char * argv[]) @@ -20,7 +20,7 @@ main (int argc, char * argv[])
20 TR_SimpleClient client; 20 TR_SimpleClient client;
21 TR_Protocol protocol; 21 TR_Protocol protocol;
22 TR_ProtoMessageRaw message; 22 TR_ProtoMessageRaw message;
23 - TR_RemoteData data; 23 + int i;
24 24
25 TR_logger = TR_INSTANCE_CAST(TR_Logger, mylogger2); 25 TR_logger = TR_INSTANCE_CAST(TR_Logger, mylogger2);
26 socket = TR_new(TR_TcpSocket, TR_logger, "192.168.2.13", 5678, 0); 26 socket = TR_new(TR_TcpSocket, TR_logger, "192.168.2.13", 5678, 0);
@@ -32,17 +32,32 @@ main (int argc, char * argv[]) @@ -32,17 +32,32 @@ main (int argc, char * argv[])
32 32
33 client = TR_new(TR_SimpleClient, connection); 33 client = TR_new(TR_SimpleClient, connection);
34 34
35 - message = (TR_ProtoMessageRaw)TR_protoCreateRequest(protocol, (TR_Socket)socket);  
36 - data = TR_new(TR_RemoteData, "test", sizeof("test"), (TR_Socket)socket);  
37 - message->data = data;  
38 - message = (TR_ProtoMessageRaw)TR_simpleClientIssue(  
39 - client,  
40 - (TR_ProtoMessage)message,  
41 - 10000000);  
42 -  
43 - printf("%s\n", ((TR_SizedData)message->data)->data);  
44 - TR_delete(message->data);  
45 - TR_delete(message); 35 + for (i=0; i<100000; i++) {
  36 + message = (TR_ProtoMessageRaw)TR_protoCreateRequest(
  37 + protocol, (TR_Socket)socket);
  38 + message->size = sizeof("test");
  39 + message->data = TR_malloc(message->size);
  40 + memcpy(message->data, "test", sizeof("test"));
  41 +
  42 + message = (TR_ProtoMessageRaw)TR_simpleClientIssue(
  43 + client,
  44 + (TR_ProtoMessage)message,
  45 + 10000000);
  46 +
  47 +#if 0
  48 + printf("%s\n", message->data);
  49 +#else
  50 + if (0 == strncmp("test", message->data, sizeof("test")-1)) {
  51 + if (i % 1000 == 0) printf("%c", '.');
  52 + } else {
  53 + printf("%c", 'f');
  54 + }
  55 +#endif
  56 + fflush(stdout);
  57 +
  58 + TR_delete(message);
  59 + }
  60 + puts("");
46 61
47 puts("cleanup..."); 62 puts("cleanup...");
48 63
Please register or login to post a comment