blob: 1990f71969e284bc5db0e18552abfe8c2b31ef38 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001#include <string.h>
2#include <stdlib.h>
3
4#ifdef UEMF
5 #include "uemf.h"
6#else
7 #include <socket.h>
8 #include <types.h>
9 #include <unistd.h>
10 #include "emfInternal.h"
11#endif
12
13static int socketDoOutput(socket_t *sp, char *buf, int toWrite, int *errCode);
14static int tryAlternateSendTo(int sock, char *buf, int toWrite, int i,
15 struct sockaddr *server);
16
17int socketHighestFd = -1;
18
19socket_t **socketList;
20int socketMax;
21
22
23int socketWriteString(int sid, char_t *buf)
24{
25 #ifdef UNICODE
26 char *byteBuf;
27 int r, buf_len;
28
29 buf_len = gstrlen(buf);
30 byteBuf = ballocUniToAsc(buf, buf_len);
31 r = socketWrite(sid, byteBuf, buf_len);
32 bfreeSafe(B_L, byteBuf);
33 return r;
34 #else
35 return socketWrite(sid, buf, strlen(buf));
36 #endif /* UNICODE */
37}
38
39
40int socketWrite(int sid, char *buf, int bufsize)
41{
42 socket_t *sp;
43 ringq_t *rq;
44 int buf_len, bytesWritten, room;
45
46 a_assert(buf);
47 a_assert(bufsize >= 0);
48
49 if ((sp = socketPtr(sid)) == NULL) {
50 return -1;
51 }
52
53 rq = &sp->outBuf;
54 for (bytesWritten = 0; bufsize > 0; ) {
55 if ((room = ringqPutBlkMax(rq)) == 0) {
56 if (socketFlush(sid) < 0) {
57 return -1;
58 }
59 if ((room = ringqPutBlkMax(rq)) == 0) {
60 if (sp->flags & SOCKET_BLOCK) {
61#if (defined (WIN) || defined (CE))
62 int errCode;
63 if (! socketWaitForEvent(sp, FD_WRITE | SOCKET_WRITABLE, &errCode)) {
64 return -1;
65 }
66#endif
67 continue;
68 }
69 break;
70 }
71 continue;
72 }
73 buf_len = min(room, bufsize);
74 ringqPutBlk(rq, (unsigned char *) buf, buf_len);
75 bytesWritten += buf_len;
76 bufsize -= buf_len;
77 buf += buf_len;
78 }
79 return bytesWritten;
80}
81
82
83int socketRead(int sid, char *buf, int bufsize)
84{
85 socket_t *sp;
86 ringq_t *rq;
87 int buf_len, room, errCode, bytesRead;
88
89 a_assert(buf);
90 a_assert(bufsize > 0);
91
92 if ((sp = socketPtr(sid)) == NULL) {
93 return -1;
94 }
95
96 if (sp->flags & SOCKET_EOF) {
97 return 0;
98 }
99
100 rq = &sp->inBuf;
101 for (bytesRead = 0; bufsize > 0; ) {
102 buf_len = min(ringqLen(rq), bufsize);
103 if (buf_len <= 0) {
104
105 if ((sp->flags & SOCKET_BLOCK) &&
106 (bytesRead > 0)) {
107 break;
108 }
109
110 ringqFlush(rq);
111 room = ringqPutBlkMax(rq);
112 buf_len = socketGetInput(sid, (char *) rq->endp, room, &errCode);
113 if (buf_len < 0) {
114 if (errCode == EWOULDBLOCK) {
115 if ((sp->flags & SOCKET_BLOCK) &&
116 (bytesRead == 0)) {
117 continue;
118 }
119// if (bytesRead >= 0) { // kw 3 ; bytesRead>=0 always be true
120 return bytesRead;
121// }
122 }
123 return -1;
124
125 } else if (buf_len == 0) {
126
127 if (bytesRead == 0) {
128 sp->flags |= SOCKET_EOF;
129 }
130 return bytesRead;
131 }
132 ringqPutBlkAdj(rq, buf_len);
133 buf_len = min(buf_len, bufsize);
134 }
135 memcpy(&buf[bytesRead], rq->servp, buf_len);
136 ringqGetBlkAdj(rq, buf_len);
137 bufsize -= buf_len;
138 bytesRead += buf_len;
139 }
140 return bytesRead;
141}
142
143int socketFlush(int sid)
144{
145 socket_t *sp;
146 ringq_t *rq;
147 int rq_len, bytesWritten, errCode;
148 int errEINTRNum = 0; /* myp: ÓÃÓÚ¹æ±Üsocket´íÎó */
149
150 if ((sp = socketPtr(sid)) == NULL) {
151 return -1;
152 }
153 rq = &sp->outBuf;
154
155 if (! (sp->flags & SOCKET_BLOCK)) {
156 sp->flags |= SOCKET_FLUSHING;
157 }
158
159 while (ringqLen(rq) > 0) {
160 rq_len = ringqGetBlkMax(&sp->outBuf);
161 bytesWritten = socketDoOutput(sp, (char*) rq->servp, rq_len, &errCode);
162 if (bytesWritten < 0) {
163 if (errCode == EINTR && errEINTRNum++ < 1000) {
164 continue;
165// } else if (errCode == EWOULDBLOCK || errCode == EAGAIN) { kw 3 // #define EWOULDBLOCK EAGAIN
166 } else if (errCode == EAGAIN) {
167 if (sp->saveMask < 0 ) {
168 sp->saveMask = sp->handlerMask;
169 socketRegisterInterest(sp,
170 sp->handlerMask | SOCKET_WRITABLE);
171 }
172 return 0;
173 }
174 return -1;
175 }
176 ringqGetBlkAdj(rq, bytesWritten);
177 }
178
179 if (ringqLen(rq) == 0) {
180 ringqFlush(rq);
181 }
182
183 if (sp->saveMask >= 0) {
184 socketRegisterInterest(sp, sp->saveMask);
185 sp->saveMask = -1;
186 }
187 sp->flags &= ~SOCKET_FLUSHING;
188 return 0;
189}
190
191int socketGets(int sid, char_t **buf)
192{
193 socket_t *sp;
194 ringq_t *lq;
195 char c = '\0'; //cov
196 int rc, rq_len;
197
198 a_assert(buf);
199 *buf = NULL;
200
201 if ((sp = socketPtr(sid)) == NULL) {
202 return -1;
203 }
204 lq = &sp->lineBuf;
205
206 while (ringqLen(lq) < 1536) {
207
208 if ((rc = socketRead(sid, &c, 1)) < 0) {
209 return rc;
210 }
211
212 if (rc == 0) {
213 if (ringqLen(lq) > 0 && (sp->flags & SOCKET_EOF)) {
214 c = '\n';
215 } else {
216 return -1;
217 }
218 }
219
220 if (c == '\n') {
221 rq_len = ringqLen(lq);
222 if (rq_len > 0) {
223 *buf = ballocAscToUni((char *)lq->servp, rq_len);
224 } else {
225 *buf = NULL;
226 }
227 ringqFlush(lq);
228 return rq_len;
229
230 } else if (c == '\r') {
231 continue;
232 }
233 ringqPutcA(lq, c);
234 }
235 ringqFlush(lq);
236 return -1;
237}
238
239
240int socketInputBuffered(int sid)
241{
242 socket_t *sp;
243
244 if ((sp = socketPtr(sid)) == NULL) {
245 return -1;
246 }
247 if (socketEof(sid)) {
248 return -1;
249 }
250 return ringqLen(&sp->lineBuf) + ringqLen(&sp->inBuf);
251}
252
253int socketEof(int sid)
254{
255 socket_t *sp;
256
257 if ((sp = socketPtr(sid)) == NULL) {
258 return -1;
259 }
260 return sp->flags & SOCKET_EOF;
261}
262
263void socketCreateHandler(int sid, int handlerMask, socketHandler_t handler,
264 int data)
265{
266 socket_t *sp;
267
268 if ((sp = socketPtr(sid)) == NULL) {
269 return;
270 }
271 sp->handler = handler;
272 sp->handler_data = data;
273 socketRegisterInterest(sp, handlerMask);
274}
275
276static int socketDoOutput(socket_t *sp, char *buf, int toWrite, int *errCode)
277{
278 struct sockaddr_in server;
279 int bytes;
280
281 a_assert(sp);
282 a_assert(buf);
283 a_assert(toWrite > 0);
284 a_assert(errCode);
285
286 *errCode = 0;
287
288 if (sp->flags & SOCKET_BROADCAST) {
289 server.sin_family = AF_INET;
290 server.sin_addr.s_addr = INADDR_BROADCAST;
291 server.sin_port = htons((short)(sp->port & 0xFFFF));
292 bytes = sendto(sp->sock, buf, toWrite, 0,(struct sockaddr *) &server, sizeof(server));
293 } else if (sp->flags & SOCKET_DATAGRAM) {
294 server.sin_family = AF_INET;
295 server.sin_addr.s_addr = inet_addr(sp->host);
296 server.sin_port = htons((short)(sp->port & 0xFFFF));
297 bytes = sendto(sp->sock, buf, toWrite, 0,(struct sockaddr *) &server, sizeof(server));
298
299 } else {
300 bytes = send(sp->sock, buf, toWrite, 0);
301 }
302
303 if (bytes < 0) {
304 *errCode = socketGetError();
305 //printf("\n socketDoOutput ERROR: bytes = %d, *errCode = %d! ", bytes, *errCode);
306 return -1;
307
308 } else if (bytes == 0 && bytes != toWrite) {
309 *errCode = EWOULDBLOCK;
310 //printf("\n socketDoOutput ERROR: bytes = %d, *errCode = %d! ", bytes, *errCode);
311 return -1;
312 }
313
314 return bytes;
315}
316
317void socketDeleteHandler(int sid)
318{
319 socket_t *sp;
320
321 if ((sp = socketPtr(sid)) == NULL) {
322 return;
323 }
324 sp->handler = NULL;
325 socketRegisterInterest(sp, 0);
326}
327
328int socketAlloc(char *host, int port, socketAccept_t accept, int flags)
329{
330 socket_t *sp;
331 int sid;
332
333 if ((sid = hAllocEntry((void***) &socketList, &socketMax,
334 sizeof(socket_t))) < 0) {
335 return -1;
336 }
337 sp = socketList[sid];
338
339 sp->sid = sid;
340 sp->accept = accept;
341 sp->port = port;
342 sp->fileHandle = -1;
343 sp->saveMask = -1;
344
345 if (host) {
346 strncpy(sp->host, host, sizeof(sp->host)-1);
347 }
348
349 a_assert((flags & ~(SOCKET_BROADCAST|SOCKET_DATAGRAM|SOCKET_BLOCK|
350 SOCKET_LISTENING)) == 0);
351 sp->flags = flags & (SOCKET_BROADCAST | SOCKET_DATAGRAM | SOCKET_BLOCK|
352 SOCKET_LISTENING);
353
354
355 ringqOpen(&sp->inBuf, SOCKET_BUFSIZ, SOCKET_BUFSIZ);
356 ringqOpen(&sp->outBuf, SOCKET_BUFSIZ + 1, SOCKET_BUFSIZ + 1);
357 ringqOpen(&sp->lineBuf, SOCKET_BUFSIZ, -1);
358
359 return sid;
360}
361
362void socketFree(int sid)
363{
364 socket_t *sp;
365 char_t buf[256];
366 int i;
367 int recv_len = -1;
368
369 if ((sp = socketPtr(sid)) == NULL) {
370 return;
371 }
372
373 socketRegisterInterest(sp, 0);
374 if (sp->sock >= 0) {
375 socketSetBlock(sid, 0);
376 if (shutdown(sp->sock, 1) >= 0) {
377 recv_len = recv(sp->sock, buf, sizeof(buf), 0);
378 if(recv_len < 0){
379 ;
380 }else if(recv_len == 0){
381 // ÕâÀï±íʾ¶Ô¶ËµÄsocketÒÑÕý³£¹Ø±Õ?
382 ;
383 }else if(recv_len == sizeof(buf)){
384 ;
385 }else{
386 ;// ÐèÒªÔٴζÁÈ¡
387 }
388 }
389#if (defined (WIN) || defined (CE))
390 closesocket(sp->sock);
391#else
392 close(sp->sock);
393#endif
394 }
395
396 ringqClose(&sp->inBuf);
397 ringqClose(&sp->outBuf);
398 ringqClose(&sp->lineBuf);
399
400 bfree(B_L, sp);
401 socketMax = hFree((void***) &socketList, sid);
402
403 socketHighestFd = -1;
404 for (i = 0; i < socketMax; i++) {
405 if ((sp = socketList[i]) == NULL) {
406 continue;
407 }
408 socketHighestFd = max(socketHighestFd, sp->sock);
409 }
410}
411
412
413int socketGetError()
414{
415#if (defined (WIN) || defined (CE))
416 switch (WSAGetLastError()) {
417 case WSAEWOULDBLOCK:
418 return EWOULDBLOCK;
419 case WSAECONNRESET:
420 return ECONNRESET;
421 case WSAENETDOWN:
422 return ENETDOWN;
423 case WSAEPROCLIM:
424 return EAGAIN;
425 case WSAEINTR:
426 return EINTR;
427 default:
428 return EINVAL;
429 }
430#else
431 return errno;
432#endif
433}
434
435socket_t *socketPtr(int sid)
436{
437 if (sid < 0 || sid >= socketMax || socketList[sid] == NULL) {
438 a_assert(NULL);
439 errno = EBADF;
440 return NULL;
441 }
442
443 a_assert(socketList[sid]);
444 return socketList[sid];
445}
446
447int socketGetHandle(int sid)
448{
449 socket_t *sp;
450
451 if ((sp = socketPtr(sid)) == NULL) {
452 return -1;
453 }
454 return sp->sock;
455}
456
457int socketGetBlock(int sid)
458{
459 socket_t *sp;
460
461 if ((sp = socketPtr(sid)) == NULL) {
462 a_assert(0);
463 return 0;
464 }
465 return (sp->flags & SOCKET_BLOCK);
466}
467
468int socketGetMode(int sid)
469{
470 socket_t *sp;
471
472 if ((sp = socketPtr(sid)) == NULL) {
473 a_assert(0);
474 return 0;
475 }
476 return sp->flags;
477}
478
479void socketSetMode(int sid, int mode)
480{
481 socket_t *sp;
482
483 if ((sp = socketPtr(sid)) == NULL) {
484 a_assert(0);
485 return;
486 }
487 sp->flags = mode;
488}
489
490int socketGetPort(int sid)
491{
492 socket_t *sp;
493
494 if ((sp = socketPtr(sid)) == NULL) {
495 return -1;
496 }
497 return sp->port;
498}
499