| ︙ | | | ︙ | |
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
*
* (Ad 2) The main functions for this are SocketSetupProc() and
* SocketCheckProc().
*/
#include "tclWinInt.h"
#define DEBUGGING
#ifdef DEBUGGING
#define DEBUG(x) fprintf(stderr, ">>> %p %s(%d): %s<<<\n", \
infoPtr, __FUNCTION__, __LINE__, x)
#else
#define DEBUG(x)
#endif
/*
|
|
|
|
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
*
* (Ad 2) The main functions for this are SocketSetupProc() and
* SocketCheckProc().
*/
#include "tclWinInt.h"
//#define DEBUGGING
//#ifdef DEBUGGING
#define DEBUG(x) fprintf(stderr, ">>> %p %s(%d): %s<<<\n", \
infoPtr, __FUNCTION__, __LINE__, x)
#else
#define DEBUG(x)
#endif
/*
|
| ︙ | | | ︙ | |
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
|
HANDLE socketThread; /* Thread handling the window */
Tcl_ThreadId threadId; /* Parent thread. */
HANDLE readyEvent; /* Event indicating that a socket event is
* ready. Also used to indicate that the
* socketThread has been initialized and has
* started. */
HANDLE socketListLock; /* Win32 Event to lock the socketList */
SocketInfo *socketList; /* Every open socket in this thread has an
* entry on this list. */
} ThreadSpecificData;
static Tcl_ThreadDataKey dataKey;
static WNDCLASS windowClass;
/*
* Static functions defined in this file.
*/
static int CreateClientSocket(Tcl_Interp *interp, SocketInfo *infoPtr);
static void InitSockets(void);
static SocketInfo * NewSocketInfo(SOCKET socket);
static void SocketExitHandler(ClientData clientData);
static LRESULT CALLBACK SocketProc(HWND hwnd, UINT message, WPARAM wParam,
LPARAM lParam);
static int SocketsEnabled(void);
static void TcpAccept(TcpFdList *fds, SOCKET newSocket, address addr);
static int WaitForAsyncConnect(SocketInfo *infoPtr, int *errorCodePtr);
static int WaitForSocketEvent(SocketInfo *infoPtr, int events,
int *errorCodePtr);
static DWORD WINAPI SocketThread(LPVOID arg);
static void TcpThreadActionProc(ClientData instanceData,
int action);
static Tcl_EventCheckProc SocketCheckProc;
static Tcl_EventProc SocketEventProc;
static Tcl_EventSetupProc SocketSetupProc;
|
>
>
>
>
|
>
|
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
|
HANDLE socketThread; /* Thread handling the window */
Tcl_ThreadId threadId; /* Parent thread. */
HANDLE readyEvent; /* Event indicating that a socket event is
* ready. Also used to indicate that the
* socketThread has been initialized and has
* started. */
HANDLE socketListLock; /* Win32 Event to lock the socketList */
SocketInfo *pendingSocketInfo;
/* This socket is opened but not jet in the
* list. This value is also checked by
* the event structure. */
SocketInfo *socketList; /* Every open socket in this thread has an
* entry on this list. */
} ThreadSpecificData;
static Tcl_ThreadDataKey dataKey;
static WNDCLASS windowClass;
/*
* Static functions defined in this file.
*/
static int CreateClientSocket(Tcl_Interp *interp, SocketInfo *infoPtr);
static void InitSockets(void);
static SocketInfo * NewSocketInfo(SOCKET socket);
static void SocketExitHandler(ClientData clientData);
static LRESULT CALLBACK SocketProc(HWND hwnd, UINT message, WPARAM wParam,
LPARAM lParam);
static int SocketsEnabled(void);
static void TcpAccept(TcpFdList *fds, SOCKET newSocket, address addr);
static int WaitForConnect(SocketInfo *infoPtr, int *errorCodePtr);
static int WaitForSocketEvent(SocketInfo *infoPtr, int events,
int *errorCodePtr);
static int FindFDInList(SocketInfo *infoPtr, SOCKET socket);
static DWORD WINAPI SocketThread(LPVOID arg);
static void TcpThreadActionProc(ClientData instanceData,
int action);
static Tcl_EventCheckProc SocketCheckProc;
static Tcl_EventProc SocketEventProc;
static Tcl_EventSetupProc SocketSetupProc;
|
| ︙ | | | ︙ | |
394
395
396
397
398
399
400
401
402
403
404
405
406
407
|
/*
* OK, this thread has never done anything with sockets before. Construct
* a worker thread to handle asynchronous events related to sockets
* assigned to _this_ thread.
*/
tsdPtr = TCL_TSD_INIT(&dataKey);
tsdPtr->socketList = NULL;
tsdPtr->hwnd = NULL;
tsdPtr->threadId = Tcl_GetCurrentThread();
tsdPtr->readyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if (tsdPtr->readyEvent == NULL) {
goto initFailure;
}
|
>
|
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
|
/*
* OK, this thread has never done anything with sockets before. Construct
* a worker thread to handle asynchronous events related to sockets
* assigned to _this_ thread.
*/
tsdPtr = TCL_TSD_INIT(&dataKey);
tsdPtr->pendingSocketInfo = NULL;
tsdPtr->socketList = NULL;
tsdPtr->hwnd = NULL;
tsdPtr->threadId = Tcl_GetCurrentThread();
tsdPtr->readyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if (tsdPtr->readyEvent == NULL) {
goto initFailure;
}
|
| ︙ | | | ︙ | |
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
|
* queued (caused by persistent states that won't generate WinSock
* events).
*/
WaitForSingleObject(tsdPtr->socketListLock, INFINITE);
for (infoPtr = tsdPtr->socketList; infoPtr != NULL;
infoPtr = infoPtr->nextPtr) {
DEBUG("A");
if ((infoPtr->readyEvents &
(infoPtr->watchEvents | FD_CONNECT | FD_ACCEPT))
&& !(infoPtr->flags & SOCKET_PENDING)
) {
DEBUG("B");
infoPtr->flags |= SOCKET_PENDING;
evPtr = ckalloc(sizeof(SocketEvent));
evPtr->header.proc = SocketEventProc;
evPtr->socket = infoPtr->sockets->fd;
Tcl_QueueEvent((Tcl_Event *) evPtr, TCL_QUEUE_TAIL);
}
}
|
|
|
|
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
|
* queued (caused by persistent states that won't generate WinSock
* events).
*/
WaitForSingleObject(tsdPtr->socketListLock, INFINITE);
for (infoPtr = tsdPtr->socketList; infoPtr != NULL;
infoPtr = infoPtr->nextPtr) {
DEBUG("Socket loop");
if ((infoPtr->readyEvents &
(infoPtr->watchEvents | FD_CONNECT | FD_ACCEPT))
&& !(infoPtr->flags & SOCKET_PENDING)
) {
DEBUG("Event found");
infoPtr->flags |= SOCKET_PENDING;
evPtr = ckalloc(sizeof(SocketEvent));
evPtr->header.proc = SocketEventProc;
evPtr->socket = infoPtr->sockets->fd;
Tcl_QueueEvent((Tcl_Event *) evPtr, TCL_QUEUE_TAIL);
}
}
|
| ︙ | | | ︙ | |
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
|
if (bind(infoPtr->sockets->fd, infoPtr->myaddr->ai_addr,
infoPtr->myaddr->ai_addrlen) == SOCKET_ERROR) {
DEBUG("bind() failed");
TclWinConvertError((DWORD) WSAGetLastError());
continue;
}
/*
* Set the socket into nonblocking mode if the connect should
* be done in the background.
*/
if (async_connect) {
/* get infoPtr lock */
WaitForSingleObject(tsdPtr->socketListLock, INFINITE);
/* Now get connect events */
infoPtr->selectEvents |= FD_CONNECT;
/* Free list lock */
SetEvent(tsdPtr->socketListLock);
// ioctlsocket(infoPtr->sockets->fd, (long) FIONBIO, &flag);
SendMessage(tsdPtr->hwnd, SOCKET_SELECT, (WPARAM) SELECT,
(LPARAM) infoPtr);
} else {
/* Free list lock */
SetEvent(tsdPtr->socketListLock);
}
/*
* Attempt to connect to the remote socket.
*/
DEBUG("connect()");
connect(infoPtr->sockets->fd, infoPtr->addr->ai_addr,
infoPtr->addr->ai_addrlen);
error = WSAGetLastError();
TclWinConvertError(error);
if (async_connect && error == WSAEWOULDBLOCK) {
DEBUG("WSAEWOULDBLOCK");
/*
* Activate FD_CONNECT notification and reenter jump
*/
infoPtr->flags |= SOCKET_REENTER_PENDING;
return TCL_OK;
reenter:
Tcl_SetErrno(infoPtr->lastError);
DEBUG("reenter");
/* get infoPtr lock */
WaitForSingleObject(tsdPtr->socketListLock, INFINITE);
infoPtr->selectEvents &= ~(FD_CONNECT);
/* Free list lock */
SetEvent(tsdPtr->socketListLock);
}
/*
* Clear the reenter flag also for the (hypothetic) case
* that connect did succeed emidiately
*/
infoPtr->flags &= ~(SOCKET_REENTER_PENDING);
#ifdef DEBUGGING
fprintf(stderr, "lastError: %d\n", Tcl_GetErrno());
#endif
if (Tcl_GetErrno() == 0) {
goto out;
}
}
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
|
>
|
>
<
>
|
<
<
<
>
>
>
>
>
>
>
<
>
>
<
>
>
>
>
>
>
>
>
>
>
<
<
<
<
<
|
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
|
if (bind(infoPtr->sockets->fd, infoPtr->myaddr->ai_addr,
infoPtr->myaddr->ai_addrlen) == SOCKET_ERROR) {
DEBUG("bind() failed");
TclWinConvertError((DWORD) WSAGetLastError());
continue;
}
/*
* For asyncroneous connect set the socket in nonblocking mode
* and activate connect notification
*/
if (async_connect) {
SocketInfo *infoPtr2;
int in_socket_list = 0;
/* get infoPtr lock */
WaitForSingleObject(tsdPtr->socketListLock, INFINITE);
/*
* Check if my infoPtr is already in the tsdPtr->socketList
* It is set after this call by TcpThreadActionProc and is set
* on a second round.
*
* If not, we buffer my infoPtr in the tsd memory so it is not
* lost by the event procedure
*/
for (infoPtr2 = tsdPtr->socketList; infoPtr2 != NULL;
infoPtr2 = infoPtr->nextPtr) {
if (infoPtr2 == infoPtr) {
in_socket_list = 1;
break;
}
}
if (!in_socket_list) {
tsdPtr->pendingSocketInfo = infoPtr;
}
/*
* Set connect mask to connect events
* This is activated by a SOCKET_SELECT message to the notifier
* thread.
*/
infoPtr->selectEvents |= FD_CONNECT;
/*
* Free list lock
*/
SetEvent(tsdPtr->socketListLock);
/* activate accept notification */
SendMessage(tsdPtr->hwnd, SOCKET_SELECT, (WPARAM) SELECT,
(LPARAM) infoPtr);
}
/*
* Attempt to connect to the remote socket.
*/
DEBUG("connect()");
connect(infoPtr->sockets->fd, infoPtr->addr->ai_addr,
infoPtr->addr->ai_addrlen);
error = WSAGetLastError();
TclWinConvertError(error);
if (async_connect && error == WSAEWOULDBLOCK) {
/*
* Asynchroneous connect
*/
DEBUG("WSAEWOULDBLOCK");
/*
* Remember that we jump back behind this next round
*/
infoPtr->flags |= SOCKET_REENTER_PENDING;
return TCL_OK;
reenter:
DEBUG("reenter");
/*
* Re-entry point for async connect after connect event or
* blocking operation
*
* Clear the reenter flag
*/
infoPtr->flags &= ~(SOCKET_REENTER_PENDING);
/* get infoPtr lock */
WaitForSingleObject(tsdPtr->socketListLock, INFINITE);
/* Get signaled connect error */
Tcl_SetErrno(infoPtr->lastError);
/* Clear eventual connect flag */
infoPtr->selectEvents &= ~(FD_CONNECT);
/* Free list lock */
SetEvent(tsdPtr->socketListLock);
}
#ifdef DEBUGGING
fprintf(stderr, "lastError: %d\n", Tcl_GetErrno());
#endif
if (Tcl_GetErrno() == 0) {
goto out;
}
}
|
| ︙ | | | ︙ | |
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
|
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* WaitForAsyncConnect --
*
* Terminate an asyncroneous connect syncroneously.
* This routine should only be called if flag ASYNC_CONNECT is set.
*
* Results:
* Returns 1 on success or 0 on failure, with an error code in
* errorCodePtr.
*
* Side effects:
* Processes socket events off the system queue.
*
*----------------------------------------------------------------------
*/
static int
WaitForAsyncConnect(
SocketInfo *infoPtr, /* Information about this socket. */
int *errorCodePtr) /* Where to store errors? */
{
int result = 1;
int oldMode;
ThreadSpecificData *tsdPtr = TclThreadDataKeyGet(&dataKey);
/*
* A non blocking socket waiting for an asyncronous connect
* returns directly an error
*/
if (infoPtr->flags & SOCKET_ASYNC) {
|
|
|
|
|
|
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
|
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* WaitForConnect --
*
* Terminate an asyncroneous connect syncroneously.
* This routine should only be called if flag ASYNC_CONNECT is set.
*
* Results:
* Returns -1 on success or 0 on failure, with an error code in
* errorCodePtr.
*
* Side effects:
* Processes socket events off the system queue.
*
*----------------------------------------------------------------------
*/
static int
WaitForConnect(
SocketInfo *infoPtr, /* Information about this socket. */
int *errorCodePtr) /* Where to store errors? */
{
int result;
int oldMode;
ThreadSpecificData *tsdPtr = TclThreadDataKeyGet(&dataKey);
/*
* A non blocking socket waiting for an asyncronous connect
* returns directly an error
*/
if (infoPtr->flags & SOCKET_ASYNC) {
|
| ︙ | | | ︙ | |
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
|
}
/*
* Check if there is an async connect to terminate
*/
if ( (infoPtr->flags & SOCKET_REENTER_PENDING)
&& !WaitForAsyncConnect(infoPtr, errorCodePtr)) {
return -1;
}
/*
* No EOF, and it is connected, so try to read more from the socket. Note
* that we clear the FD_READ bit because read events are level triggered
* so a new event will be generated if there is still data available to be
|
|
|
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
|
}
/*
* Check if there is an async connect to terminate
*/
if ( (infoPtr->flags & SOCKET_REENTER_PENDING)
&& !WaitForConnect(infoPtr, errorCodePtr)) {
return -1;
}
/*
* No EOF, and it is connected, so try to read more from the socket. Note
* that we clear the FD_READ bit because read events are level triggered
* so a new event will be generated if there is still data available to be
|
| ︙ | | | ︙ | |
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
|
}
/*
* Check if there is an async connect to terminate
*/
if ( (infoPtr->flags & SOCKET_REENTER_PENDING)
&& !WaitForAsyncConnect(infoPtr, errorCodePtr)) {
return -1;
}
while (1) {
SendMessage(tsdPtr->hwnd, SOCKET_SELECT,
(WPARAM) UNSELECT, (LPARAM) infoPtr);
|
|
|
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
|
}
/*
* Check if there is an async connect to terminate
*/
if ( (infoPtr->flags & SOCKET_REENTER_PENDING)
&& !WaitForConnect(infoPtr, errorCodePtr)) {
return -1;
}
while (1) {
SendMessage(tsdPtr->hwnd, SOCKET_SELECT,
(WPARAM) UNSELECT, (LPARAM) infoPtr);
|
| ︙ | | | ︙ | |
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
|
UINT message,
WPARAM wParam,
LPARAM lParam)
{
int event, error;
SOCKET socket;
SocketInfo *infoPtr = NULL; /* DEBUG */
TcpFdList *fds = NULL;
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
#ifdef _WIN64
GetWindowLongPtr(hwnd, GWLP_USERDATA);
#else
GetWindowLong(hwnd, GWL_USERDATA);
#endif
|
>
|
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
|
UINT message,
WPARAM wParam,
LPARAM lParam)
{
int event, error;
SOCKET socket;
SocketInfo *infoPtr = NULL; /* DEBUG */
int info_found = 0;
TcpFdList *fds = NULL;
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
#ifdef _WIN64
GetWindowLongPtr(hwnd, GWLP_USERDATA);
#else
GetWindowLong(hwnd, GWL_USERDATA);
#endif
|
| ︙ | | | ︙ | |
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
|
break;
case SOCKET_MESSAGE:
event = WSAGETSELECTEVENT(lParam);
error = WSAGETSELECTERROR(lParam);
socket = (SOCKET) wParam;
/*
* Find the specified socket on the socket list and update its
* eventState flag.
*/
DEBUG("FOO");
WaitForSingleObject(tsdPtr->socketListLock, INFINITE);
DEBUG("BAR");
for (infoPtr = tsdPtr->socketList; infoPtr != NULL;
infoPtr = infoPtr->nextPtr) {
for (fds = infoPtr->sockets; fds != NULL; fds = fds->next) {
#ifdef DEBUGGING
fprintf(stderr,"socket = %d, fd=%d, event = %d, error = %d\n",
socket, fds->fd, event, error);
#endif
if (fds->fd == socket) {
if (event & FD_READ)
DEBUG("|->FD_READ");
if (event & FD_WRITE)
DEBUG("|->FD_WRITE");
/*
* Update the socket state.
*
* A count of FD_ACCEPTS is stored, so if an FD_CLOSE event
* happens, then clear the FD_ACCEPT count. Otherwise,
* increment the count if the current event is an FD_ACCEPT.
*/
if (event & FD_CLOSE) {
DEBUG("FD_CLOSE");
infoPtr->acceptEventCount = 0;
infoPtr->readyEvents &= ~(FD_WRITE|FD_ACCEPT);
} else if (event & FD_ACCEPT) {
DEBUG("FD_ACCEPT");
infoPtr->acceptEventCount++;
}
if (event & FD_CONNECT) {
DEBUG("FD_CONNECT");
/*
* Remember any error that occurred so we can report
* connection failures.
*/
if (error != ERROR_SUCCESS) {
TclWinConvertError((DWORD) error);
infoPtr->lastError = Tcl_GetErrno();
}
}
/*
* Inform main thread about signaled events
*/
infoPtr->readyEvents |= event;
/*
* Wake up the Main Thread.
*/
SetEvent(tsdPtr->readyEvent);
Tcl_ThreadAlert(tsdPtr->threadId);
break;
}
}
}
SetEvent(tsdPtr->socketListLock);
break;
case SOCKET_SELECT:
DEBUG("SOCKET_SELECT");
infoPtr = (SocketInfo *) lParam;
|
>
>
>
>
>
>
>
>
>
>
>
>
>
<
<
<
|
>
>
|
>
>
>
>
>
>
>
>
|
>
|
|
>
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<
<
<
|
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
|
break;
case SOCKET_MESSAGE:
event = WSAGETSELECTEVENT(lParam);
error = WSAGETSELECTERROR(lParam);
socket = (SOCKET) wParam;
#ifdef DEBUGGING
fprintf(stderr,"event = %d, error=%d\n",event,error);
#endif
if (event & FD_READ) DEBUG("READ Event");
if (event & FD_WRITE) DEBUG("WRITE Event");
if (event & FD_CLOSE) DEBUG("CLOSE Event");
if (event & FD_CONNECT)
DEBUG("CONNECT Event");
if (event & FD_ACCEPT) DEBUG("ACCEPT Event");
DEBUG("Get list lock");
WaitForSingleObject(tsdPtr->socketListLock, INFINITE);
/*
* Find the specified socket on the socket list and update its
* eventState flag.
*/
for (infoPtr = tsdPtr->socketList; infoPtr != NULL;
infoPtr = infoPtr->nextPtr) {
DEBUG("Cur InfoPtr");
if ( FindFDInList(infoPtr,socket) ) {
info_found = 1;
DEBUG("InfoPtr found");
break;
}
}
/*
* Check if there is a pending info structure not jet in the
* list
*/
if ( !info_found
&& tsdPtr->pendingSocketInfo != NULL
&& FindFDInList(tsdPtr->pendingSocketInfo,socket) ) {
infoPtr = tsdPtr->pendingSocketInfo;
DEBUG("Pending InfoPtr found");
info_found = 1;
}
if (info_found) {
if (event & FD_READ)
DEBUG("|->FD_READ");
if (event & FD_WRITE)
DEBUG("|->FD_WRITE");
/*
* Update the socket state.
*
* A count of FD_ACCEPTS is stored, so if an FD_CLOSE event
* happens, then clear the FD_ACCEPT count. Otherwise,
* increment the count if the current event is an FD_ACCEPT.
*/
if (event & FD_CLOSE) {
DEBUG("FD_CLOSE");
infoPtr->acceptEventCount = 0;
infoPtr->readyEvents &= ~(FD_WRITE|FD_ACCEPT);
} else if (event & FD_ACCEPT) {
DEBUG("FD_ACCEPT");
infoPtr->acceptEventCount++;
}
if (event & FD_CONNECT) {
DEBUG("FD_CONNECT");
/*
* Remember any error that occurred so we can report
* connection failures.
*/
if (error != ERROR_SUCCESS) {
TclWinConvertError((DWORD) error);
infoPtr->lastError = Tcl_GetErrno();
}
}
/*
* Inform main thread about signaled events
*/
infoPtr->readyEvents |= event;
/*
* Wake up the Main Thread.
*/
SetEvent(tsdPtr->readyEvent);
Tcl_ThreadAlert(tsdPtr->threadId);
}
SetEvent(tsdPtr->socketListLock);
break;
case SOCKET_SELECT:
DEBUG("SOCKET_SELECT");
infoPtr = (SocketInfo *) lParam;
|
| ︙ | | | ︙ | |
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
|
DEBUG("SOCKET_TERMINATE");
DestroyWindow(hwnd);
break;
}
return 0;
}
/*
*----------------------------------------------------------------------
*
* Tcl_GetHostName --
*
* Returns the name of the local host.
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
|
DEBUG("SOCKET_TERMINATE");
DestroyWindow(hwnd);
break;
}
return 0;
}
/*
*----------------------------------------------------------------------
*
* FindFDInList --
*
* Return true, if the given file descriptior is contained in the
* file descriptor list.
*
* Results:
* true if found.
*
* Side effects:
*
*----------------------------------------------------------------------
*/
static int
FindFDInList(
SocketInfo *infoPtr,
SOCKET socket)
{
TcpFdList *fds;
for (fds = infoPtr->sockets; fds != NULL; fds = fds->next) {
#ifdef DEBUGGING
fprintf(stderr,"socket = %d, fd=%d",socket,fds);
#endif
if (fds->fd == socket) {
return 1;
}
}
return 0;
}
/*
*----------------------------------------------------------------------
*
* Tcl_GetHostName --
*
* Returns the name of the local host.
|
| ︙ | | | ︙ | |
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
|
Tcl_MutexLock(&socketMutex);
InitSockets();
Tcl_MutexUnlock(&socketMutex);
tsdPtr = TCL_TSD_INIT(&dataKey);
WaitForSingleObject(tsdPtr->socketListLock, INFINITE);
infoPtr->nextPtr = tsdPtr->socketList;
tsdPtr->socketList = infoPtr;
SetEvent(tsdPtr->socketListLock);
notifyCmd = SELECT;
} else {
SocketInfo **nextPtrPtr;
int removed = 0;
tsdPtr = TCL_TSD_INIT(&dataKey);
/*
* TIP #218, Bugfix: All access to socketList has to be protected by
* the lock.
*/
WaitForSingleObject(tsdPtr->socketListLock, INFINITE);
for (nextPtrPtr = &(tsdPtr->socketList); (*nextPtrPtr) != NULL;
nextPtrPtr = &((*nextPtrPtr)->nextPtr)) {
if ((*nextPtrPtr) == infoPtr) {
(*nextPtrPtr) = infoPtr->nextPtr;
removed = 1;
break;
}
|
>
>
>
>
>
>
>
>
|
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
|
Tcl_MutexLock(&socketMutex);
InitSockets();
Tcl_MutexUnlock(&socketMutex);
tsdPtr = TCL_TSD_INIT(&dataKey);
WaitForSingleObject(tsdPtr->socketListLock, INFINITE);
DEBUG("Inserting pointer to list");
infoPtr->nextPtr = tsdPtr->socketList;
tsdPtr->socketList = infoPtr;
if (infoPtr == tsdPtr->pendingSocketInfo) {
DEBUG("Clearing temporary info pointer");
tsdPtr->pendingSocketInfo = NULL;
}
SetEvent(tsdPtr->socketListLock);
notifyCmd = SELECT;
} else {
SocketInfo **nextPtrPtr;
int removed = 0;
tsdPtr = TCL_TSD_INIT(&dataKey);
/*
* TIP #218, Bugfix: All access to socketList has to be protected by
* the lock.
*/
WaitForSingleObject(tsdPtr->socketListLock, INFINITE);
DEBUG("Removing pointer from list");
for (nextPtrPtr = &(tsdPtr->socketList); (*nextPtrPtr) != NULL;
nextPtrPtr = &((*nextPtrPtr)->nextPtr)) {
if ((*nextPtrPtr) == infoPtr) {
(*nextPtrPtr) = infoPtr->nextPtr;
removed = 1;
break;
}
|
| ︙ | | | ︙ | |