Browse Source

fix some memory leaks

1.0.0
Georg Hopp 11 years ago
parent
commit
a3f209e521
  1. 5
      include/tr/interface/protocol.h
  2. 21
      src/connection.c
  3. 4
      src/i_protocol.c
  4. 5
      src/io_handler.c
  5. 17
      src/protocol_handler.c
  6. 4
      src/protocol_raw.c
  7. 44
      testers/test_handler.c
  8. 8
      testers/testclient.sh
  9. 3
      testers/testserver2.c

5
include/tr/interface/protocol.h

@ -35,7 +35,8 @@ typedef TR_ProtoMessage (* fptr_TR_protoCreateMessage)(void *);
typedef TR_ProtoMessage (* fptr_TR_protoCreateRequest)(void *, va_list *); typedef TR_ProtoMessage (* fptr_TR_protoCreateRequest)(void *, va_list *);
typedef TR_ProtoMessage (* fptr_TR_protoCreateResponse)(void *, va_list *); typedef TR_ProtoMessage (* fptr_TR_protoCreateResponse)(void *, va_list *);
typedef TR_RemoteData (* fptr_TR_protoCompose)(void *, TR_ProtoMessage); typedef TR_RemoteData (* fptr_TR_protoCompose)(void *, TR_ProtoMessage);
typedef size_t (* fptr_TR_protoParse)(void *, TR_ProtoMessage, TR_RemoteData);
typedef TR_RemoteData (* fptr_TR_protoParse)(
void *, TR_ProtoMessage, TR_RemoteData);
TR_INTERFACE(TR_Protocol) { TR_INTERFACE(TR_Protocol) {
TR_IFID; TR_IFID;
@ -51,7 +52,7 @@ TR_ProtoMessage TR_vprotoCreateRequest(void *, TR_Socket, va_list*);
TR_ProtoMessage TR_protoCreateRequest(void *, TR_Socket, ...); TR_ProtoMessage TR_protoCreateRequest(void *, TR_Socket, ...);
TR_ProtoMessage TR_vprotoCreateResponse(void *, TR_Socket, va_list*); TR_ProtoMessage TR_vprotoCreateResponse(void *, TR_Socket, va_list*);
TR_ProtoMessage TR_protoCreateResponse(void *, TR_Socket, ...); TR_ProtoMessage TR_protoCreateResponse(void *, TR_Socket, ...);
size_t TR_protoParse(void *, TR_ProtoMessage, TR_RemoteData);
TR_RemoteData TR_protoParse(void *, TR_ProtoMessage, TR_RemoteData);
TR_RemoteData TR_protoCompose(void *, TR_ProtoMessage); TR_RemoteData TR_protoCompose(void *, TR_ProtoMessage);
#endif // __TR_INTERFACE_PROTOCOL_H__ #endif // __TR_INTERFACE_PROTOCOL_H__

21
src/connection.c

@ -63,7 +63,6 @@ connectionNextMessage(void * _this, TR_RemoteData * data)
TR_CommEndPoint comm = _this; TR_CommEndPoint comm = _this;
TR_ProtoMessage ret_message = NULL; TR_ProtoMessage ret_message = NULL;
TR_RemoteData new_data = NULL; TR_RemoteData new_data = NULL;
size_t end;
if (*data) { if (*data) {
if (! this->current_message || this->current_message->ready) if (! this->current_message || this->current_message->ready)
@ -72,7 +71,15 @@ connectionNextMessage(void * _this, TR_RemoteData * data)
TR_protoCreateMessage(comm->protocol, (*data)->remote); TR_protoCreateMessage(comm->protocol, (*data)->remote);
} }
end = TR_protoParse(comm->protocol, this->current_message, *data);
/*
* This will return NULL if all data was consumed or a new TR_RemoteData
* with the remaining data.
* In other words, the protocol implementatio is completely responsible
* for the data management here. It can decide to copy the data, but
* then has to free the given TR_RemoteData by itself. This gives most
* flexibility when writing a protocol.
*/
new_data = TR_protoParse(comm->protocol, this->current_message, *data);
/** /**
* We define that the only valid reason for a protocol parser to not * We define that the only valid reason for a protocol parser to not
@ -98,18 +105,12 @@ connectionNextMessage(void * _this, TR_RemoteData * data)
* true and we should make it an ERROR). * true and we should make it an ERROR).
*/ */
if (this->current_message->ready) { if (this->current_message->ready) {
if (end != ((TR_SizedData)*data)->size) {
new_data = TR_new(
TR_RemoteData,
((TR_SizedData)*data)->data + end,
((TR_SizedData)*data)->size - end,
(*data)->remote);
}
ret_message = this->current_message; ret_message = this->current_message;
this->current_message = NULL; this->current_message = NULL;
} else { } else {
if (end != ((TR_SizedData)*data)->size) {
if (new_data) {
TR_delete(*data); TR_delete(*data);
TR_delete(new_data);
TR_loggerLog( TR_loggerLog(
TR_logger, TR_logger,
TR_LOGGER_WARNING, TR_LOGGER_WARNING,

4
src/i_protocol.c

@ -85,10 +85,10 @@ TR_protoCreateResponse(void * _this, TR_Socket remote, ...)
return callret; return callret;
} }
size_t
TR_RemoteData
TR_protoParse(void * _this, TR_ProtoMessage message, TR_RemoteData data) TR_protoParse(void * _this, TR_ProtoMessage message, TR_RemoteData data)
{ {
size_t callret;
TR_RemoteData callret;
TR_RETCALL(_this, TR_Protocol, parse, callret, message, data); TR_RETCALL(_this, TR_Protocol, parse, callret, message, data);
return callret; return callret;
} }

5
src/io_handler.c

@ -78,6 +78,11 @@ ioHandlerRead(void * _this, TR_Event event)
return TR_EVENT_DONE; return TR_EVENT_DONE;
case TRUE: case TRUE:
if (event->subject->fin) {
TR_delete(data);
return TR_EVENT_DONE;
}
revent = TR_eventSubjectEmit( revent = TR_eventSubjectEmit(
event->subject, event->subject,
TR_CEP_EVENT_NEW_DATA, TR_CEP_EVENT_NEW_DATA,

17
src/protocol_handler.c

@ -21,6 +21,7 @@
*/ */
#include <unistd.h> #include <unistd.h>
#include <inttypes.h>
#include "trbase.h" #include "trbase.h"
#include "trevent.h" #include "trevent.h"
@ -55,17 +56,19 @@ protocolHandlerParse(void * _this, TR_Event event)
TR_ProtoMessage message; TR_ProtoMessage message;
while ((message = TR_cepNextMessage(endpoint, &data))) { while ((message = TR_cepNextMessage(endpoint, &data))) {
TR_eventHandlerIssueEvent(
if (! TR_eventHandlerIssueEvent(
(TR_EventHandler)_this, (TR_EventHandler)_this,
TR_eventSubjectEmit( TR_eventSubjectEmit(
event->subject, event->subject,
TR_CEP_EVENT_NEW_MSG, TR_CEP_EVENT_NEW_MSG,
message));
if (message->close) {
// also check that we are a response. Well this is
// how it is done in the python code...
TR_cepSetClose(endpoint);
message))) {
TR_delete(message);
} else {
if (message->close) {
// also check that we are a response. Well this is
// how it is done in the python code...
TR_cepSetClose(endpoint);
}
} }
} }

4
src/protocol_raw.c

@ -48,7 +48,7 @@ protocolRawCreateMessage(void * _this, TR_Socket remote)
} }
static static
size_t
TR_RemoteData
protocolRawParse(void * _this, TR_ProtoMessage _message, TR_RemoteData data) protocolRawParse(void * _this, TR_ProtoMessage _message, TR_RemoteData data)
{ {
TR_ProtoMessageRaw message = (TR_ProtoMessageRaw)_message; TR_ProtoMessageRaw message = (TR_ProtoMessageRaw)_message;
@ -56,7 +56,7 @@ protocolRawParse(void * _this, TR_ProtoMessage _message, TR_RemoteData data)
message->data = data; message->data = data;
_message->ready = 1; _message->ready = 1;
return ((TR_SizedData)data)->size;
return NULL;
} }
static static

44
testers/test_handler.c

@ -11,7 +11,7 @@ static
TR_EventDone TR_EventDone
testHandlerHeartbeat(TR_EventHandler this, TR_Event event) testHandlerHeartbeat(TR_EventHandler this, TR_Event event)
{ {
printf("%zd beats since last beat / handled: %llu/s\n",
printf("%zd beat(s) since last beat / handled: %llu/s\n",
((TR_EventDispatcher)event->subject)->n_beats, ((TR_EventDispatcher)event->subject)->n_beats,
((TestHandler)this)->handled); ((TestHandler)this)->handled);
((TestHandler)this)->handled = 0; ((TestHandler)this)->handled = 0;
@ -23,22 +23,10 @@ static
TR_EventDone TR_EventDone
testHandlerNewMessage(TR_EventHandler this, TR_Event event) testHandlerNewMessage(TR_EventHandler this, TR_Event event)
{ {
// TR_ProtoMessageRaw msg = event->data;
// TR_SizedData data = (TR_SizedData)msg->data;
// char buf[data->size + 1];
// int i;
TR_Event _event; TR_Event _event;
((TestHandler)this)->handled++; ((TestHandler)this)->handled++;
// printf("handled data %p\n", event->data);
// memcpy(buf, data->data, data->size);
// buf[data->size] = 0;
// for (i = 0; buf[i]; i++) {
// if (! isprint(buf[i])) buf[i] = '.';
// }
// printf("echo message: %s(%zd)\n", buf, data->size);
_event = TR_eventSubjectEmit( _event = TR_eventSubjectEmit(
event->subject, event->subject,
TR_CEP_EVENT_MSG_READY, TR_CEP_EVENT_MSG_READY,
@ -58,14 +46,16 @@ testHandlerClose(TR_EventHandler this, TR_Event event)
return TR_EVENT_PENDING; return TR_EVENT_PENDING;
} }
//static
//TR_EventDone
//testHandlerUpgrade(TR_EventHandler this, TR_Event event)
//{
// printf("upgrade: %"PRIdPTR"\n", event->id);
//
// return TR_EVENT_PENDING;
//}
#if 0
static
TR_EventDone
testHandlerUpgrade(TR_EventHandler this, TR_Event event)
{
printf("upgrade: %"PRIdPTR"\n", event->id);
return TR_EVENT_PENDING;
}
#endif
static static
int int
@ -103,11 +93,13 @@ testHandlerCvInit(TR_class_ptr class)
TR_CommEndPoint, TR_CommEndPoint,
TR_CEP_EVENT_CLOSE, TR_CEP_EVENT_CLOSE,
testHandlerClose); testHandlerClose);
// TR_EVENT_HANDLER_SET_METHOD(
// class,
// TR_CommEndPoint,
// TR_CEP_EVENT_UPGRADE,
// testHandlerUpgrade);
#if 0
TR_EVENT_HANDLER_SET_METHOD(
class,
TR_CommEndPoint,
TR_CEP_EVENT_UPGRADE,
testHandlerUpgrade);
#endif
} }
TR_INSTANCE(TR_Hash, testHandlerEventMethods); TR_INSTANCE(TR_Hash, testHandlerEventMethods);

8
testers/testclient.sh

@ -1,9 +1,9 @@
#!/bin/sh #!/bin/sh
BS=8192 BS=8192
COUNT=10000
CONCURENT=200
IP="192.168.2.13"
COUNT=1000000
CONCURENT=20
IP="localhost"
pids="" pids=""
i=0 i=0
@ -12,7 +12,7 @@ MESSAGE="GET / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n"
while [ $i -lt ${CONCURENT} ] while [ $i -lt ${CONCURENT} ]
do do
dd if=/dev/zero bs=${BS} count=${COUNT} | nc -q 1 ${IP} 5678 >/dev/null & dd if=/dev/zero bs=${BS} count=${COUNT} | nc -q 1 ${IP} 5678 >/dev/null &
#echo -en "${MESSAGE}" | nc -q 1 ${IP} 5678 &
#echo -en "${MESSAGE}" | nc -q 1 -u ${IP} 5678 &
pids="${pids} $!" pids="${pids} $!"
i=$((i + 1)) i=$((i + 1))

3
testers/testserver2.c

@ -10,6 +10,7 @@
#include "test_handler.h" #include "test_handler.h"
TR_INSTANCE(TR_LoggerSyslog, mylogger, {TR_LOGGER_INFO}); TR_INSTANCE(TR_LoggerSyslog, mylogger, {TR_LOGGER_INFO});
TR_INSTANCE(TR_LoggerStderr, mylogger2, {TR_LOGGER_INFO});
int int
main (int argc, char * argv[]) main (int argc, char * argv[])
@ -18,7 +19,7 @@ main (int argc, char * argv[])
TR_Protocol protocol = TR_new(TR_ProtocolRaw); TR_Protocol protocol = TR_new(TR_ProtocolRaw);
TestHandler test_handler = TR_new(TestHandler); TestHandler test_handler = TR_new(TestHandler);
TR_logger = TR_INSTANCE_CAST(TR_Logger, mylogger);
TR_logger = TR_INSTANCE_CAST(TR_Logger, mylogger2);
TR_serverAddHandler(server, (TR_EventHandler)test_handler); TR_serverAddHandler(server, (TR_EventHandler)test_handler);
TR_serverBindTcp(server, "0.0.0.0", 5678, protocol); TR_serverBindTcp(server, "0.0.0.0", 5678, protocol);

Loading…
Cancel
Save