aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src
diff options
context:
space:
mode:
authorDavid Walter Seikel2014-05-17 10:51:51 +1000
committerDavid Walter Seikel2014-05-17 10:51:51 +1000
commitbf2487ed5290366bf14cd399f4c25a4564f511d0 (patch)
treee9e62d3639737347d869cf48b38cb31d3697b74e /src
parentRearrange things in the libraries. (diff)
downloadSledjHamr-bf2487ed5290366bf14cd399f4c25a4564f511d0.zip
SledjHamr-bf2487ed5290366bf14cd399f4c25a4564f511d0.tar.gz
SledjHamr-bf2487ed5290366bf14cd399f4c25a4564f511d0.tar.bz2
SledjHamr-bf2487ed5290366bf14cd399f4c25a4564f511d0.tar.xz
Add a smarter server connect function.
Diffstat (limited to 'src')
-rw-r--r--src/libraries/SledjHamr.c74
-rw-r--r--src/libraries/SledjHamr.h2
2 files changed, 76 insertions, 0 deletions
diff --git a/src/libraries/SledjHamr.c b/src/libraries/SledjHamr.c
index 2d6ed3a..17b7610 100644
--- a/src/libraries/SledjHamr.c
+++ b/src/libraries/SledjHamr.c
@@ -1,5 +1,79 @@
1#include <unistd.h>
1#include "SledjHamr.h" 2#include "SledjHamr.h"
2 3
4
5struct _conct
6{
7 char *address;
8 int port;
9 void *pointer;
10 Ecore_Event_Handler_Cb addCb, dataCb, delCb;
11 Ecore_Event_Handler *add, *data, *del;
12};
13
14
15static Eina_Bool _add(void *data, int type, Ecore_Con_Event_Server_Del *ev)
16{
17 struct _conct *this = data;
18
19 ecore_event_handler_del(this->add);
20 ecore_event_handler_del(this->del);
21
22 if (this->addCb)
23 {
24 ecore_event_handler_add(ECORE_CON_EVENT_SERVER_ADD, this->addCb, this->pointer);
25 this->addCb(this->pointer, type, ev);
26 }
27 if (this->dataCb)
28 ecore_event_handler_add(ECORE_CON_EVENT_SERVER_DATA, this->dataCb, this->pointer);
29 if (this->delCb)
30 ecore_event_handler_add(ECORE_CON_EVENT_SERVER_DEL, this->delCb, this->pointer);
31
32 return ECORE_CALLBACK_RENEW;
33}
34
35static Eina_Bool _del(void *data, int type, Ecore_Con_Event_Server_Del *ev)
36{
37 struct _conct *this = data;
38
39 printf("FAILED to connect to server %s:%d, trying again!\n", this->address, this->port);
40 ecore_event_handler_del(this->add);
41 ecore_event_handler_del(this->del);
42 if (ev->server) ecore_con_server_del(ev->server);
43 sleep(1);
44 reachOut(this->address, this->port, this->pointer, this->addCb, this->dataCb, this->delCb);
45 return ECORE_CALLBACK_CANCEL;
46}
47
48Ecore_Con_Server *reachOut(char *address, int port, void *data, Ecore_Event_Handler_Cb _addCb, Ecore_Event_Handler_Cb _dataCb, Ecore_Event_Handler_Cb _delCb)
49{
50 Ecore_Con_Server *server = NULL;
51 struct _conct *this = malloc(sizeof(struct _conct));
52 int count = 0;
53
54 this->address = address;
55 this->port = port;
56 this->pointer = data;
57 this->addCb = _addCb;
58 this->dataCb = _dataCb;
59 this->delCb = _delCb;
60 // This loop is overkill I think.
61 while ((!server) && (10 > count))
62 {
63 if ((server = ecore_con_server_connect(ECORE_CON_REMOTE_TCP, address, port, this->pointer)))
64 {
65 this->add = ecore_event_handler_add(ECORE_CON_EVENT_SERVER_ADD, (Ecore_Event_Handler_Cb) _add, this);
66 this->del = ecore_event_handler_add(ECORE_CON_EVENT_SERVER_DEL, (Ecore_Event_Handler_Cb) _del, this);
67 }
68 count++;
69 }
70
71 if (!server)
72 printf("Failed to connect to server %s:%d!\n", this->address, this->port);
73
74 return server;
75}
76
3void sendBack(Ecore_Con_Client *client, const char *SID, const char *message, ...) 77void sendBack(Ecore_Con_Client *client, const char *SID, const char *message, ...)
4{ 78{
5 va_list args; 79 va_list args;
diff --git a/src/libraries/SledjHamr.h b/src/libraries/SledjHamr.h
index 548c5e0..6c63bcc 100644
--- a/src/libraries/SledjHamr.h
+++ b/src/libraries/SledjHamr.h
@@ -10,11 +10,13 @@
10 10
11#include <stdlib.h> 11#include <stdlib.h>
12 12
13#include <Ecore.h>
13#include <Ecore_Con.h> 14#include <Ecore_Con.h>
14 15
15 16
16#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(*array)) 17#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(*array))
17 18
19Ecore_Con_Server *reachOut(char *address, int port, void *data, Ecore_Event_Handler_Cb _add, Ecore_Event_Handler_Cb _data, Ecore_Event_Handler_Cb _del);
18void sendBack(Ecore_Con_Client *client, const char *SID, const char *message, ...); 20void sendBack(Ecore_Con_Client *client, const char *SID, const char *message, ...);
19void sendForth(Ecore_Con_Server *server, const char *SID, const char *message, ...); 21void sendForth(Ecore_Con_Server *server, const char *SID, const char *message, ...);
20 22