3 #endif /* ifdef HAVE_CONFIG_H */
8 # define alloca __builtin_alloca
10 # define alloca __alloca
11 #elif defined _MSC_VER
13 # define alloca _alloca
14 #else /* ifdef HAVE_ALLOCA_H */
18 # endif /* ifdef __cplusplus */
19 void * alloca (size_t);
20 #endif /* ifdef HAVE_ALLOCA_H */
25 #ifdef HAVE_NETINET_IN_H
26 # include <netinet/in.h>
27 #endif /* ifdef HAVE_NETINET_IN_H */
30 # include <winsock2.h>
31 #endif /* ifdef _WIN32 */
36 #include "Eet_private.h"
38 #define MAGIC_EET_DATA_PACKET 0x4270ACE1
40 struct _Eet_Connection
42 Eet_Read_Cb *eet_read_cb;
43 Eet_Write_Cb *eet_write_cb;
54 eet_connection_new(Eet_Read_Cb *eet_read_cb,
55 Eet_Write_Cb *eet_write_cb,
56 const void *user_data)
60 if (!eet_read_cb || !eet_write_cb)
63 conn = calloc(1, sizeof (Eet_Connection));
67 conn->eet_read_cb = eet_read_cb;
68 conn->eet_write_cb = eet_write_cb;
69 conn->user_data = (void *)user_data;
72 } /* eet_connection_new */
75 eet_connection_received(Eet_Connection *conn,
79 if ((!conn) || (!data) || (!size))
90 if (size < sizeof (int) * 2)
95 if (ntohl(msg[0]) != MAGIC_EET_DATA_PACKET)
98 packet_size = ntohl(msg[1]);
99 /* Message should always be under 64K */
100 if (packet_size > 64 * 1024)
103 data = (void *)(msg + 2);
104 size -= sizeof (int) * 2;
105 if ((size_t)packet_size <= size)
107 /* Not a partial receive, go the quick way. */
108 if (!conn->eet_read_cb(data, packet_size, conn->user_data))
111 data = (void *)((char *)data + packet_size);
118 conn->size = packet_size;
119 if (conn->allocated < conn->size)
123 tmp = realloc(conn->buffer, conn->size);
128 conn->allocated = conn->size;
132 /* Partial receive */
134 (conn->size - conn->received >=
135 size) ? size : conn->size - conn->received;
136 memcpy((char *)conn->buffer + conn->received, data, copy_size);
138 conn->received += copy_size;
139 data = (void *)((char *)data + copy_size);
142 if (conn->received == conn->size)
146 data_size = conn->size;
150 /* Completed a packet. */
151 if (!conn->eet_read_cb(conn->buffer, data_size, conn->user_data))
153 /* Something goes wrong. Stop now. */
161 } /* eet_connection_received */
164 _eet_connection_raw_send(Eet_Connection *conn,
170 /* Message should never be above 64K */
171 if (data_size > 64 * 1024)
174 message = alloca(data_size + sizeof (int) * 2);
175 message[0] = htonl(MAGIC_EET_DATA_PACKET);
176 message[1] = htonl(data_size);
178 memcpy(message + 2, data, data_size);
180 conn->eet_write_cb(message,
181 data_size + sizeof (int) * 2,
184 } /* _eet_connection_raw_send */
187 eet_connection_send(Eet_Connection *conn,
188 Eet_Data_Descriptor *edd,
190 const char *cipher_key)
194 Eina_Bool ret = EINA_FALSE;
196 flat_data = eet_data_descriptor_encode_cipher(edd,
203 if (_eet_connection_raw_send(conn, flat_data, data_size))
208 } /* eet_connection_send */
211 eet_connection_node_send(Eet_Connection *conn,
213 const char *cipher_key)
217 Eina_Bool ret = EINA_FALSE;
219 data = eet_data_node_encode_cipher(node, cipher_key, &data_size);
223 if (_eet_connection_raw_send(conn, data, data_size))
228 } /* eet_connection_node_send */
231 eet_connection_close(Eet_Connection *conn,
240 *on_going = conn->received == 0 ? EINA_FALSE : EINA_TRUE;
242 user_data = conn->user_data;
248 } /* eet_connection_close */