16 changed files with 236 additions and 10 deletions
-
7.gitignore
-
4include/Makefile.am
-
47include/tr/threaded_server.h
-
1include/trcomm.h
-
2src/Makefile.am
-
2src/comm_manager.c
-
12src/connector.c
-
2src/io_handler.c
-
2src/protocol_handler.c
-
2src/simple_client.c
-
71src/threaded_server.c
-
44src/threaded_server_start.c
-
1testers/build.sh
-
2testers/test_handler.c
-
3testers/testclient.c
-
44testers/testserver_thread.c
@ -0,0 +1,47 @@ |
|||||
|
/** |
||||
|
* \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 <http://www.gnu.org/licenses/>. |
||||
|
*/ |
||||
|
|
||||
|
#ifndef __TR_THREADED_SERVER_H__ |
||||
|
#define __TR_THREADED_SERVER_H__ |
||||
|
|
||||
|
#include <sys/types.h> |
||||
|
|
||||
|
#include "trbase.h" |
||||
|
#include "trevent.h" |
||||
|
|
||||
|
#include "tr/server.h" |
||||
|
|
||||
|
TR_CLASS(TR_ThreadedServer) { |
||||
|
TR_EXTENDS(TR_Server); |
||||
|
|
||||
|
TR_EventThread * threads; |
||||
|
size_t n_threads; |
||||
|
}; |
||||
|
TR_INSTANCE_INIT(TR_ThreadedServer); |
||||
|
TR_CLASSVARS_DECL(TR_ThreadedServer) {}; |
||||
|
|
||||
|
void TR_threadedServerStart(TR_ThreadedServer, unsigned long); |
||||
|
|
||||
|
#endif // __TR_THREADED_SERVER_H__ |
||||
|
|
||||
|
// vim: set ts=4 sw=4: |
||||
|
|
||||
@ -0,0 +1,71 @@ |
|||||
|
/** |
||||
|
* \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 <http://www.gnu.org/licenses/>. |
||||
|
*/ |
||||
|
|
||||
|
#include <stdarg.h> |
||||
|
|
||||
|
#include <sys/types.h> |
||||
|
|
||||
|
#include "trbase.h" |
||||
|
#include "trio.h" |
||||
|
#include "trevent.h" |
||||
|
|
||||
|
#include "tr/threaded_server.h" |
||||
|
|
||||
|
static |
||||
|
int |
||||
|
threadedServerCtor(void * _this, va_list * params) |
||||
|
{ |
||||
|
TR_ThreadedServer this = _this; |
||||
|
int i; |
||||
|
|
||||
|
TR_PARENTCALL(TR_ThreadedServer, _this, TR_Class, ctor, params); |
||||
|
|
||||
|
this->n_threads = va_arg(*params, size_t); |
||||
|
this->threads = TR_malloc(sizeof(TR_EventThread) * this->n_threads); |
||||
|
|
||||
|
for (i=0; i<this->n_threads; i++) { |
||||
|
this->threads[i] = TR_new( |
||||
|
TR_EventThread, |
||||
|
((TR_Server)this)->dispatcher); |
||||
|
} |
||||
|
|
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
static |
||||
|
void |
||||
|
threadedServerDtor(void * _this) |
||||
|
{ |
||||
|
TR_ThreadedServer this = _this; |
||||
|
int i; |
||||
|
|
||||
|
for (i=0; i<this->n_threads; i++) { |
||||
|
TR_delete(this->threads[i]); |
||||
|
} |
||||
|
|
||||
|
TR_PARENTCALL(TR_ThreadedServer, _this, TR_Class, dtor); |
||||
|
} |
||||
|
|
||||
|
TR_INIT_IFACE(TR_Class, threadedServerCtor, threadedServerDtor, NULL); |
||||
|
TR_CREATE_CLASS(TR_ThreadedServer, TR_Server, NULL, TR_IF(TR_Class)); |
||||
|
|
||||
|
// vim: set ts=4 sw=4: |
||||
@ -0,0 +1,44 @@ |
|||||
|
/** |
||||
|
* \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 <http://www.gnu.org/licenses/>. |
||||
|
*/ |
||||
|
|
||||
|
#include "trbase.h" |
||||
|
#include "trevent.h" |
||||
|
|
||||
|
#include "tr/threaded_server.h" |
||||
|
|
||||
|
void |
||||
|
TR_threadedServerStart(TR_ThreadedServer this, unsigned long heartbeat) |
||||
|
{ |
||||
|
int i; |
||||
|
|
||||
|
TR_eventDispatcherSetHeartbeat(((TR_Server)this)->dispatcher, heartbeat); |
||||
|
|
||||
|
for (i=0; i<this->n_threads; i++) { |
||||
|
TR_eventThreadStart(this->threads[i]); |
||||
|
} |
||||
|
|
||||
|
for (i=0; i<this->n_threads; i++) { |
||||
|
TR_eventThreadJoin(this->threads[i]); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// vim: set ts=4 sw=4: |
||||
@ -0,0 +1,44 @@ |
|||||
|
#include <stdio.h> |
||||
|
#include <string.h> |
||||
|
#include <inttypes.h> |
||||
|
|
||||
|
#include "trbase.h" |
||||
|
#include "trcomm.h" |
||||
|
#include "trio.h" |
||||
|
#include "trevent.h" |
||||
|
|
||||
|
#include "test_handler.h" |
||||
|
|
||||
|
TR_INSTANCE(TR_LoggerSyslog, mylogger, {TR_LOGGER_INFO}); |
||||
|
TR_INSTANCE(TR_LoggerStderr, mylogger2, {TR_LOGGER_INFO}); |
||||
|
|
||||
|
int |
||||
|
main (int argc, char * argv[]) |
||||
|
{ |
||||
|
TR_ThreadedServer server = TR_new(TR_ThreadedServer, 2); |
||||
|
TR_Protocol protocol = TR_new(TR_ProtocolRaw); |
||||
|
TestHandler test_handler = TR_new(TestHandler); |
||||
|
|
||||
|
//TR_logger = TR_INSTANCE_CAST(TR_Logger, mylogger2); |
||||
|
|
||||
|
TR_serverAddHandler((TR_Server)server, (TR_EventHandler)test_handler); |
||||
|
TR_serverBindTcp((TR_Server)server, "0.0.0.0", 5678, protocol); |
||||
|
TR_serverBindUdp((TR_Server)server, "0.0.0.0", 5678, protocol); |
||||
|
|
||||
|
TR_threadedServerStart(server, 1000); |
||||
|
|
||||
|
puts("cleanup..."); |
||||
|
|
||||
|
TR_delete(server); |
||||
|
TR_delete(test_handler); |
||||
|
TR_delete(protocol); |
||||
|
|
||||
|
TR_eventHandlerClassCleanup(TestHandler); |
||||
|
TR_serverClassCleanup(); |
||||
|
|
||||
|
TR_cleanup(); |
||||
|
|
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
// vim: set ts=4 sw=4: |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue