Commit ec07940a329dfe1306b762c458956514a9073ea7

Authored by Georg Hopp
1 parent f7980118

add DatagramService which is pretty much the same as Connection... they probably should be unified.

... ... @@ -5,6 +5,7 @@ nobase_include_HEADERS = trcomm.h \
5 5 tr/connect_entry_point.h \
6 6 tr/connection.h \
7 7 tr/connector.h \
  8 + tr/datagram_service.h \
8 9 tr/io_handler.h \
9 10 tr/proto_message.h \
10 11 tr/protocol.h \
... ...
... ... @@ -45,9 +45,6 @@ TR_CLASSVARS_DECL(TR_Connection) {
45 45 #define TR_CON_EVENT_NEW_CON (TR_CEP_EVENT_MAX + 1)
46 46 #define TR_CON_EVENT_MAX ((size_t)TR_CON_EVENT_NEW_CON)
47 47
48   -TR_ProtoMessage TR_conNextMessage(TR_Connection);
49   -int TR_conCompose(TR_Connection, TR_ProtoMessage);
50   -
51 48 #endif // __TR_CONNECTION_H__
52 49
53 50 // vim: set ts=4 sw=4:
... ...
  1 +/**
  2 + * \file
  3 + *
  4 + * \author Georg Hopp
  5 + *
  6 + * \copyright
  7 + * Copyright © 2014 Georg Hopp
  8 + *
  9 + * This program is free software: you can redistribute it and/or modify
  10 + * it under the terms of the GNU General Public License as published by
  11 + * the Free Software Foundation, either version 3 of the License, or
  12 + * (at your option) any later version.
  13 + *
  14 + * This program is distributed in the hope that it will be useful,
  15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17 + * GNU General Public License for more details.
  18 + *
  19 + * You should have received a copy of the GNU General Public License
  20 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21 + */
  22 +
  23 +#ifndef __TR_DATAGRAM_SERVICE_H__
  24 +#define __TR_DATAGRAM_SERVICE_H__
  25 +
  26 +#include <sys/types.h>
  27 +
  28 +#include "trbase.h"
  29 +#include "trevent.h"
  30 +#include "trdata.h"
  31 +
  32 +#include "tr/comm_end_point.h"
  33 +#include "tr/proto_message.h"
  34 +
  35 +TR_CLASS(TR_DatagramService) {
  36 + TR_EXTENDS(TR_CommEndPoint);
  37 +};
  38 +TR_INSTANCE_INIT(TR_DatagramService);
  39 +TR_CLASSVARS_DECL(TR_DatagramService) {
  40 + TR_CV_EXTENDS(TR_CommEndPoint);
  41 +};
  42 +
  43 +TR_ProtoMessage TR_dsNextMessage(TR_DatagramService);
  44 +int TR_dsCompose(TR_DatagramService, TR_ProtoMessage);
  45 +
  46 +#endif // __TR_DATAGRAM_SERVICE_H__
  47 +
  48 +// vim: set ts=4 sw=4:
  49 +
... ...
... ... @@ -7,6 +7,7 @@
7 7 #include "tr/connect_entry_point.h"
8 8 #include "tr/connection.h"
9 9 #include "tr/connector.h"
  10 +#include "tr/datagram_service.h"
10 11 #include "tr/io_handler.h"
11 12 #include "tr/proto_message.h"
12 13 #include "tr/protocol.h"
... ...
... ... @@ -17,6 +17,7 @@ TRCOMM = cep_append_read_data.c \
17 17 conn_entry_point.c \
18 18 connection.c \
19 19 connector.c \
  20 + datagram_service.c \
20 21 io_handler.c \
21 22 proto_message.c \
22 23 protocol.c \
... ...
  1 +/**
  2 + * \file
  3 + *
  4 + * \author Georg Hopp
  5 + *
  6 + * \copyright
  7 + * Copyright © 2014 Georg Hopp
  8 + *
  9 + * This program is free software: you can redistribute it and/or modify
  10 + * it under the terms of the GNU General Public License as published by
  11 + * the Free Software Foundation, either version 3 of the License, or
  12 + * (at your option) any later version.
  13 + *
  14 + * This program is distributed in the hope that it will be useful,
  15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17 + * GNU General Public License for more details.
  18 + *
  19 + * You should have received a copy of the GNU General Public License
  20 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21 + */
  22 +
  23 +#include <stdarg.h>
  24 +#include <stdint.h>
  25 +
  26 +#include <sys/types.h>
  27 +
  28 +#include "trbase.h"
  29 +#include "trio.h"
  30 +#include "trdata.h"
  31 +
  32 +#include "tr/datagram_service.h"
  33 +#include "tr/interface/protocol.h"
  34 +#include "tr/interface/comm_end_point.h"
  35 +
  36 +static
  37 +int
  38 +datagramServiceCtor(void * _this, va_list * params)
  39 +{
  40 + TR_PARENTCALL(TR_DatagramService, _this, TR_Class, ctor, params);
  41 +
  42 + return 0;
  43 +}
  44 +
  45 +static
  46 +void
  47 +datagramServiceDtor(void * _this)
  48 +{
  49 + TR_PARENTCALL(TR_DatagramService, _this, TR_Class, dtor);
  50 +}
  51 +
  52 +static
  53 +TR_ProtoMessage
  54 +datagramServiceNextMessage(void * _this)
  55 +{
  56 + TR_CommEndPoint comm = _this;
  57 + TR_RemoteData data = TR_queueGet(comm->read_buffer);
  58 + TR_ProtoMessage ret_message = NULL;
  59 + size_t end;
  60 +
  61 + if (NULL == data) return ret_message;
  62 +
  63 + ret_message = TR_protoCreateMessage(comm->protocol, data->remote);
  64 + end = TR_protoParse(comm->protocol, ret_message, data);
  65 +
  66 + if (end != ((TR_SizedData)data)->size) {
  67 + /**
  68 + * TODO
  69 + * This means that the parser has not consumed all of the data.
  70 + * We do not know the reason, but with HTTP this should only occur
  71 + * when the message is complete... anyway, to prevent us from
  72 + * looping forever because a protocol implementation is buggy
  73 + * we should close the connection after end was 0 the second time.
  74 + * This can be done by firing a close event.
  75 + */
  76 + switch(end) {
  77 + default:
  78 + {
  79 + TR_RemoteData new_data = TR_new(
  80 + TR_RemoteData,
  81 + ((TR_SizedData)data)->data + end,
  82 + ((TR_SizedData)data)->size - end,
  83 + data->remote);
  84 + TR_delete(data);
  85 + data = new_data;
  86 + }
  87 + // intended drop through
  88 +
  89 + case 0:
  90 + TR_queuePutFirst(comm->read_buffer, data);
  91 + }
  92 + }
  93 +
  94 + if (! ret_message->ready) {
  95 + TR_delete(ret_message);
  96 + }
  97 +
  98 + return ret_message;
  99 +}
  100 +
  101 +static
  102 +void
  103 +datagramServiceCompose(void * _this, TR_ProtoMessage message)
  104 +{
  105 + TR_queuePut(
  106 + ((TR_CommEndPoint)_this)->write_buffer,
  107 + TR_protoCompose(((TR_CommEndPoint)_this)->protocol, message));
  108 +}
  109 +
  110 +intptr_t datagramService_events[TR_CEP_EVENT_MAX + 1];
  111 +TR_INIT_IFACE(TR_Class, datagramServiceCtor, datagramServiceDtor, NULL);
  112 +TR_INIT_IFACE(
  113 + TR_CommEndPoint,
  114 + datagramServiceNextMessage,
  115 + datagramServiceCompose);
  116 +TR_CREATE_CLASS(
  117 + TR_DatagramService,
  118 + TR_CommEndPoint,
  119 + NULL,
  120 + TR_IF(TR_Class),
  121 + TR_IF(TR_CommEndPoint)) = {
  122 + {{
  123 + TR_CEP_EVENT_MAX + 1,
  124 + datagramService_events
  125 + }}
  126 +};
  127 +
  128 +// vim: set ts=4 sw=4:
... ...
... ... @@ -39,7 +39,7 @@ testHandlerNewMessage(TR_EventHandler this, TR_Event event)
39 39 for (i = 0; buf[i]; i++) {
40 40 if (! isprint(buf[i])) buf[i] = '.';
41 41 }
42   -// printf("echo message: %s(%zd)\n", buf, data->size);
  42 + printf("echo message: %s(%zd)\n", buf, data->size);
43 43
44 44 TR_eventHandlerIssueEvent(
45 45 (TR_EventHandler)this,
... ... @@ -130,8 +130,13 @@ main (int argc, char * argv[])
130 130 TR_IoHandler io_handler = TR_new(TR_IoHandler);
131 131 TR_ProtocolHandler protocol_handler = TR_new(TR_ProtocolHandler);
132 132 TestHandler test_handler = TR_new(TestHandler);
  133 +#if 0
133 134 TR_ConnEntryPoint ep;
134 135 TR_TcpSocket ep_sock;
  136 +#else
  137 + TR_DatagramService ep;
  138 + TR_UdpSocket ep_sock;
  139 +#endif
135 140 TR_Protocol protocol;
136 141
137 142 TR_logger = TR_INSTANCE_CAST(TR_Logger, mylogger);
... ... @@ -147,8 +152,15 @@ main (int argc, char * argv[])
147 152 (TR_EventHandler)test_handler);
148 153
149 154 protocol = TR_new(TR_ProtocolRaw);
  155 +#if 0
150 156 ep_sock = TR_new(TR_TcpSocket, TR_logger, "0.0.0.0", 5678, 0);
151 157 ep = TR_new(TR_ConnEntryPoint, ep_sock, protocol);
  158 +#else
  159 + ep_sock = TR_new(TR_UdpSocket, TR_logger, "0.0.0.0", 5678, 0);
  160 + TR_socketBind((TR_Socket)ep_sock);
  161 + TR_socketNonblock((TR_Socket)ep_sock);
  162 + ep = TR_new(TR_DatagramService, ep_sock, protocol);
  163 +#endif
152 164
153 165 TR_commManagerAddEndpoint(cmgr, (TR_CommEndPoint)ep);
154 166
... ...
Please register or login to post a comment