aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/eet/src/lib/eet_connection.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--libraries/eet/src/lib/eet_connection.c222
1 files changed, 0 insertions, 222 deletions
diff --git a/libraries/eet/src/lib/eet_connection.c b/libraries/eet/src/lib/eet_connection.c
deleted file mode 100644
index 7b6b934..0000000
--- a/libraries/eet/src/lib/eet_connection.c
+++ /dev/null
@@ -1,222 +0,0 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif /* ifdef HAVE_CONFIG_H */
4
5#ifdef HAVE_ALLOCA_H
6# include <alloca.h>
7#elif defined __GNUC__
8# define alloca __builtin_alloca
9#elif defined _AIX
10# define alloca __alloca
11#elif defined _MSC_VER
12# include <malloc.h>
13# define alloca _alloca
14#else /* ifdef HAVE_ALLOCA_H */
15# include <stddef.h>
16# ifdef __cplusplus
17extern "C"
18# endif /* ifdef __cplusplus */
19void *alloca(size_t);
20#endif /* ifdef HAVE_ALLOCA_H */
21
22#include <string.h>
23#include <stdlib.h>
24
25#ifdef HAVE_NETINET_IN_H
26# include <netinet/in.h>
27#endif /* ifdef HAVE_NETINET_IN_H */
28
29#ifdef _WIN32
30# include <winsock2.h>
31#endif /* ifdef _WIN32 */
32
33#include <Eina.h>
34
35#include "Eet.h"
36#include "Eet_private.h"
37
38/* max message size: 1Mb - raised from original 64Kb */
39#define MAX_MSG_SIZE (1024 * 1024)
40#define MAGIC_EET_DATA_PACKET 0x4270ACE1
41
42struct _Eet_Connection
43{
44 Eet_Read_Cb *eet_read_cb;
45 Eet_Write_Cb *eet_write_cb;
46 void *user_data;
47
48 size_t allocated;
49 size_t size;
50 size_t received;
51
52 void *buffer;
53};
54
55EAPI Eet_Connection *
56eet_connection_new(Eet_Read_Cb *eet_read_cb,
57 Eet_Write_Cb *eet_write_cb,
58 const void *user_data)
59{
60 Eet_Connection *conn;
61
62 if ((!eet_read_cb) || (!eet_write_cb)) return NULL;
63
64 conn = calloc(1, sizeof (Eet_Connection));
65 if (!conn) return NULL;
66 conn->eet_read_cb = eet_read_cb;
67 conn->eet_write_cb = eet_write_cb;
68 conn->user_data = (void *)user_data;
69 return conn;
70}
71
72EAPI int
73eet_connection_received(Eet_Connection *conn,
74 const void *data,
75 size_t size)
76{
77 if ((!conn) || (!data) || (!size)) return size;
78 do
79 {
80 size_t copy_size;
81
82 if (conn->size == 0)
83 {
84 const int *msg;
85 size_t packet_size;
86
87 if (size < (sizeof(int) * 2)) break;
88
89 msg = data;
90 /* Check the magic */
91 if (ntohl(msg[0]) != MAGIC_EET_DATA_PACKET) break;
92
93 packet_size = ntohl(msg[1]);
94 /* Message should always be under MAX_MSG_SIZE */
95 if (packet_size > MAX_MSG_SIZE) break;
96
97 data = (void *)(msg + 2);
98 size -= sizeof(int) * 2;
99 if ((size_t)packet_size <= size)
100 {
101 /* Not a partial receive, go the quick way. */
102 if (!conn->eet_read_cb(data, packet_size, conn->user_data))
103 break;
104
105 data = (void *)((char *)data + packet_size);
106 size -= packet_size;
107 conn->received = 0;
108 continue;
109 }
110 conn->size = packet_size;
111 if (conn->allocated < conn->size)
112 {
113 void *tmp;
114
115 tmp = realloc(conn->buffer, conn->size);
116 if (!tmp) break;
117 conn->buffer = tmp;
118 conn->allocated = conn->size;
119 }
120 }
121
122 /* Partial receive */
123 copy_size =
124 (conn->size - conn->received >=
125 size) ? size : conn->size - conn->received;
126 memcpy((char *)conn->buffer + conn->received, data, copy_size);
127
128 conn->received += copy_size;
129 data = (void *)((char *)data + copy_size);
130 size -= copy_size;
131
132 if (conn->received == conn->size)
133 {
134 size_t data_size;
135
136 data_size = conn->size;
137 conn->size = 0;
138 conn->received = 0;
139 /* Completed a packet. */
140 if (!conn->eet_read_cb(conn->buffer, data_size, conn->user_data))
141 {
142 /* Something goes wrong. Stop now. */
143 size += data_size;
144 break;
145 }
146 }
147 }
148 while (size > 0);
149
150 return size;
151}
152
153static Eina_Bool
154_eet_connection_raw_send(Eet_Connection *conn,
155 void *data,
156 int data_size)
157{
158 int *message;
159
160 /* Message should always be under MAX_MSG_SIZE */
161 if (data_size > MAX_MSG_SIZE) return EINA_FALSE;
162 message = alloca(data_size + (sizeof(int) * 2));
163 message[0] = htonl(MAGIC_EET_DATA_PACKET);
164 message[1] = htonl(data_size);
165 memcpy(message + 2, data, data_size);
166 conn->eet_write_cb(message,
167 data_size + (sizeof(int) * 2),
168 conn->user_data);
169 return EINA_TRUE;
170}
171
172EAPI Eina_Bool
173eet_connection_send(Eet_Connection *conn,
174 Eet_Data_Descriptor *edd,
175 const void *data_in,
176 const char *cipher_key)
177{
178 void *flat_data;
179 int data_size;
180 Eina_Bool ret = EINA_FALSE;
181
182 flat_data = eet_data_descriptor_encode_cipher(edd,
183 data_in,
184 cipher_key,
185 &data_size);
186 if (!flat_data) return EINA_FALSE;
187 if (_eet_connection_raw_send(conn, flat_data, data_size)) ret = EINA_TRUE;
188 free(flat_data);
189 return ret;
190}
191
192EAPI Eina_Bool
193eet_connection_node_send(Eet_Connection *conn,
194 Eet_Node *node,
195 const char *cipher_key)
196{
197 void *data;
198 int data_size;
199 Eina_Bool ret = EINA_FALSE;
200
201 data = eet_data_node_encode_cipher(node, cipher_key, &data_size);
202 if (!data) return EINA_FALSE;
203 if (_eet_connection_raw_send(conn, data, data_size))
204 ret = EINA_TRUE;
205 free(data);
206 return ret;
207}
208
209EAPI void *
210eet_connection_close(Eet_Connection *conn,
211 Eina_Bool *on_going)
212{
213 void *user_data;
214
215 if (!conn) return NULL;
216 if (on_going) *on_going = conn->received == 0 ? EINA_FALSE : EINA_TRUE;
217 user_data = conn->user_data;
218 free(conn->buffer);
219 free(conn);
220 return user_data;
221}
222