From b84e10694fcc4ca88717975d2412dcd6c59aa591 Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Fri, 15 Aug 2014 19:20:55 +0100 Subject: [PATCH] simplify server interface --- include/tr/datagram_entry_point.h | 1 + include/tr/server.h | 3 +++ src/Makefile.am | 2 ++ src/server_bind_tcp.c | 41 +++++++++++++++++++++++++++++++ src/server_bind_udp.c | 41 +++++++++++++++++++++++++++++++ testers/testserver2.c | 12 ++------- 6 files changed, 90 insertions(+), 10 deletions(-) create mode 100644 src/server_bind_tcp.c create mode 100644 src/server_bind_udp.c diff --git a/include/tr/datagram_entry_point.h b/include/tr/datagram_entry_point.h index ecdc807..d8f4ca5 100644 --- a/include/tr/datagram_entry_point.h +++ b/include/tr/datagram_entry_point.h @@ -30,6 +30,7 @@ #include "trdata.h" #include "tr/comm_end_point.h" +#include "tr/datagram_service.h" TR_CLASS(TR_DatagramEntryPoint) { TR_EXTENDS(TR_DatagramService); diff --git a/include/tr/server.h b/include/tr/server.h index dd02153..6646ea7 100644 --- a/include/tr/server.h +++ b/include/tr/server.h @@ -32,6 +32,7 @@ #include "tr/connector.h" #include "tr/io_handler.h" #include "tr/protocol_handler.h" +#include "tr/protocol.h" TR_CLASS(TR_Server) { TR_CommManager comm_manager; @@ -49,6 +50,8 @@ TR_CLASSVARS_DECL(TR_Server) {}; #define TR_serverAddHandler(srv, handler) \ TR_eventDispatcherRegisterHandler((srv)->dispatcher, (handler)) +void TR_serverBindTcp(TR_Server, const char *, int, TR_Protocol); +void TR_serverBindUdp(TR_Server, const char *, int, TR_Protocol); void TR_serverStart(TR_Server, int); #define TR_serverClassCleanup() \ diff --git a/src/Makefile.am b/src/Makefile.am index 12b73e6..fa68c64 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -24,6 +24,8 @@ TRCOMM = cep_append_read_data.c \ protocol_message_raw.c \ protocol_raw.c \ server.c \ + server_bind_tcp.c \ + server_bind_udp.c \ server_start.c \ i_comm_end_point.c \ i_comm_manager.c \ diff --git a/src/server_bind_tcp.c b/src/server_bind_tcp.c new file mode 100644 index 0000000..43ea1e2 --- /dev/null +++ b/src/server_bind_tcp.c @@ -0,0 +1,41 @@ +/** + * \file + * + * \author Georg Hopp + * + * \copyright + * Copyright © 2014 Georg Hopp + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "trio.h" + +#include "tr/server.h" +#include "tr/protocol.h" +#include "tr/connect_entry_point.h" + +void +TR_serverBindTcp(TR_Server this, const char * host, int port, TR_Protocol proto) +{ + TR_Socket socket = (TR_Socket)TR_new( + TR_TcpSocket, + TR_logger, + host, + port, 0); + + TR_serverAddEndpoint(this, TR_new(TR_ConnEntryPoint, socket, proto)); +} + +// vim: set ts=4 sw=4: diff --git a/src/server_bind_udp.c b/src/server_bind_udp.c new file mode 100644 index 0000000..b9a87e5 --- /dev/null +++ b/src/server_bind_udp.c @@ -0,0 +1,41 @@ +/** + * \file + * + * \author Georg Hopp + * + * \copyright + * Copyright © 2014 Georg Hopp + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "trio.h" + +#include "tr/server.h" +#include "tr/protocol.h" +#include "tr/datagram_entry_point.h" + +void +TR_serverBindUdp(TR_Server this, const char * host, int port, TR_Protocol proto) +{ + TR_Socket socket = (TR_Socket)TR_new( + TR_UdpSocket, + TR_logger, + host, + port, 0); + + TR_serverAddEndpoint(this, TR_new(TR_DatagramEntryPoint, socket, proto)); +} + +// vim: set ts=4 sw=4: diff --git a/testers/testserver2.c b/testers/testserver2.c index 11f9072..e47da20 100644 --- a/testers/testserver2.c +++ b/testers/testserver2.c @@ -17,20 +17,12 @@ main (int argc, char * argv[]) TR_Server server = TR_new(TR_Server); TR_Protocol protocol = TR_new(TR_ProtocolRaw); TestHandler test_handler = TR_new(TestHandler); - TR_Socket socket; TR_logger = TR_INSTANCE_CAST(TR_Logger, mylogger); TR_serverAddHandler(server, (TR_EventHandler)test_handler); - socket = (TR_Socket)TR_new(TR_TcpSocket, TR_logger, "0.0.0.0", 5678, 0); - TR_serverAddEndpoint( - server, - TR_new(TR_ConnEntryPoint, socket, protocol)); - - socket = TR_new(TR_UdpSocket, TR_logger, "0.0.0.0", 5678, 0); - TR_serverAddEndpoint( - server, - TR_new(TR_DatagramEntryPoint, socket, protocol)); + TR_serverBindTcp(server, "0.0.0.0", 5678, protocol); + TR_serverBindUdp(server, "0.0.0.0", 5678, protocol); TR_serverStart(server, 1000);