diff options
Diffstat (limited to 'libraries/eet/src/lib/eet_connection.c')
-rw-r--r-- | libraries/eet/src/lib/eet_connection.c | 129 |
1 files changed, 51 insertions, 78 deletions
diff --git a/libraries/eet/src/lib/eet_connection.c b/libraries/eet/src/lib/eet_connection.c index 6171b7e..7b6b934 100644 --- a/libraries/eet/src/lib/eet_connection.c +++ b/libraries/eet/src/lib/eet_connection.c | |||
@@ -35,6 +35,8 @@ void *alloca(size_t); | |||
35 | #include "Eet.h" | 35 | #include "Eet.h" |
36 | #include "Eet_private.h" | 36 | #include "Eet_private.h" |
37 | 37 | ||
38 | /* max message size: 1Mb - raised from original 64Kb */ | ||
39 | #define MAX_MSG_SIZE (1024 * 1024) | ||
38 | #define MAGIC_EET_DATA_PACKET 0x4270ACE1 | 40 | #define MAGIC_EET_DATA_PACKET 0x4270ACE1 |
39 | 41 | ||
40 | struct _Eet_Connection | 42 | struct _Eet_Connection |
@@ -56,74 +58,62 @@ eet_connection_new(Eet_Read_Cb *eet_read_cb, | |||
56 | const void *user_data) | 58 | const void *user_data) |
57 | { | 59 | { |
58 | Eet_Connection *conn; | 60 | Eet_Connection *conn; |
59 | 61 | ||
60 | if (!eet_read_cb || !eet_write_cb) | 62 | if ((!eet_read_cb) || (!eet_write_cb)) return NULL; |
61 | return NULL; | 63 | |
62 | |||
63 | conn = calloc(1, sizeof (Eet_Connection)); | 64 | conn = calloc(1, sizeof (Eet_Connection)); |
64 | if (!conn) | 65 | if (!conn) return NULL; |
65 | return NULL; | ||
66 | |||
67 | conn->eet_read_cb = eet_read_cb; | 66 | conn->eet_read_cb = eet_read_cb; |
68 | conn->eet_write_cb = eet_write_cb; | 67 | conn->eet_write_cb = eet_write_cb; |
69 | conn->user_data = (void *)user_data; | 68 | conn->user_data = (void *)user_data; |
70 | |||
71 | return conn; | 69 | return conn; |
72 | } /* eet_connection_new */ | 70 | } |
73 | 71 | ||
74 | EAPI int | 72 | EAPI int |
75 | eet_connection_received(Eet_Connection *conn, | 73 | eet_connection_received(Eet_Connection *conn, |
76 | const void *data, | 74 | const void *data, |
77 | size_t size) | 75 | size_t size) |
78 | { | 76 | { |
79 | if ((!conn) || (!data) || (!size)) | 77 | if ((!conn) || (!data) || (!size)) return size; |
80 | return size; | 78 | do |
81 | 79 | { | |
82 | do { | ||
83 | size_t copy_size; | 80 | size_t copy_size; |
84 | 81 | ||
85 | if (conn->size == 0) | 82 | if (conn->size == 0) |
86 | { | 83 | { |
87 | const int *msg; | 84 | const int *msg; |
88 | size_t packet_size; | 85 | size_t packet_size; |
89 | 86 | ||
90 | if (size < sizeof (int) * 2) | 87 | if (size < (sizeof(int) * 2)) break; |
91 | break; | ||
92 | 88 | ||
93 | msg = data; | 89 | msg = data; |
94 | /* Check the magic */ | 90 | /* Check the magic */ |
95 | if (ntohl(msg[0]) != MAGIC_EET_DATA_PACKET) | 91 | if (ntohl(msg[0]) != MAGIC_EET_DATA_PACKET) break; |
96 | break; | ||
97 | 92 | ||
98 | packet_size = ntohl(msg[1]); | 93 | packet_size = ntohl(msg[1]); |
99 | /* Message should always be under 64K */ | 94 | /* Message should always be under MAX_MSG_SIZE */ |
100 | if (packet_size > 64 * 1024) | 95 | if (packet_size > MAX_MSG_SIZE) break; |
101 | break; | ||
102 | 96 | ||
103 | data = (void *)(msg + 2); | 97 | data = (void *)(msg + 2); |
104 | size -= sizeof (int) * 2; | 98 | size -= sizeof(int) * 2; |
105 | if ((size_t)packet_size <= size) | 99 | if ((size_t)packet_size <= size) |
106 | { | 100 | { |
107 | /* Not a partial receive, go the quick way. */ | 101 | /* Not a partial receive, go the quick way. */ |
108 | if (!conn->eet_read_cb(data, packet_size, conn->user_data)) | 102 | if (!conn->eet_read_cb(data, packet_size, conn->user_data)) |
109 | break; | 103 | break; |
110 | 104 | ||
111 | data = (void *)((char *)data + packet_size); | 105 | data = (void *)((char *)data + packet_size); |
112 | size -= packet_size; | 106 | size -= packet_size; |
113 | 107 | conn->received = 0; | |
114 | conn->received = 0; | 108 | continue; |
115 | continue; | ||
116 | } | 109 | } |
117 | |||
118 | conn->size = packet_size; | 110 | conn->size = packet_size; |
119 | if (conn->allocated < conn->size) | 111 | if (conn->allocated < conn->size) |
120 | { | 112 | { |
121 | void *tmp; | 113 | void *tmp; |
122 | 114 | ||
123 | tmp = realloc(conn->buffer, conn->size); | 115 | tmp = realloc(conn->buffer, conn->size); |
124 | if (!tmp) | 116 | if (!tmp) break; |
125 | break; | ||
126 | |||
127 | conn->buffer = tmp; | 117 | conn->buffer = tmp; |
128 | conn->allocated = conn->size; | 118 | conn->allocated = conn->size; |
129 | } | 119 | } |
@@ -132,33 +122,33 @@ eet_connection_received(Eet_Connection *conn, | |||
132 | /* Partial receive */ | 122 | /* Partial receive */ |
133 | copy_size = | 123 | copy_size = |
134 | (conn->size - conn->received >= | 124 | (conn->size - conn->received >= |
135 | size) ? size : conn->size - conn->received; | 125 | size) ? size : conn->size - conn->received; |
136 | memcpy((char *)conn->buffer + conn->received, data, copy_size); | 126 | memcpy((char *)conn->buffer + conn->received, data, copy_size); |
137 | 127 | ||
138 | conn->received += copy_size; | 128 | conn->received += copy_size; |
139 | data = (void *)((char *)data + copy_size); | 129 | data = (void *)((char *)data + copy_size); |
140 | size -= copy_size; | 130 | size -= copy_size; |
141 | 131 | ||
142 | if (conn->received == conn->size) | 132 | if (conn->received == conn->size) |
143 | { | 133 | { |
144 | size_t data_size; | 134 | size_t data_size; |
145 | 135 | ||
146 | data_size = conn->size; | 136 | data_size = conn->size; |
147 | conn->size = 0; | 137 | conn->size = 0; |
148 | conn->received = 0; | 138 | conn->received = 0; |
149 | |||
150 | /* Completed a packet. */ | 139 | /* Completed a packet. */ |
151 | if (!conn->eet_read_cb(conn->buffer, data_size, conn->user_data)) | 140 | if (!conn->eet_read_cb(conn->buffer, data_size, conn->user_data)) |
152 | { | 141 | { |
153 | /* Something goes wrong. Stop now. */ | 142 | /* Something goes wrong. Stop now. */ |
154 | size += data_size; | 143 | size += data_size; |
155 | break; | 144 | break; |
156 | } | 145 | } |
157 | } | 146 | } |
158 | } while (size > 0); | 147 | } |
159 | 148 | while (size > 0); | |
149 | |||
160 | return size; | 150 | return size; |
161 | } /* eet_connection_received */ | 151 | } |
162 | 152 | ||
163 | static Eina_Bool | 153 | static Eina_Bool |
164 | _eet_connection_raw_send(Eet_Connection *conn, | 154 | _eet_connection_raw_send(Eet_Connection *conn, |
@@ -167,21 +157,17 @@ _eet_connection_raw_send(Eet_Connection *conn, | |||
167 | { | 157 | { |
168 | int *message; | 158 | int *message; |
169 | 159 | ||
170 | /* Message should never be above 64K */ | 160 | /* Message should always be under MAX_MSG_SIZE */ |
171 | if (data_size > 64 * 1024) | 161 | if (data_size > MAX_MSG_SIZE) return EINA_FALSE; |
172 | return EINA_FALSE; | 162 | message = alloca(data_size + (sizeof(int) * 2)); |
173 | |||
174 | message = alloca(data_size + sizeof (int) * 2); | ||
175 | message[0] = htonl(MAGIC_EET_DATA_PACKET); | 163 | message[0] = htonl(MAGIC_EET_DATA_PACKET); |
176 | message[1] = htonl(data_size); | 164 | message[1] = htonl(data_size); |
177 | |||
178 | memcpy(message + 2, data, data_size); | 165 | memcpy(message + 2, data, data_size); |
179 | |||
180 | conn->eet_write_cb(message, | 166 | conn->eet_write_cb(message, |
181 | data_size + sizeof (int) * 2, | 167 | data_size + (sizeof(int) * 2), |
182 | conn->user_data); | 168 | conn->user_data); |
183 | return EINA_TRUE; | 169 | return EINA_TRUE; |
184 | } /* _eet_connection_raw_send */ | 170 | } |
185 | 171 | ||
186 | EAPI Eina_Bool | 172 | EAPI Eina_Bool |
187 | eet_connection_send(Eet_Connection *conn, | 173 | eet_connection_send(Eet_Connection *conn, |
@@ -197,15 +183,11 @@ eet_connection_send(Eet_Connection *conn, | |||
197 | data_in, | 183 | data_in, |
198 | cipher_key, | 184 | cipher_key, |
199 | &data_size); | 185 | &data_size); |
200 | if (!flat_data) | 186 | if (!flat_data) return EINA_FALSE; |
201 | return EINA_FALSE; | 187 | if (_eet_connection_raw_send(conn, flat_data, data_size)) ret = EINA_TRUE; |
202 | |||
203 | if (_eet_connection_raw_send(conn, flat_data, data_size)) | ||
204 | ret = EINA_TRUE; | ||
205 | |||
206 | free(flat_data); | 188 | free(flat_data); |
207 | return ret; | 189 | return ret; |
208 | } /* eet_connection_send */ | 190 | } |
209 | 191 | ||
210 | EAPI Eina_Bool | 192 | EAPI Eina_Bool |
211 | eet_connection_node_send(Eet_Connection *conn, | 193 | eet_connection_node_send(Eet_Connection *conn, |
@@ -217,15 +199,12 @@ eet_connection_node_send(Eet_Connection *conn, | |||
217 | Eina_Bool ret = EINA_FALSE; | 199 | Eina_Bool ret = EINA_FALSE; |
218 | 200 | ||
219 | data = eet_data_node_encode_cipher(node, cipher_key, &data_size); | 201 | data = eet_data_node_encode_cipher(node, cipher_key, &data_size); |
220 | if (!data) | 202 | if (!data) return EINA_FALSE; |
221 | return EINA_FALSE; | ||
222 | |||
223 | if (_eet_connection_raw_send(conn, data, data_size)) | 203 | if (_eet_connection_raw_send(conn, data, data_size)) |
224 | ret = EINA_TRUE; | 204 | ret = EINA_TRUE; |
225 | |||
226 | free(data); | 205 | free(data); |
227 | return ret; | 206 | return ret; |
228 | } /* eet_connection_node_send */ | 207 | } |
229 | 208 | ||
230 | EAPI void * | 209 | EAPI void * |
231 | eet_connection_close(Eet_Connection *conn, | 210 | eet_connection_close(Eet_Connection *conn, |
@@ -233,17 +212,11 @@ eet_connection_close(Eet_Connection *conn, | |||
233 | { | 212 | { |
234 | void *user_data; | 213 | void *user_data; |
235 | 214 | ||
236 | if (!conn) | 215 | if (!conn) return NULL; |
237 | return NULL; | 216 | if (on_going) *on_going = conn->received == 0 ? EINA_FALSE : EINA_TRUE; |
238 | |||
239 | if (on_going) | ||
240 | *on_going = conn->received == 0 ? EINA_FALSE : EINA_TRUE; | ||
241 | |||
242 | user_data = conn->user_data; | 217 | user_data = conn->user_data; |
243 | |||
244 | free(conn->buffer); | 218 | free(conn->buffer); |
245 | free(conn); | 219 | free(conn); |
246 | |||
247 | return user_data; | 220 | return user_data; |
248 | } /* eet_connection_close */ | 221 | } |
249 | 222 | ||