summaryrefslogtreecommitdiff
path: root/src/lib/ecore_con/ecore_con_eet.c
blob: 9720b5799fd4c6ba2180b5f215c15d390bc93ede (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#ifdef STDC_HEADERS
# include <stdlib.h>
# include <stddef.h>
#else
# ifdef HAVE_STDLIB_H
#  include <stdlib.h>
# endif
#endif

#ifdef HAVE_ARPA_INET_H
# include <arpa/inet.h>
#endif

#include <Eina.h>

#include "Ecore_Con_Eet.h"

#define ECORE_CON_EET_RAW_MAGIC 0xDEAD007
#define ECORE_CON_EET_DATA_KEY "ecore_con_eet_data_key"

typedef struct _Ecore_Con_Eet_Base_Data Ecore_Con_Eet_Base_Data;
typedef struct _Ecore_Con_Eet_Server_Obj_Data Ecore_Con_Eet_Server_Obj_Data;
typedef struct _Ecore_Con_Eet_Client_Obj_Data Ecore_Con_Eet_Client_Obj_Data;
typedef struct _Ecore_Con_Eet_Data     Ecore_Con_Eet_Data;
typedef struct _Ecore_Con_Eet_Raw_Data Ecore_Con_Eet_Raw_Data;
typedef struct _Ecore_Con_Eet_Client   Ecore_Con_Eet_Client;
typedef struct _Ecore_Con_Eet_Server   Ecore_Con_Eet_Server;

struct _Ecore_Con_Eet_Server_Obj_Data
{
   Eina_List *connections;
   Eina_List *client_connect_callbacks;
   Eina_List *client_disconnect_callbacks;

   Ecore_Event_Handler *handler_add;
   Ecore_Event_Handler *handler_del;
   Ecore_Event_Handler *handler_data;
};

struct _Ecore_Con_Eet_Client_Obj_Data
{
   Ecore_Con_Reply *r;
   Eina_List       *server_connect_callbacks;
   Eina_List       *server_disconnect_callbacks;

   Ecore_Event_Handler *handler_add;
   Ecore_Event_Handler *handler_del;
   Ecore_Event_Handler *handler_data;
};

struct _Ecore_Con_Reply
{
   Ecore_Con_Eet          *ece;
   Ecore_Con_Client       *client;

   Eet_Connection         *econn;

   char                   *buffer_section;
   unsigned char          *buffer;
   unsigned int            buffer_length;
   unsigned int            buffer_current;
   Ecore_Con_Eet_Raw_Data *buffer_handler;
};

struct _Ecore_Con_Eet_Data
{
   Ecore_Con_Eet_Data_Cb func;
   const char           *name;
   const void           *data;
};

struct _Ecore_Con_Eet_Raw_Data
{
   Ecore_Con_Eet_Raw_Data_Cb func;
   const char               *name;
   const void               *data;
};

struct _Ecore_Con_Eet_Client
{
   Ecore_Con_Eet_Client_Cb func;
   const void             *data;
};

struct _Ecore_Con_Eet_Server
{
   Ecore_Con_Eet_Server_Cb func;
   const void             *data;
};

struct _Ecore_Con_Eet_Base_Data
{
   Ecore_Con_Server    *server;

   Eet_Data_Descriptor *edd;
   Eet_Data_Descriptor *matching;

   Eina_Hash           *data_callbacks;
   Eina_Hash           *raw_data_callbacks;
};

static void
_ecore_con_eet_data_free(void *data)
{
   Ecore_Con_Eet_Data *eced = data;

   eina_stringshare_del(eced->name);
   free(eced);
}

static void
_ecore_con_eet_raw_data_free(void *data)
{
   Ecore_Con_Eet_Raw_Data *eced = data;

   eina_stringshare_del(eced->name);
   free(eced);
}

static void
_ecore_con_eet_reply_cleanup(Ecore_Con_Reply *n)
{
   if (n->buffer_handler) free(n->buffer);
   n->buffer = NULL;
   n->buffer_handler = NULL;
   free(n->buffer_section);
   n->buffer_section = NULL;
}

typedef struct _Ecore_Con_Eet_Protocol Ecore_Con_Eet_Protocol;
struct _Ecore_Con_Eet_Protocol
{
   const char *type;
   void       *data;
};

static const char *
_ecore_con_eet_data_type_get(const void *data, Eina_Bool *unknow EINA_UNUSED)
{
   const Ecore_Con_Eet_Protocol *p = data;

   return p->type;
}

static Eina_Bool
_ecore_con_eet_data_type_set(const char *type, void *data, Eina_Bool unknow EINA_UNUSED)
{
   Ecore_Con_Eet_Protocol *p = data;

   p->type = type;
   return EINA_TRUE;
}

static void
_ecore_con_eet_data_descriptor_setup(Ecore_Con_Eet_Base_Data *ece)
{
   Eet_Data_Descriptor_Class eddc;

   EET_EINA_STREAM_DATA_DESCRIPTOR_CLASS_SET(&eddc, Ecore_Con_Eet_Protocol);
   ece->edd = eet_data_descriptor_stream_new(&eddc);

   eddc.version = EET_DATA_DESCRIPTOR_CLASS_VERSION;
   eddc.func.type_get = _ecore_con_eet_data_type_get;
   eddc.func.type_set = _ecore_con_eet_data_type_set;
   ece->matching = eet_data_descriptor_stream_new(&eddc);

   EET_DATA_DESCRIPTOR_ADD_VARIANT(ece->edd, Ecore_Con_Eet_Protocol, "data", data, type, ece->matching);
}

/* Dealing with a server listening to connection */
static Eina_Bool
_ecore_con_eet_read_cb(const void *eet_data, size_t size, void *user_data)
{
   Ecore_Con_Reply *n = user_data;
   Ecore_Con_Eet_Protocol *protocol;
   Ecore_Con_Eet_Data *cb;
   Ecore_Con_Eet_Base_Data *ece_data = eo_data_scope_get(n->ece, ECORE_CON_EET_BASE_CLASS);

   protocol = eet_data_descriptor_decode(ece_data->edd, eet_data, size);
   if (!protocol) return EINA_TRUE;

   cb = eina_hash_find(ece_data->data_callbacks, protocol->type);
   if (!cb) return EINA_TRUE;  /* Should I report unknow protocol communication ? */

   cb->func((void *)cb->data, n, cb->name, protocol->data);

   eina_stringshare_del(protocol->type);
   free(protocol);

   return EINA_TRUE;
}

static Eina_Bool
_ecore_con_eet_server_write_cb(const void *data, size_t size, void *user_data)
{
   Ecore_Con_Reply *n = user_data;

   if (ecore_con_client_send(n->client, data, size) != (int)size)
     return EINA_FALSE;
   return EINA_TRUE;
}

static Eina_Bool
_ecore_con_eet_client_write_cb(const void *data, size_t size, void *user_data)
{
   Ecore_Con_Reply *n = user_data;
   Ecore_Con_Eet_Base_Data *ece_data = eo_data_scope_get(n->ece, ECORE_CON_EET_BASE_CLASS);

   if (ecore_con_server_send(ece_data->server, data, size) != (int)size)
     return EINA_FALSE;

   return EINA_TRUE;
}

static Eina_Bool
_ecore_con_eet_server_connected(void *data, int type EINA_UNUSED, Ecore_Con_Event_Client_Add *ev)
{
   Ecore_Con_Eet_Client *ecec;
   Eina_List *ll;
   Ecore_Con_Reply *n;
   Ecore_Con_Eet *ece_obj = data;
   Ecore_Con_Eet_Base_Data *base_data = eo_data_scope_get(ece_obj, ECORE_CON_EET_BASE_CLASS);
   Ecore_Con_Eet_Server_Obj_Data *r = eo_data_scope_get(ece_obj, ECORE_CON_EET_SERVER_OBJ_CLASS);

   if (ecore_con_client_server_get(ev->client) != base_data->server)
     return EINA_TRUE;

   n = calloc(1, sizeof (Ecore_Con_Reply));
   if (!n) return EINA_TRUE;

   n->client = ev->client;
   n->ece = ece_obj;
   n->econn = eet_connection_new(_ecore_con_eet_read_cb, _ecore_con_eet_server_write_cb, n);
   ecore_con_client_data_set(n->client, n);

   EINA_LIST_FOREACH(r->client_connect_callbacks, ll, ecec)
     if (!ecec->func((void *)ecec->data, n, n->client))
       {
          eet_connection_close(n->econn, NULL);
          free(n);
          return EINA_TRUE;
       }

   r->connections = eina_list_append(r->connections, n);

   return EINA_TRUE;
}

static Eina_Bool
_ecore_con_eet_server_disconnected(void *data, int type EINA_UNUSED, Ecore_Con_Event_Client_Del *ev)
{
   Ecore_Con_Eet *ece_obj = data;
   Ecore_Con_Eet_Base_Data *base_data = eo_data_scope_get(ece_obj, ECORE_CON_EET_BASE_CLASS);
   Ecore_Con_Eet_Server_Obj_Data *r = eo_data_scope_get(ece_obj, ECORE_CON_EET_SERVER_OBJ_CLASS);
   Ecore_Con_Reply *n;
   Eina_List *l;

   if (ecore_con_client_server_get(ev->client) != base_data->server)
     return EINA_TRUE;

   EINA_LIST_FOREACH(r->connections, l, n)
     if (n->client == ev->client)
       {
          Ecore_Con_Eet_Client *ecec;
          Eina_List *ll;

          EINA_LIST_FOREACH(r->client_disconnect_callbacks, ll, ecec)
            ecec->func((void *)ecec->data, n, n->client);

          eet_connection_close(n->econn, NULL);
          free(n);
          r->connections = eina_list_remove_list(r->connections, l);
          return EINA_TRUE;
       }

   return EINA_TRUE;
}

static void
_ecore_con_eet_raw_data_push(Ecore_Con_Reply *n, void *data, int size)
{
   if (n->buffer_handler)
     memcpy(n->buffer + n->buffer_current, data, size);
   n->buffer_current += size;

   if (n->buffer_current == n->buffer_length)
     {
        if (n->buffer_handler)
          n->buffer_handler->func((void *)n->buffer_handler->data, n, n->buffer_handler->name, n->buffer_section, n->buffer, n->buffer_length);
        _ecore_con_eet_reply_cleanup(n);
     }
}

static void
_ecore_con_eet_data(Ecore_Con_Reply *n, void *data, unsigned int size)
{
   /* FIXME: Enforce detection of attack and kill connection on that case */
   if (n->buffer)
     {
        if (n->buffer_current + size > n->buffer_length)
          {
             _ecore_con_eet_reply_cleanup(n);
             return;
          }

        _ecore_con_eet_raw_data_push(n, data, size);
        return;
     }
   else if (eet_connection_empty(n->econn) && size > (int)(4 * sizeof (unsigned int) + 2))
     {
        unsigned int *tmp = data;
        size -= 4 * sizeof (unsigned int);

        if (ntohl(tmp[0]) == ECORE_CON_EET_RAW_MAGIC)
          {
             unsigned int protocol_length = ntohl(tmp[1]);
             unsigned int section_length = ntohl(tmp[2]);
             unsigned int data_length = ntohl(tmp[3]);

             if (protocol_length > 1 && section_length > 1 && protocol_length + section_length <= size && data_length < 10 * 1024 * 1024)
               {
                  char *buffer = (char *)&tmp[4];
                  char *protocol;
                  char *section;
                  Ecore_Con_Eet_Base_Data *eceb_data = eo_data_scope_get(n->ece,ECORE_CON_EET_BASE_CLASS);

                  protocol = buffer;
                  section = buffer + protocol_length;

                  if (protocol[protocol_length - 1] == '\0' &&
                      section[section_length - 1] == '\0')
                    {
                       size -= protocol_length + section_length;
                       buffer = section + section_length;

                       n->buffer_handler = eina_hash_find(eceb_data->raw_data_callbacks, protocol);
                       n->buffer_section = strdup(section);
                       n->buffer_length = data_length;
                       n->buffer_current = 0;
                       if (n->buffer_handler)
                         n->buffer = malloc(sizeof (unsigned char) * data_length);
                       else
                         n->buffer = (void *)1;
                       if (n->buffer)
                         {
                            _ecore_con_eet_raw_data_push(n, buffer, size);
                            return;
                         }
                       _ecore_con_eet_reply_cleanup(n);

                       size += protocol_length + section_length;
                    }
               }
          }

        size += 4 * sizeof (unsigned int);
     }

   eet_connection_received(n->econn, data, size);
}

static Eina_Bool
_ecore_con_eet_server_data(void *data, int type EINA_UNUSED, Ecore_Con_Event_Client_Data *ev)
{
   Ecore_Con_Eet *ece_obj = data;
   Ecore_Con_Eet_Base_Data *r = eo_data_scope_get(ece_obj, ECORE_CON_EET_BASE_CLASS);
   Ecore_Con_Reply *n;

   if (ecore_con_client_server_get(ev->client) != r->server)
     return EINA_TRUE;

   n = ecore_con_client_data_get(ev->client);

   eo_ref(ece_obj);
   _ecore_con_eet_data(n, ev->data, ev->size);
   eo_unref(ece_obj);

   return EINA_TRUE;
}

/* Dealing connection to a server */

static Eina_Bool
_ecore_con_eet_client_connected(void *data, int type EINA_UNUSED, Ecore_Con_Event_Server_Add *ev)
{
   Ecore_Con_Eet_Server *eces;
   Ecore_Con_Eet *ece_obj = data;
   Ecore_Con_Eet_Base_Data *base_data = eo_data_scope_get(ece_obj, ECORE_CON_EET_BASE_CLASS);
   Ecore_Con_Eet_Client_Obj_Data *r = eo_data_scope_get(ece_obj, ECORE_CON_EET_CLIENT_OBJ_CLASS);
   Ecore_Con_Reply *n;
   Eina_List *ll;

   /* Client did connect */
   if (base_data->server != ev->server) return EINA_TRUE;
   if (r->r) return EINA_TRUE;

   n = calloc(1, sizeof (Ecore_Con_Reply));
   if (!n) return EINA_TRUE;

   n->client = NULL;
   n->ece = ece_obj;
   n->econn = eet_connection_new(_ecore_con_eet_read_cb, _ecore_con_eet_client_write_cb, n);

   EINA_LIST_FOREACH(r->server_connect_callbacks, ll, eces)
     {
        Ecore_Con_Eet_Base_Data *temp = eo_data_scope_get(n->ece, ECORE_CON_EET_BASE_CLASS);
        if (!eces->func((void *)eces->data, n, temp->server))
          {
             eet_connection_close(n->econn, NULL);
             free(n);
             return EINA_TRUE;
          }
     }

   r->r = n;

   return EINA_TRUE;
}

static Eina_Bool
_ecore_con_eet_client_disconnected(void *data, int type EINA_UNUSED, Ecore_Con_Event_Server_Del *ev)
{
   Ecore_Con_Eet *ece_obj = data;
   Ecore_Con_Eet_Base_Data *base_data = eo_data_scope_get(ece_obj, ECORE_CON_EET_BASE_CLASS);
   Ecore_Con_Eet_Client_Obj_Data *r = eo_data_scope_get(ece_obj, ECORE_CON_EET_CLIENT_OBJ_CLASS);
   Ecore_Con_Eet_Server *eces;
   Eina_List *ll;

   if (base_data->server != ev->server) return EINA_TRUE;
   if (!r->r) return EINA_TRUE;

   /* Client disconnected */
   EINA_LIST_FOREACH(r->server_disconnect_callbacks, ll, eces)
     eces->func((void *)eces->data, r->r, base_data->server);

   eet_connection_close(r->r->econn, NULL);
   free(r->r);
   r->r = NULL;

   return EINA_TRUE;
}

static Eina_Bool
_ecore_con_eet_client_data(void *data, int type EINA_UNUSED, Ecore_Con_Event_Server_Data *ev)
{
   Ecore_Con_Eet *ece_obj = data;
   Ecore_Con_Eet_Base_Data *base_data = eo_data_scope_get(ece_obj, ECORE_CON_EET_BASE_CLASS);
   Ecore_Con_Eet_Client_Obj_Data *r = eo_data_scope_get(ece_obj, ECORE_CON_EET_CLIENT_OBJ_CLASS);

   if (base_data->server != ev->server) return EINA_TRUE;
   if (!r->r) return EINA_TRUE;

   /* Got some data */
   eo_ref(ece_obj);
   _ecore_con_eet_data(r->r, ev->data, ev->size);
   eo_unref(ece_obj);

   return EINA_TRUE;
}

/*************
 * Generated API
 */

EOLIAN static void
_ecore_con_eet_base_data_callback_set(Eo *obj EINA_UNUSED, Ecore_Con_Eet_Base_Data *pd, const char *name, Ecore_Con_Eet_Data_Cb func, const void *data)
{
   Ecore_Con_Eet_Data *eced;

   eced = calloc(1, sizeof (Ecore_Con_Eet_Data));
   if (!eced) return;

   eced->func = func;
   eced->data = data;
   eced->name = eina_stringshare_add(name);

   eina_hash_direct_add(pd->data_callbacks, eced->name, eced);
}

EOLIAN static void
_ecore_con_eet_base_raw_data_callback_set(Eo *obj EINA_UNUSED, Ecore_Con_Eet_Base_Data *pd, const char *name, Ecore_Con_Eet_Raw_Data_Cb func, const void *data)
{
   Ecore_Con_Eet_Raw_Data *ecerd;

   ecerd = calloc(1, sizeof (Ecore_Con_Eet_Raw_Data));
   if (!ecerd) return;

   ecerd->func = func;
   ecerd->data = data;
   ecerd->name = eina_stringshare_add(name);

   eina_hash_direct_add(pd->raw_data_callbacks, ecerd->name, ecerd);
}

EOLIAN static void
_ecore_con_eet_base_data_callback_del(Eo *obj EINA_UNUSED, Ecore_Con_Eet_Base_Data *pd, const char *name)
{
   eina_hash_del(pd->data_callbacks, name, NULL);
}

EOLIAN static void
_ecore_con_eet_base_raw_data_callback_del(Eo *obj, Ecore_Con_Eet_Base_Data *pd, const char *name)
{
   Ecore_Con_Eet_Client_Obj_Data *eced = eo_data_scope_get(obj, ECORE_CON_EET_CLIENT_OBJ_CLASS);

   if (eo_isa(obj, ECORE_CON_EET_CLIENT_OBJ_CLASS) &&
       eced->r->buffer_handler &&
       !strcmp(eced->r->buffer_handler->name, name))
     {
        eced->r->buffer_handler = NULL;
        free(eced->r->buffer);
        eced->r->buffer = (void *)1;
     }
   eina_hash_del(pd->raw_data_callbacks, name, NULL);
}

EOLIAN static void
_ecore_con_eet_base_send(Eo *obj EINA_UNUSED, Ecore_Con_Eet_Base_Data *pd, Ecore_Con_Reply *reply, const char *name, void *value)
{
   Ecore_Con_Eet_Protocol protocol;

   if (!reply) return;

   protocol.type = name;
   protocol.data = value;

   eet_connection_send(reply->econn, pd->edd, &protocol, NULL);
}

EOLIAN static void
_ecore_con_eet_base_raw_send(Eo *obj EINA_UNUSED, Ecore_Con_Eet_Base_Data *pd, Ecore_Con_Reply *reply, const char *protocol_name, const char *section, void *value, unsigned int length)
{
   unsigned int protocol[4];
   unsigned int protocol_length;
   unsigned int section_length;
   unsigned int size;
   char *tmp;

   if (!reply) return;
   if (!protocol_name) return;
   if (!section) return;

   protocol_length = strlen(protocol_name) + 1;
   if (protocol_length == 1) return;
   section_length = strlen(section) + 1;

   protocol[0] = htonl(ECORE_CON_EET_RAW_MAGIC);
   protocol[1] = htonl(protocol_length);
   protocol[2] = htonl(section_length);
   protocol[3] = htonl(length);

   size = sizeof (protocol) + protocol_length + section_length;
   tmp = alloca(size);
   memcpy(tmp, protocol, sizeof (protocol));
   memcpy(tmp + sizeof (protocol), protocol_name, protocol_length);
   memcpy(tmp + sizeof (protocol) + protocol_length, section, section_length);

   if (reply->client)
     {
        ecore_con_client_send(reply->client, tmp, size);
        ecore_con_client_send(reply->client, value, length);
     }
   else
     {
        ecore_con_server_send(pd->server, tmp, size);
        ecore_con_server_send(pd->server, value, length);
     }
}

EOLIAN static void
_ecore_con_eet_base_register(Eo *obj EINA_UNUSED, Ecore_Con_Eet_Base_Data *pd, const char *name, Eet_Data_Descriptor *edd)
{
   EET_DATA_DESCRIPTOR_ADD_MAPPING(pd->matching, name, edd);
}

EOLIAN static Eo_Base *
_ecore_con_eet_server_obj_eo_base_constructor(Eo *obj, Ecore_Con_Eet_Server_Obj_Data *pd EINA_UNUSED)
{
   obj = eo_do_super_ret(obj, ECORE_CON_EET_SERVER_OBJ_CLASS, obj, eo_constructor());

   if (!obj) return NULL;

   pd->handler_add = ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_ADD,
                                             (Ecore_Event_Handler_Cb)_ecore_con_eet_server_connected, obj);
   pd->handler_del = ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_DEL,
                                             (Ecore_Event_Handler_Cb)_ecore_con_eet_server_disconnected, obj);
   pd->handler_data = ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_DATA,
                                              (Ecore_Event_Handler_Cb)_ecore_con_eet_server_data, obj);

   return obj;
}

EOLIAN static void
_ecore_con_eet_server_obj_eo_base_destructor(Eo *obj, Ecore_Con_Eet_Server_Obj_Data *pd EINA_UNUSED)
{
   Ecore_Con_Reply *n;
   Ecore_Con_Eet_Client *c;

   EINA_LIST_FREE(pd->connections, n)
     {
        _ecore_con_eet_reply_cleanup(n);
        eet_connection_close(n->econn, NULL);
        free(n);
     }
   EINA_LIST_FREE(pd->client_connect_callbacks, c)
     free(c);
   EINA_LIST_FREE(pd->client_disconnect_callbacks, c)
     free(c);

   ecore_event_handler_del(pd->handler_add);
   ecore_event_handler_del(pd->handler_del);
   ecore_event_handler_del(pd->handler_data);

   eo_do_super(obj, ECORE_CON_EET_SERVER_OBJ_CLASS, eo_destructor());
}

EOLIAN static Eo_Base *
_ecore_con_eet_client_obj_eo_base_constructor(Eo *obj, Ecore_Con_Eet_Client_Obj_Data *pd EINA_UNUSED)
{
   obj = eo_do_super_ret(obj, ECORE_CON_EET_CLIENT_OBJ_CLASS, obj, eo_constructor());

   if (!obj) return NULL;

   pd->handler_add = ecore_event_handler_add(ECORE_CON_EVENT_SERVER_ADD,
                                             (Ecore_Event_Handler_Cb)_ecore_con_eet_client_connected, obj);
   pd->handler_del = ecore_event_handler_add(ECORE_CON_EVENT_SERVER_DEL,
                                             (Ecore_Event_Handler_Cb)_ecore_con_eet_client_disconnected, obj);
   pd->handler_data = ecore_event_handler_add(ECORE_CON_EVENT_SERVER_DATA,
                                              (Ecore_Event_Handler_Cb)_ecore_con_eet_client_data, obj);

   return obj;
}

EOLIAN static void
_ecore_con_eet_client_obj_eo_base_destructor(Eo *obj, Ecore_Con_Eet_Client_Obj_Data *pd EINA_UNUSED)
{
   Ecore_Con_Eet_Server *s;

   if (pd->r)
     {
        _ecore_con_eet_reply_cleanup(pd->r);
        eet_connection_close(pd->r->econn, NULL);
     }
   EINA_LIST_FREE(pd->server_connect_callbacks, s)
     free(s);
   EINA_LIST_FREE(pd->server_disconnect_callbacks, s)
     free(s);

   ecore_event_handler_del(pd->handler_add);
   ecore_event_handler_del(pd->handler_del);
   ecore_event_handler_del(pd->handler_data);

   eo_do_super(obj, ECORE_CON_EET_CLIENT_OBJ_CLASS, eo_destructor());
}

EOLIAN static Eo_Base *
_ecore_con_eet_base_eo_base_constructor(Eo *obj, Ecore_Con_Eet_Base_Data *pd)
{
   obj = eo_do_super_ret(obj, ECORE_CON_EET_BASE_CLASS, obj, eo_constructor());

   if (!obj) return NULL;

   pd->data_callbacks = eina_hash_stringshared_new(_ecore_con_eet_data_free);
   pd->raw_data_callbacks = eina_hash_string_superfast_new(_ecore_con_eet_raw_data_free);

   _ecore_con_eet_data_descriptor_setup(pd);

   return obj;
}

EOLIAN static void
_ecore_con_eet_base_eo_base_destructor(Eo *obj, Ecore_Con_Eet_Base_Data *pd)
{
   eo_do_super(obj, ECORE_CON_EET_BASE_CLASS, eo_destructor());

   eet_data_descriptor_free(pd->edd);
   eet_data_descriptor_free(pd->matching);
   eina_hash_free(pd->data_callbacks);
   eina_hash_free(pd->raw_data_callbacks);
}

EOLIAN static Eo_Base *
_ecore_con_eet_base_eo_base_finalize(Eo *obj, Ecore_Con_Eet_Base_Data *pd)
{
   if (pd->server) return obj;

   eet_data_descriptor_free(pd->edd);
   eet_data_descriptor_free(pd->matching);
   eina_hash_free(pd->data_callbacks);
   eina_hash_free(pd->raw_data_callbacks);

   return NULL;
}

EOLIAN static void
_ecore_con_eet_base_server_set(Eo *obj EINA_UNUSED, Ecore_Con_Eet_Base_Data *pd, Ecore_Con_Server *data)
{
   if (!eo_isa(data, EFL_NETWORK_SERVER_CLASS))
     return;

   pd->server = data;
}

EOLIAN static Ecore_Con_Server *
_ecore_con_eet_base_server_get(Eo *obj EINA_UNUSED, Ecore_Con_Eet_Base_Data *pd)
{
   return pd->server;
}

/**************
* Global API *
**************/

EAPI Ecore_Con_Eet *
ecore_con_eet_server_new(Ecore_Con_Server *server)
{
   Ecore_Con_Eet *ece_obj;

   if (!server) return NULL;

   ece_obj = eo_add(ECORE_CON_EET_SERVER_OBJ_CLASS, NULL,
                    ecore_con_eet_base_server_set(server));

   return ece_obj;
}

EAPI Ecore_Con_Eet *
ecore_con_eet_client_new(Ecore_Con_Server *server)
{
   Ecore_Con_Eet *ece_obj;

   if (!server) return NULL;

   ece_obj = eo_add(ECORE_CON_EET_CLIENT_OBJ_CLASS, NULL,
                    ecore_con_eet_base_server_set(server));

   return ece_obj;
}

EAPI void
ecore_con_eet_server_free(Ecore_Con_Eet *r)
{
   eo_del(r);
}

EAPI void
ecore_con_eet_register(Ecore_Con_Eet *ece, const char *name, Eet_Data_Descriptor *edd)
{
   eo_do(ece, ecore_con_eet_base_register(name, edd));
}

EAPI void
ecore_con_eet_data_callback_add(Ecore_Con_Eet *ece, const char *name, Ecore_Con_Eet_Data_Cb func, const void *data)
{
   eo_do(ece, ecore_con_eet_base_data_callback_set(name, func, data));
}

EAPI void
ecore_con_eet_data_callback_del(Ecore_Con_Eet *ece, const char *name)
{
   eo_do(ece, ecore_con_eet_base_data_callback_del(name));
}

EAPI void
ecore_con_eet_raw_data_callback_add(Ecore_Con_Eet *ece, const char *name, Ecore_Con_Eet_Raw_Data_Cb func, const void *data)
{
   eo_do(ece, ecore_con_eet_base_raw_data_callback_set(name, func, data));
}

EAPI void
ecore_con_eet_raw_data_callback_del(Ecore_Con_Eet *ece, const char *name)
{
   eo_do(ece, ecore_con_eet_base_raw_data_callback_del(name));
}

EAPI void
ecore_con_eet_client_connect_callback_add(Ecore_Con_Eet *ece, Ecore_Con_Eet_Client_Cb func, const void *data)
{
   Ecore_Con_Eet_Server_Obj_Data *eces = eo_data_scope_get(ece, ECORE_CON_EET_SERVER_OBJ_CLASS);
   Ecore_Con_Eet_Client *c;

   if (!eces || !func) return;

   c = calloc(1, sizeof (Ecore_Con_Eet_Client));
   if (!c) return;

   c->func = func;
   c->data = data;

   eces->client_connect_callbacks = eina_list_append(eces->client_connect_callbacks, c);
}

EAPI void
ecore_con_eet_client_connect_callback_del(Ecore_Con_Eet *ece, Ecore_Con_Eet_Client_Cb func, const void *data)
{
   Ecore_Con_Eet_Server_Obj_Data *eces = eo_data_scope_get(ece, ECORE_CON_EET_SERVER_OBJ_CLASS);
   Ecore_Con_Eet_Client *c;
   Eina_List *l;

   if (!eces || !func) return;

   EINA_LIST_FOREACH(eces->client_connect_callbacks, l, c)
     if (c->func == func && c->data == data)
       {
          eces->client_connect_callbacks = eina_list_remove_list(eces->client_connect_callbacks, l);
          free(c);
          return;
       }
}

EAPI void
ecore_con_eet_client_disconnect_callback_add(Ecore_Con_Eet *ece, Ecore_Con_Eet_Client_Cb func, const void *data)
{
   Ecore_Con_Eet_Server_Obj_Data *eces = eo_data_scope_get(ece, ECORE_CON_EET_SERVER_OBJ_CLASS);
   Ecore_Con_Eet_Client *c;

   if (!eces || !func) return;

   c = calloc(1, sizeof (Ecore_Con_Eet_Client));
   if (!c) return;

   c->func = func;
   c->data = data;

   eces->client_disconnect_callbacks = eina_list_append(eces->client_disconnect_callbacks, c);
}

EAPI void
ecore_con_eet_client_disconnect_callback_del(Ecore_Con_Eet *ece, Ecore_Con_Eet_Client_Cb func, const void *data)
{
   Ecore_Con_Eet_Server_Obj_Data *eced = eo_data_scope_get(ece, ECORE_CON_EET_SERVER_OBJ_CLASS);
   Ecore_Con_Eet_Client *c;
   Eina_List *l;

   if (!eced || !func) return;

   EINA_LIST_FOREACH(eced->client_disconnect_callbacks, l, c)
     if (c->func == func && c->data == data)
       {
          eced->client_disconnect_callbacks = eina_list_remove_list(eced->client_disconnect_callbacks, l);
          free(c);
          return;
       }
}

EAPI void
ecore_con_eet_server_connect_callback_add(Ecore_Con_Eet *ece, Ecore_Con_Eet_Server_Cb func, const void *data)
{
   Ecore_Con_Eet_Client_Obj_Data *eced = eo_data_scope_get(ece, ECORE_CON_EET_CLIENT_OBJ_CLASS);
   Ecore_Con_Eet_Server *s;

   if (!eced || !func) return;

   s = calloc(1, sizeof (Ecore_Con_Eet_Server));
   if (!s) return;

   s->func = func;
   s->data = data;

   eced->server_connect_callbacks = eina_list_append(eced->server_connect_callbacks, s);
}

EAPI void
ecore_con_eet_server_connect_callback_del(Ecore_Con_Eet *ece, Ecore_Con_Eet_Server_Cb func, const void *data)
{
   Ecore_Con_Eet_Client_Obj_Data *eced = eo_data_scope_get(ece, ECORE_CON_EET_CLIENT_OBJ_CLASS);
   Ecore_Con_Eet_Server *s;
   Eina_List *l;

   if (!eced || !func) return;

   EINA_LIST_FOREACH(eced->server_connect_callbacks, l, s)
     if (s->func == func && s->data == data)
       {
          eced->server_connect_callbacks = eina_list_remove_list(eced->server_connect_callbacks, l);
          free(s);
          return;
       }
}

EAPI void
ecore_con_eet_server_disconnect_callback_add(Ecore_Con_Eet *ece, Ecore_Con_Eet_Server_Cb func, const void *data)
{
   Ecore_Con_Eet_Client_Obj_Data *eced = eo_data_scope_get(ece, ECORE_CON_EET_CLIENT_OBJ_CLASS);
   Ecore_Con_Eet_Server *s;

   if (!eced || !func) return;

   s = calloc(1, sizeof (Ecore_Con_Eet_Server));
   if (!s) return;

   s->func = func;
   s->data = data;

   eced->server_disconnect_callbacks = eina_list_append(eced->server_disconnect_callbacks, s);
}

EAPI void
ecore_con_eet_server_disconnect_callback_del(Ecore_Con_Eet *ece, Ecore_Con_Eet_Server_Cb func, const void *data)
{
   Ecore_Con_Eet_Client_Obj_Data *eced = eo_data_scope_get(ece, ECORE_CON_EET_CLIENT_OBJ_CLASS);
   Ecore_Con_Eet_Server *s;
   Eina_List *l;

   if (!eced || !func) return;

   EINA_LIST_FOREACH(eced->server_disconnect_callbacks, l, s)
     if (s->func == func && s->data == data)
       {
          eced->server_disconnect_callbacks = eina_list_remove_list(eced->server_disconnect_callbacks, l);
          free(s);
          return;
       }
}

EAPI void
ecore_con_eet_data_set(Ecore_Con_Eet *ece, const void *data)
{
   eo_do(ece,eo_key_data_set(ECORE_CON_EET_DATA_KEY, data));
}

EAPI const void *
ecore_con_eet_data_get(Ecore_Con_Eet *ece)
{
   const void *temp;

   return eo_do_ret(ece, temp, eo_key_data_get(ECORE_CON_EET_DATA_KEY));
}

EAPI Ecore_Con_Eet *
ecore_con_eet_reply(Ecore_Con_Reply *reply)
{
   if (!reply) return NULL;
   return reply->ece;
}

EAPI void
ecore_con_eet_send(Ecore_Con_Reply *reply, const char *name, void *value)
{
   eo_do(reply->ece, ecore_con_eet_base_send(reply, name, value));
}

EAPI void
ecore_con_eet_raw_send(Ecore_Con_Reply *reply, const char *protocol_name, const char *section, void *value, unsigned int length)
{
   eo_do(reply->ece,
         ecore_con_eet_base_raw_send(reply, protocol_name, section, value,
                                     length));
}

#include "ecore_con_eet_base.eo.c"
#include "ecore_con_eet_server_obj.eo.c"
#include "ecore_con_eet_client_obj.eo.c"