1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
/*
* tclWinSock.c --
*
* This file contains Windows-specific socket related code.
*
* Copyright (c) 1995-1997 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* RCS: @(#) $Id: tclWinSock.c,v 1.10 1999/05/26 20:24:43 redman Exp $
*/
#include "tclWinInt.h"
/*
* The following variable is used to tell whether this module has been
* initialized.
*/
static int initialized = 0;
static int hostnameInitialized = 0;
static char hostname[255]; /* This buffer should be big enough for
* hostname plus domain name. */
static int useThreads = 0;
static HANDLE socketThread; /* Thread used with WSAEventSelect to check
sockets for fileevents on NT (not used for
Win95 or Win98) */
static WSAEVENT socketEvent; /* Event triggered by WSAEventSelect, for all
sockets, NT only */
static WSAEVENT killEvent; /* Event used to kill off the socketThread, NT
only */
static CRITICAL_SECTION socketCS; /* Critical Section used even when building
without threads */
TCL_DECLARE_MUTEX(socketMutex) /* Mutex used when built with multithreading
*/
/*
* The following structure contains pointers to all of the WinSock API entry
* points used by Tcl. It is initialized by InitSockets. Since we
* dynamically load Winsock.dll on demand, we must use this function table
* to refer to functions in the socket API.
*/
|
|
<
<
<
<
<
<
<
<
<
<
<
<
|
<
<
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
/*
* tclWinSock.c --
*
* This file contains Windows-specific socket related code.
*
* Copyright (c) 1995-1997 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* RCS: @(#) $Id: tclWinSock.c,v 1.11 1999/06/08 19:02:15 stanton Exp $
*/
#include "tclWinInt.h"
/*
* The following variable is used to tell whether this module has been
* initialized.
*/
static int initialized = 0;
static int hostnameInitialized = 0;
static char hostname[255]; /* This buffer should be big enough for
* hostname plus domain name. */
TCL_DECLARE_MUTEX(socketMutex)
/*
* The following structure contains pointers to all of the WinSock API entry
* points used by Tcl. It is initialized by InitSockets. Since we
* dynamically load Winsock.dll on demand, we must use this function table
* to refer to functions in the socket API.
*/
|
| ︙ | | | ︙ | |
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
int (PASCAL FAR *getsockname)(SOCKET sock, struct sockaddr FAR *name,
int FAR *namelen);
int (PASCAL FAR *WSAStartup)(WORD wVersionRequired, LPWSADATA lpWSAData);
int (PASCAL FAR *WSACleanup)(void);
int (PASCAL FAR *WSAGetLastError)(void);
int (PASCAL FAR *WSAAsyncSelect)(SOCKET s, HWND hWnd, u_int wMsg,
long lEvent);
/*
* The following are used for the WinSock 2.0 for NT implementation
*/
WSAEVENT (PASCAL FAR *WSACreateEvent)(void);
BOOL (PASCAL FAR *WSACloseEvent)(WSAEVENT event);
int (PASCAL FAR *WSAEventSelect)(SOCKET s, WSAEVENT event, long lEvent);
BOOL (PASCAL FAR *WSAResetEvent)(WSAEVENT event);
BOOL (PASCAL FAR *WSASetEvent)(WSAEVENT event);
int (PASCAL FAR *WSAEnumNetworkEvents)(SOCKET s, WSAEVENT event,
LPWSANETWORKEVENTS events);
DWORD (PASCAL FAR *WSAWaitForMultipleEvents)(DWORD cEvents,
const WSAEVENT FAR *events,
BOOL fWaitAll, DWORD dwTimeOUT, BOOL fAlertable);
} winSock;
/*
* The following defines declare the messages used on socket windows.
*/
#define SOCKET_MESSAGE WM_USER+1
/*
* The following defines the timeout value for the WSAWaitForMultipleEvents
* in milliseconds.
*/
#define TCL_SOCKET_EVENT_TIMEOUT 100
/*
* The following structure is used to store the data associated with
* each socket.
*/
typedef struct SocketInfo {
Tcl_Channel channel; /* Channel associated with this socket. */
SOCKET socket; /* Windows SOCKET handle. */
int flags; /* Bit field comprised of the flags
* described below. */
int watchEvents; /* OR'ed combination of FD_READ, FD_WRITE,
* FD_CLOSE, FD_ACCEPT and FD_CONNECT that
* indicate which events are interesting. */
int readyEvents; /* OR'ed combination of FD_READ, FD_WRITE,
* FD_CLOSE, FD_ACCEPT and FD_CONNECT that
* indicate which events have occurred. Not
* used for the WinSock 2.0 for NT
* implementation. */
int selectEvents; /* OR'ed combination of FD_READ, FD_WRITE,
* FD_CLOSE, FD_ACCEPT and FD_CONNECT that
* indicate which events are currently
* being selected. */
Tcl_TcpAcceptProc *acceptProc; /* Proc to call on accept. */
ClientData acceptProcData; /* The data for the accept proc. */
int lastError; /* Error code from last message. */
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
<
<
|
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
int (PASCAL FAR *getsockname)(SOCKET sock, struct sockaddr FAR *name,
int FAR *namelen);
int (PASCAL FAR *WSAStartup)(WORD wVersionRequired, LPWSADATA lpWSAData);
int (PASCAL FAR *WSACleanup)(void);
int (PASCAL FAR *WSAGetLastError)(void);
int (PASCAL FAR *WSAAsyncSelect)(SOCKET s, HWND hWnd, u_int wMsg,
long lEvent);
} winSock;
/*
* The following defines declare the messages used on socket windows.
*/
#define SOCKET_MESSAGE WM_USER+1
/*
* The following structure is used to store the data associated with
* each socket.
*/
typedef struct SocketInfo {
Tcl_Channel channel; /* Channel associated with this socket. */
SOCKET socket; /* Windows SOCKET handle. */
int flags; /* Bit field comprised of the flags
* described below. */
int watchEvents; /* OR'ed combination of FD_READ, FD_WRITE,
* FD_CLOSE, FD_ACCEPT and FD_CONNECT that
* indicate which events are interesting. */
int readyEvents; /* OR'ed combination of FD_READ, FD_WRITE,
* FD_CLOSE, FD_ACCEPT and FD_CONNECT that
* indicate which events have occurred. */
int selectEvents; /* OR'ed combination of FD_READ, FD_WRITE,
* FD_CLOSE, FD_ACCEPT and FD_CONNECT that
* indicate which events are currently
* being selected. */
Tcl_TcpAcceptProc *acceptProc; /* Proc to call on accept. */
ClientData acceptProcData; /* The data for the accept proc. */
int lastError; /* Error code from last message. */
|
| ︙ | | | ︙ | |
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
#define SOCKET_EOF (1<<1) /* A zero read happened on
* the socket. */
#define SOCKET_ASYNC_CONNECT (1<<2) /* This socket uses async connect. */
#define SOCKET_PENDING (1<<3) /* A message has been sent
* for this socket */
typedef struct ThreadSpecificData {
SocketInfo *socketList; /* List of all sockets in the thread */
/*
* Data need for the Win95/Win98 implementation:
*/
HWND hwnd; /* Handle to window for socket messages. */
/*
* Data need for the WinNT implementation:
*/
HANDLE wakeEvent; /* Event to wake up the socket thread */
Tcl_ThreadId threadId; /* ID of the thread */
struct ThreadSpecificData *next; /* Next entry in the thread list */
} ThreadSpecificData;
ThreadSpecificData *firstWaitingThread; /* List of threads */
static Tcl_ThreadDataKey dataKey;
/*
* Static functions defined in this file.
*/
static SocketInfo * CreateSocket _ANSI_ARGS_((Tcl_Interp *interp,
|
<
<
<
>
|
<
<
<
<
<
<
<
<
<
<
|
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
#define SOCKET_EOF (1<<1) /* A zero read happened on
* the socket. */
#define SOCKET_ASYNC_CONNECT (1<<2) /* This socket uses async connect. */
#define SOCKET_PENDING (1<<3) /* A message has been sent
* for this socket */
typedef struct ThreadSpecificData {
/*
* Every open socket has an entry on the following list.
*/
HWND hwnd; /* Handle to window for socket messages. */
SocketInfo *socketList;
} ThreadSpecificData;
static Tcl_ThreadDataKey dataKey;
/*
* Static functions defined in this file.
*/
static SocketInfo * CreateSocket _ANSI_ARGS_((Tcl_Interp *interp,
|
| ︙ | | | ︙ | |
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
static void SocketCheckProc _ANSI_ARGS_((ClientData clientData,
int flags));
static int SocketEventProc _ANSI_ARGS_((Tcl_Event *evPtr,
int flags));
static void SocketExitHandler _ANSI_ARGS_((ClientData clientData));
static LRESULT CALLBACK SocketProc _ANSI_ARGS_((HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam));
static DWORD WINAPI SocketThread(LPVOID arg);
static void SocketSetupProc _ANSI_ARGS_((ClientData clientData,
int flags));
static void SocketThreadExitHandler _ANSI_ARGS_((ClientData clientData));
static int SocketsEnabled _ANSI_ARGS_((void));
static void TcpAccept _ANSI_ARGS_((SocketInfo *infoPtr));
static int TcpBlockProc _ANSI_ARGS_((ClientData instanceData,
int mode));
|
<
|
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
static void SocketCheckProc _ANSI_ARGS_((ClientData clientData,
int flags));
static int SocketEventProc _ANSI_ARGS_((Tcl_Event *evPtr,
int flags));
static void SocketExitHandler _ANSI_ARGS_((ClientData clientData));
static LRESULT CALLBACK SocketProc _ANSI_ARGS_((HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam));
static void SocketSetupProc _ANSI_ARGS_((ClientData clientData,
int flags));
static void SocketThreadExitHandler _ANSI_ARGS_((ClientData clientData));
static int SocketsEnabled _ANSI_ARGS_((void));
static void TcpAccept _ANSI_ARGS_((SocketInfo *infoPtr));
static int TcpBlockProc _ANSI_ARGS_((ClientData instanceData,
int mode));
|
| ︙ | | | ︙ | |
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
|
/*
* Define version of Winsock required by Tcl.
*/
#define WSA_VERSION_REQD MAKEWORD(1,1)
/*
*----------------------------------------------------------------------
*
* GetSocketState --
*
* Retrieve the state of a given socket from the winsock API
* using WSAEnumNetWorkEvents. Modify the state appropriately
* when the winsock version is wrong.
*
* Assumes Mutex is held.
*
* Results:
* State of the socket in FD_* masks.
*
* Side effects:
* Modifies the infoPtr values to reflect the current state.
*
*----------------------------------------------------------------------
*/
int GetSocketState(SocketInfo *infoPtr)
{
WSANETWORKEVENTS netEvents;
int event = 0;
if (!useThreads) {
return infoPtr->readyEvents;
}
EnterCriticalSection(&socketCS);
if ((*winSock.WSAEnumNetworkEvents)(infoPtr->socket,
socketEvent, &netEvents) == 0) {
event = netEvents.lNetworkEvents;
if (event & FD_CLOSE) {
event &= ~(FD_WRITE|FD_ACCEPT);
}
if (event & FD_CONNECT) {
/*
* The socket is now connected, so clear the
* async connect flag.
*/
infoPtr->flags &= ~(SOCKET_ASYNC_CONNECT);
}
if(infoPtr->flags & SOCKET_ASYNC_CONNECT) {
infoPtr->flags &= ~(SOCKET_ASYNC_CONNECT);
event |= FD_WRITE;
}
}
LeaveCriticalSection(&socketCS);
return event;
}
/*
*----------------------------------------------------------------------
*
* InitSockets --
*
* Initialize the socket module. Attempts to load the wsock32.dll
* library and set up the winSock function table. If successful,
* registers the event window for the socket notifier code.
*
* Results:
* None.
*
* Side effects:
* Dynamically loads wsock32.dll, and registers a new window
* class and creates a window for use in asynchronous socket
* notification.
*
*----------------------------------------------------------------------
*/
static void
InitSockets()
{
WSADATA wsaData;
DWORD id;
OSVERSIONINFO info;
static WNDCLASSA class;
ThreadSpecificData *tsdPtr =
(ThreadSpecificData *)TclThreadDataKeyGet(&dataKey);
Tcl_MutexLock(&socketMutex);
if (! initialized) {
InitializeCriticalSection(&socketCS);
initialized = 1;
Tcl_CreateExitHandler(SocketExitHandler, (ClientData) NULL);
/*
* Find out if we're running on Win32s.
*/
info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&info);
/*
* Check to see if Sockets are supported on this system.
*
* For WinNT, use the WinSock 2.0 API to avoid creating extra windows
* to handle socket events.
*
* Not all versions of Win95 have WinSock 2.0, don't count on it being
* there. Also, until someone can get the code to work on Win98 or
* Win95 with WinSock 2.0, use the window-based version for those
* OS's. Major socket users (servers) will probably be on NT anyway.
*/
if ((info.dwPlatformId == VER_PLATFORM_WIN32_NT)&&
(SearchPathA(NULL, "ws2_32", ".dll", 0, NULL, NULL) != 0) ) {
useThreads = 1;
winSock.hInstance = LoadLibraryA("ws2_32.dll");
} else if (SearchPathA(NULL, "wsock32", ".dll", 0, NULL, NULL) != 0) {
winSock.hInstance = LoadLibraryA("wsock32.dll");
} else {
winSock.hInstance = NULL;
}
/*
* Initialize the function table.
*/
if (!SocketsEnabled()) {
Tcl_MutexUnlock(&socketMutex);
return;
}
winSock.accept = (SOCKET (PASCAL FAR *)(SOCKET s,
struct sockaddr FAR *addr, int FAR *addrlen))
GetProcAddress(winSock.hInstance, "accept");
winSock.bind = (int (PASCAL FAR *)(SOCKET s,
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
>
>
<
<
<
<
|
|
|
|
|
<
<
<
<
|
|
<
<
<
<
|
214
215
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
/*
* Define version of Winsock required by Tcl.
*/
#define WSA_VERSION_REQD MAKEWORD(1,1)
/*
*----------------------------------------------------------------------
*
* InitSockets --
*
* Initialize the socket module. Attempts to load the wsock32.dll
* library and set up the winSock function table. If successful,
* registers the event window for the socket notifier code.
*
* Assumes Mutex is held.
*
* Results:
* None.
*
* Side effects:
* Dynamically loads wsock32.dll, and registers a new window
* class and creates a window for use in asynchronous socket
* notification.
*
*----------------------------------------------------------------------
*/
static void
InitSockets()
{
WSADATA wsaData;
OSVERSIONINFO info;
static WNDCLASSA class;
ThreadSpecificData *tsdPtr =
(ThreadSpecificData *)TclThreadDataKeyGet(&dataKey);
if (! initialized) {
initialized = 1;
Tcl_CreateExitHandler(SocketExitHandler, (ClientData) NULL);
/*
* Find out if we're running on Win32s.
*/
info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&info);
/*
* Check to see if Sockets are supported on this system. Since
* win32s panics if we call WSAStartup on a system that doesn't
* have winsock.dll, we need to look for it on the system first.
* If we find winsock, then load the library and initialize the
* stub table.
*/
if ((info.dwPlatformId != VER_PLATFORM_WIN32s)
|| (SearchPathA(NULL, "WINSOCK", ".DLL", 0, NULL, NULL) != 0)) {
winSock.hInstance = LoadLibraryA("wsock32.dll");
} else {
winSock.hInstance = NULL;
}
/*
* Initialize the function table.
*/
if (!SocketsEnabled()) {
return;
}
winSock.accept = (SOCKET (PASCAL FAR *)(SOCKET s,
struct sockaddr FAR *addr, int FAR *addrlen))
GetProcAddress(winSock.hInstance, "accept");
winSock.bind = (int (PASCAL FAR *)(SOCKET s,
|
| ︙ | | | ︙ | |
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
|
winSock.getservbyname = (struct servent FAR * (PASCAL FAR *)
(const char FAR * name, const char FAR * proto))
GetProcAddress(winSock.hInstance, "getservbyname");
winSock.getsockname = (int (PASCAL FAR *)(SOCKET sock,
struct sockaddr FAR *name, int FAR *namelen))
GetProcAddress(winSock.hInstance, "getsockname");
winSock.WSAStartup = (int (PASCAL FAR *)(WORD wVersionRequired,
LPWSADATA lpWSAData)) GetProcAddress(winSock.hInstance,
"WSAStartup");
winSock.WSACleanup = (int (PASCAL FAR *)(void))
GetProcAddress(winSock.hInstance, "WSACleanup");
winSock.WSAGetLastError = (int (PASCAL FAR *)(void))
GetProcAddress(winSock.hInstance, "WSAGetLastError");
winSock.WSAAsyncSelect = (int (PASCAL FAR *)(SOCKET s, HWND hWnd,
u_int wMsg, long lEvent))
GetProcAddress(winSock.hInstance, "WSAAsyncSelect");
if (useThreads) {
winSock.WSACreateEvent = (WSAEVENT (PASCAL FAR *)(void))
GetProcAddress(winSock.hInstance, "WSACreateEvent");
winSock.WSACloseEvent = (BOOL (PASCAL FAR *)(WSAEVENT event))
GetProcAddress(winSock.hInstance, "WSACloseEvent");
winSock.WSAEventSelect = (int (PASCAL FAR *)(SOCKET s,
WSAEVENT event,
long lEvent))
GetProcAddress(winSock.hInstance, "WSAEventSelect");
winSock.WSAResetEvent = (BOOL (PASCAL FAR *)(WSAEVENT event))
GetProcAddress(winSock.hInstance, "WSAResetEvent");
winSock.WSASetEvent = (BOOL (PASCAL FAR *)(WSAEVENT event))
GetProcAddress(winSock.hInstance, "WSASetEvent");
winSock.WSAEnumNetworkEvents = (int (PASCAL FAR *)(SOCKET s,
WSAEVENT event,
LPWSANETWORKEVENTS events))
GetProcAddress(winSock.hInstance, "WSAEnumNetworkEvents");
winSock.WSAWaitForMultipleEvents =
(DWORD (PASCAL FAR *)(DWORD cEvents,
const WSAEVENT FAR *events,
BOOL fWaitAll, DWORD dwTimeOUT, BOOL fAlertable))
GetProcAddress(winSock.hInstance, "WSAWaitForMultipleEvents");
}
/*
* Now check that all fields are properly initialized. If not, return
* zero to indicate that we failed to initialize properly.
*/
if ((winSock.hInstance == NULL) ||
(winSock.accept == NULL) ||
|
|
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
winSock.getservbyname = (struct servent FAR * (PASCAL FAR *)
(const char FAR * name, const char FAR * proto))
GetProcAddress(winSock.hInstance, "getservbyname");
winSock.getsockname = (int (PASCAL FAR *)(SOCKET sock,
struct sockaddr FAR *name, int FAR *namelen))
GetProcAddress(winSock.hInstance, "getsockname");
winSock.WSAStartup = (int (PASCAL FAR *)(WORD wVersionRequired,
LPWSADATA lpWSAData)) GetProcAddress(winSock.hInstance, "WSAStartup");
winSock.WSACleanup = (int (PASCAL FAR *)(void))
GetProcAddress(winSock.hInstance, "WSACleanup");
winSock.WSAGetLastError = (int (PASCAL FAR *)(void))
GetProcAddress(winSock.hInstance, "WSAGetLastError");
winSock.WSAAsyncSelect = (int (PASCAL FAR *)(SOCKET s, HWND hWnd,
u_int wMsg, long lEvent))
GetProcAddress(winSock.hInstance, "WSAAsyncSelect");
/*
* Now check that all fields are properly initialized. If not, return
* zero to indicate that we failed to initialize properly.
*/
if ((winSock.hInstance == NULL) ||
(winSock.accept == NULL) ||
|
| ︙ | | | ︙ | |
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
|
(winSock.getservbyname == NULL) ||
(winSock.getsockname == NULL) ||
(winSock.WSAStartup == NULL) ||
(winSock.WSACleanup == NULL) ||
(winSock.WSAGetLastError == NULL) ||
(winSock.WSAAsyncSelect == NULL)) {
goto unloadLibrary;
} else if (useThreads &&
((winSock.WSACreateEvent == NULL) ||
(winSock.WSACloseEvent == NULL) ||
(winSock.WSAGetLastError == NULL) ||
(winSock.WSAEventSelect == NULL) ||
(winSock.WSAResetEvent == NULL) ||
(winSock.WSASetEvent == NULL) ||
(winSock.WSAEnumNetworkEvents == NULL) ||
(winSock.WSAWaitForMultipleEvents == NULL))) {
/*
* WinSock 2.0 not correctly installed, use 1.x
*/
useThreads = 0;
}
/*
* Create the async notification window with a new class. We
* must create a new class to avoid a Windows 95 bug that causes
* us to get the wrong message number for socket events if the
* message window is a subclass of a static control.
*/
if (!useThreads) {
class.style = 0;
class.cbClsExtra = 0;
class.cbWndExtra = 0;
class.hInstance = TclWinGetTclInstance();
class.hbrBackground = NULL;
class.lpszMenuName = NULL;
class.lpszClassName = "TclSocket";
class.lpfnWndProc = SocketProc;
class.hIcon = NULL;
class.hCursor = NULL;
if (!RegisterClassA(&class)) {
TclWinConvertError(GetLastError());
(*winSock.WSACleanup)();
goto unloadLibrary;
}
}
/*
* Initialize the winsock library and check the version number.
*/
if ((*winSock.WSAStartup)(WSA_VERSION_REQD, &wsaData) != 0) {
goto unloadLibrary;
}
if (wsaData.wVersion != WSA_VERSION_REQD) {
(*winSock.WSACleanup)();
goto unloadLibrary;
}
if (useThreads) {
socketEvent = (*winSock.WSACreateEvent)();
killEvent = (*winSock.WSACreateEvent)();
socketThread = CreateThread(NULL, 8000, SocketThread,
NULL, 0, &id);
SetThreadPriority(socketThread, THREAD_PRIORITY_HIGHEST);
}
}
Tcl_MutexUnlock(&socketMutex);
/*
* Check for per-thread initialization.
*/
EnterCriticalSection(&socketCS);
if (tsdPtr == NULL) {
tsdPtr = TCL_TSD_INIT(&dataKey);
tsdPtr->socketList = NULL;
if (useThreads) {
tsdPtr->wakeEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
} else {
tsdPtr->hwnd = CreateWindowA("TclSocket", "TclSocket",
WS_TILED, 0, 0, 0, 0, NULL, NULL, class.hInstance, NULL);
if (tsdPtr->hwnd == NULL) {
FreeLibrary(winSock.hInstance);
winSock.hInstance = NULL;
LeaveCriticalSection(&socketCS);
return;
}
}
Tcl_CreateEventSource(SocketSetupProc, SocketCheckProc, NULL);
Tcl_CreateThreadExitHandler(SocketThreadExitHandler, NULL);
tsdPtr->threadId = Tcl_GetCurrentThread();
tsdPtr->next = firstWaitingThread;
firstWaitingThread = tsdPtr;
}
LeaveCriticalSection(&socketCS);
return;
unloadLibrary:
FreeLibrary(winSock.hInstance);
winSock.hInstance = NULL;
Tcl_MutexUnlock(&socketMutex);
return;
}
/*
*----------------------------------------------------------------------
*
* SocketsEnabled --
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<
|
|
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
|
>
|
|
<
<
<
<
|
<
<
<
<
<
<
<
|
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
|
(winSock.getservbyname == NULL) ||
(winSock.getsockname == NULL) ||
(winSock.WSAStartup == NULL) ||
(winSock.WSACleanup == NULL) ||
(winSock.WSAGetLastError == NULL) ||
(winSock.WSAAsyncSelect == NULL)) {
goto unloadLibrary;
}
/*
* Create the async notification window with a new class. We
* must create a new class to avoid a Windows 95 bug that causes
* us to get the wrong message number for socket events if the
* message window is a subclass of a static control.
*/
class.style = 0;
class.cbClsExtra = 0;
class.cbWndExtra = 0;
class.hInstance = TclWinGetTclInstance();
class.hbrBackground = NULL;
class.lpszMenuName = NULL;
class.lpszClassName = "TclSocket";
class.lpfnWndProc = SocketProc;
class.hIcon = NULL;
class.hCursor = NULL;
if (!RegisterClassA(&class)) {
TclWinConvertError(GetLastError());
(*winSock.WSACleanup)();
goto unloadLibrary;
}
/*
* Initialize the winsock library and check the version number.
*/
if ((*winSock.WSAStartup)(WSA_VERSION_REQD, &wsaData) != 0) {
goto unloadLibrary;
}
if (wsaData.wVersion != WSA_VERSION_REQD) {
(*winSock.WSACleanup)();
goto unloadLibrary;
}
}
/*
* Check for per-thread initialization.
*/
if (tsdPtr == NULL) {
tsdPtr = TCL_TSD_INIT(&dataKey);
tsdPtr->socketList = NULL;
tsdPtr->hwnd = CreateWindowA("TclSocket", "TclSocket",
WS_TILED, 0, 0, 0, 0, NULL, NULL, class.hInstance, NULL);
if (tsdPtr->hwnd == NULL) {
goto unloadLibrary;
}
Tcl_CreateEventSource(SocketSetupProc, SocketCheckProc, NULL);
Tcl_CreateThreadExitHandler(SocketThreadExitHandler, NULL);
}
return;
unloadLibrary:
FreeLibrary(winSock.hInstance);
winSock.hInstance = NULL;
return;
}
/*
*----------------------------------------------------------------------
*
* SocketsEnabled --
|
| ︙ | | | ︙ | |
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
|
*/
/* ARGSUSED */
static int
SocketsEnabled()
{
int enabled;
EnterCriticalSection(&socketCS);
enabled = (winSock.hInstance != NULL);
LeaveCriticalSection(&socketCS);
return enabled;
}
/*
*----------------------------------------------------------------------
*
|
|
|
|
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
|
*/
/* ARGSUSED */
static int
SocketsEnabled()
{
int enabled;
Tcl_MutexLock(&socketMutex);
enabled = (winSock.hInstance != NULL);
Tcl_MutexUnlock(&socketMutex);
return enabled;
}
/*
*----------------------------------------------------------------------
*
|
| ︙ | | | ︙ | |
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
|
*/
/* ARGSUSED */
static void
SocketExitHandler(clientData)
ClientData clientData; /* Not used. */
{
EnterCriticalSection(&socketCS);
if (useThreads && (socketThread != NULL)) {
LeaveCriticalSection(&socketCS);
(*winSock.WSASetEvent)(killEvent);
WaitForSingleObject(socketThread, INFINITE);
EnterCriticalSection(&socketCS);
CloseHandle(socketThread);
(*winSock.WSACloseEvent)(killEvent);
(*winSock.WSACloseEvent)(socketEvent);
}
if (winSock.hInstance) {
if (!useThreads) {
UnregisterClassA("TclSocket", TclWinGetTclInstance());
}
(*winSock.WSACleanup)();
FreeLibrary(winSock.hInstance);
winSock.hInstance = NULL;
}
initialized = 0;
hostnameInitialized = 0;
LeaveCriticalSection(&socketCS);
}
/*
*----------------------------------------------------------------------
*
* SocketThreadExitHandler --
*
|
|
<
<
<
<
<
<
<
<
<
<
|
<
|
|
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
|
*/
/* ARGSUSED */
static void
SocketExitHandler(clientData)
ClientData clientData; /* Not used. */
{
Tcl_MutexLock(&socketMutex);
if (winSock.hInstance) {
UnregisterClassA("TclSocket", TclWinGetTclInstance());
(*winSock.WSACleanup)();
FreeLibrary(winSock.hInstance);
winSock.hInstance = NULL;
}
initialized = 0;
hostnameInitialized = 0;
Tcl_MutexUnlock(&socketMutex);
}
/*
*----------------------------------------------------------------------
*
* SocketThreadExitHandler --
*
|
| ︙ | | | ︙ | |
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
|
*/
/* ARGSUSED */
static void
SocketThreadExitHandler(clientData)
ClientData clientData; /* Not used. */
{
ThreadSpecificData *tsdPtr, *nextPtr;
tsdPtr = (ThreadSpecificData *)TclThreadDataKeyGet(&dataKey);
if (useThreads) {
EnterCriticalSection(&socketCS);
CloseHandle(tsdPtr->wakeEvent);
if (firstWaitingThread == tsdPtr) {
firstWaitingThread = tsdPtr->next;
} else {
for (nextPtr = firstWaitingThread;
nextPtr != NULL;
nextPtr = nextPtr->next) {
if (nextPtr->next == tsdPtr) {
nextPtr->next = tsdPtr->next;
break;
}
}
}
LeaveCriticalSection(&socketCS);
} else {
DestroyWindow(tsdPtr->hwnd);
}
Tcl_DeleteEventSource(SocketSetupProc, SocketCheckProc, NULL);
}
/*
*----------------------------------------------------------------------
*
|
|
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
<
|
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
|
*/
/* ARGSUSED */
static void
SocketThreadExitHandler(clientData)
ClientData clientData; /* Not used. */
{
ThreadSpecificData *tsdPtr =
(ThreadSpecificData *)TclThreadDataKeyGet(&dataKey);
DestroyWindow(tsdPtr->hwnd);
Tcl_DeleteEventSource(SocketSetupProc, SocketCheckProc, NULL);
}
/*
*----------------------------------------------------------------------
*
|
| ︙ | | | ︙ | |
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
|
*----------------------------------------------------------------------
*/
int
TclpHasSockets(interp)
Tcl_Interp *interp;
{
InitSockets();
if (SocketsEnabled()) {
return TCL_OK;
}
if (interp != NULL) {
Tcl_AppendResult(interp, "sockets are not available on this system",
NULL);
|
>
>
|
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
|
*----------------------------------------------------------------------
*/
int
TclpHasSockets(interp)
Tcl_Interp *interp;
{
Tcl_MutexLock(&socketMutex);
InitSockets();
Tcl_MutexUnlock(&socketMutex);
if (SocketsEnabled()) {
return TCL_OK;
}
if (interp != NULL) {
Tcl_AppendResult(interp, "sockets are not available on this system",
NULL);
|
| ︙ | | | ︙ | |
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
|
SocketSetupProc(data, flags)
ClientData data; /* Not used. */
int flags; /* Event flags as passed to Tcl_DoOneEvent. */
{
SocketInfo *infoPtr;
Tcl_Time blockTime = { 0, 0 };
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
int readyEvents;
if (!(flags & TCL_FILE_EVENTS)) {
return;
}
/*
* Check to see if there is a ready socket. If so, poll.
*/
EnterCriticalSection(&socketCS);
for (infoPtr = tsdPtr->socketList; infoPtr != NULL;
infoPtr = infoPtr->nextPtr) {
readyEvents = GetSocketState(infoPtr);
if (readyEvents & infoPtr->watchEvents) {
Tcl_SetMaxBlockTime(&blockTime);
break;
}
}
LeaveCriticalSection(&socketCS);
}
/*
*----------------------------------------------------------------------
*
* SocketCheckProc --
*
|
<
|
<
<
|
<
|
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
|
SocketSetupProc(data, flags)
ClientData data; /* Not used. */
int flags; /* Event flags as passed to Tcl_DoOneEvent. */
{
SocketInfo *infoPtr;
Tcl_Time blockTime = { 0, 0 };
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
if (!(flags & TCL_FILE_EVENTS)) {
return;
}
/*
* Check to see if there is a ready socket. If so, poll.
*/
for (infoPtr = tsdPtr->socketList; infoPtr != NULL;
infoPtr = infoPtr->nextPtr) {
if (infoPtr->readyEvents & infoPtr->watchEvents) {
Tcl_SetMaxBlockTime(&blockTime);
break;
}
}
}
/*
*----------------------------------------------------------------------
*
* SocketCheckProc --
*
|
| ︙ | | | ︙ | |
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
|
*/
static void
SocketCheckProc(data, flags)
ClientData data; /* Not used. */
int flags; /* Event flags as passed to Tcl_DoOneEvent. */
{
int readyEvents;
SocketInfo *infoPtr;
SocketEvent *evPtr;
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
if (!(flags & TCL_FILE_EVENTS)) {
return;
}
/*
* Queue events for any ready sockets that don't already have events
* queued (caused by persistent states that won't generate WinSock
* events).
*/
EnterCriticalSection(&socketCS);
for (infoPtr = tsdPtr->socketList; infoPtr != NULL;
infoPtr = infoPtr->nextPtr) {
readyEvents = GetSocketState(infoPtr);
if ((readyEvents & infoPtr->watchEvents)
&& !(infoPtr->flags & SOCKET_PENDING)) {
infoPtr->flags |= SOCKET_PENDING;
evPtr = (SocketEvent *) ckalloc(sizeof(SocketEvent));
evPtr->header.proc = SocketEventProc;
evPtr->socket = infoPtr->socket;
LeaveCriticalSection(&socketCS);
Tcl_QueueEvent((Tcl_Event *) evPtr, TCL_QUEUE_TAIL);
EnterCriticalSection(&socketCS);
}
}
LeaveCriticalSection(&socketCS);
}
/*
*----------------------------------------------------------------------
*
* SocketEventProc --
*
|
<
|
<
<
|
<
<
<
|
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
|
*/
static void
SocketCheckProc(data, flags)
ClientData data; /* Not used. */
int flags; /* Event flags as passed to Tcl_DoOneEvent. */
{
SocketInfo *infoPtr;
SocketEvent *evPtr;
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
if (!(flags & TCL_FILE_EVENTS)) {
return;
}
/*
* Queue events for any ready sockets that don't already have events
* queued (caused by persistent states that won't generate WinSock
* events).
*/
for (infoPtr = tsdPtr->socketList; infoPtr != NULL;
infoPtr = infoPtr->nextPtr) {
if ((infoPtr->readyEvents & infoPtr->watchEvents)
&& !(infoPtr->flags & SOCKET_PENDING)) {
infoPtr->flags |= SOCKET_PENDING;
evPtr = (SocketEvent *) ckalloc(sizeof(SocketEvent));
evPtr->header.proc = SocketEventProc;
evPtr->socket = infoPtr->socket;
Tcl_QueueEvent((Tcl_Event *) evPtr, TCL_QUEUE_TAIL);
}
}
}
/*
*----------------------------------------------------------------------
*
* SocketEventProc --
*
|
| ︙ | | | ︙ | |
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
|
int flags; /* Flags that indicate what events to
* handle, such as TCL_FILE_EVENTS. */
{
SocketInfo *infoPtr;
SocketEvent *eventPtr = (SocketEvent *) evPtr;
int mask = 0;
int events;
int readyEvents;
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
if (!(flags & TCL_FILE_EVENTS)) {
return 0;
}
/*
* Find the specified socket on the socket list.
*/
EnterCriticalSection(&socketCS);
for (infoPtr = tsdPtr->socketList; infoPtr != NULL;
infoPtr = infoPtr->nextPtr) {
if (infoPtr->socket == eventPtr->socket) {
break;
}
}
LeaveCriticalSection(&socketCS);
/*
* Discard events that have gone stale.
*/
if (!infoPtr) {
return 1;
}
infoPtr->flags &= ~SOCKET_PENDING;
/*
* Handle connection requests directly.
*/
readyEvents = GetSocketState(infoPtr);
if (readyEvents & FD_ACCEPT) {
TcpAccept(infoPtr);
return 1;
}
/*
* Mask off unwanted events and compute the read/write mask so
* we can notify the channel.
*/
events = readyEvents & infoPtr->watchEvents;
if (events & FD_CLOSE) {
/*
* If the socket was closed and the channel is still interested
* in read events, then we need to ensure that we keep polling
* for this event until someone does something with the channel.
* Note that we do this before calling Tcl_NotifyChannel so we don't
|
<
<
<
<
|
|
|
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
|
int flags; /* Flags that indicate what events to
* handle, such as TCL_FILE_EVENTS. */
{
SocketInfo *infoPtr;
SocketEvent *eventPtr = (SocketEvent *) evPtr;
int mask = 0;
int events;
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
if (!(flags & TCL_FILE_EVENTS)) {
return 0;
}
/*
* Find the specified socket on the socket list.
*/
for (infoPtr = tsdPtr->socketList; infoPtr != NULL;
infoPtr = infoPtr->nextPtr) {
if (infoPtr->socket == eventPtr->socket) {
break;
}
}
/*
* Discard events that have gone stale.
*/
if (!infoPtr) {
return 1;
}
infoPtr->flags &= ~SOCKET_PENDING;
/*
* Handle connection requests directly.
*/
if (infoPtr->readyEvents & FD_ACCEPT) {
TcpAccept(infoPtr);
return 1;
}
/*
* Mask off unwanted events and compute the read/write mask so
* we can notify the channel.
*/
events = infoPtr->readyEvents & infoPtr->watchEvents;
if (events & FD_CLOSE) {
/*
* If the socket was closed and the channel is still interested
* in read events, then we need to ensure that we keep polling
* for this event until someone does something with the channel.
* Note that we do this before calling Tcl_NotifyChannel so we don't
|
| ︙ | | | ︙ | |
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
|
* We must check to see if data is really available, since someone
* could have consumed the data in the meantime. Turn off async
* notification so select will work correctly. If the socket is
* still readable, notify the channel driver, otherwise reset the
* async select handler and keep waiting.
*/
if (!useThreads) {
(void) (*winSock.WSAAsyncSelect)(infoPtr->socket,
tsdPtr->hwnd, 0, 0);
}
FD_ZERO(&readFds);
FD_SET(infoPtr->socket, &readFds);
timeout.tv_usec = 0;
timeout.tv_sec = 0;
if ((*winSock.select)(0, &readFds, NULL, NULL, &timeout) != 0) {
mask |= TCL_READABLE;
} else if (!useThreads) {
(void) (*winSock.WSAAsyncSelect)(infoPtr->socket, tsdPtr->hwnd,
SOCKET_MESSAGE, infoPtr->selectEvents);
}
infoPtr->readyEvents &= ~(FD_READ);
}
if (events & (FD_WRITE | FD_CONNECT)) {
mask |= TCL_WRITABLE;
}
if (mask) {
Tcl_NotifyChannel(infoPtr->channel, mask);
|
<
|
<
|
<
|
<
|
>
|
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
|
* We must check to see if data is really available, since someone
* could have consumed the data in the meantime. Turn off async
* notification so select will work correctly. If the socket is
* still readable, notify the channel driver, otherwise reset the
* async select handler and keep waiting.
*/
(void) (*winSock.WSAAsyncSelect)(infoPtr->socket, tsdPtr->hwnd, 0, 0);
FD_ZERO(&readFds);
FD_SET(infoPtr->socket, &readFds);
timeout.tv_usec = 0;
timeout.tv_sec = 0;
if ((*winSock.select)(0, &readFds, NULL, NULL, &timeout) != 0) {
mask |= TCL_READABLE;
} else {
(void) (*winSock.WSAAsyncSelect)(infoPtr->socket, tsdPtr->hwnd,
SOCKET_MESSAGE, infoPtr->selectEvents);
infoPtr->readyEvents &= ~(FD_READ);
}
}
if (events & (FD_WRITE | FD_CONNECT)) {
mask |= TCL_WRITABLE;
}
if (mask) {
Tcl_NotifyChannel(infoPtr->channel, mask);
|
| ︙ | | | ︙ | |
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
|
}
}
/*
* Remove the socket from socketList.
*/
EnterCriticalSection(&socketCS);
for (nextPtrPtr = &(tsdPtr->socketList); (*nextPtrPtr) != NULL;
nextPtrPtr = &((*nextPtrPtr)->nextPtr)) {
if ((*nextPtrPtr) == infoPtr) {
(*nextPtrPtr) = infoPtr->nextPtr;
break;
}
}
LeaveCriticalSection(&socketCS);
ckfree((char *) infoPtr);
return errorCode;
}
/*
*----------------------------------------------------------------------
|
<
<
|
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
|
}
}
/*
* Remove the socket from socketList.
*/
for (nextPtrPtr = &(tsdPtr->socketList); (*nextPtrPtr) != NULL;
nextPtrPtr = &((*nextPtrPtr)->nextPtr)) {
if ((*nextPtrPtr) == infoPtr) {
(*nextPtrPtr) = infoPtr->nextPtr;
break;
}
}
ckfree((char *) infoPtr);
return errorCode;
}
/*
*----------------------------------------------------------------------
|
| ︙ | | | ︙ | |
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
|
infoPtr->flags = 0;
infoPtr->watchEvents = 0;
infoPtr->readyEvents = 0;
infoPtr->selectEvents = 0;
infoPtr->acceptProc = NULL;
infoPtr->lastError = 0;
EnterCriticalSection(&socketCS);
infoPtr->nextPtr = tsdPtr->socketList;
tsdPtr->socketList = infoPtr;
LeaveCriticalSection(&socketCS);
return infoPtr;
}
/*
*----------------------------------------------------------------------
*
|
<
<
|
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
|
infoPtr->flags = 0;
infoPtr->watchEvents = 0;
infoPtr->readyEvents = 0;
infoPtr->selectEvents = 0;
infoPtr->acceptProc = NULL;
infoPtr->lastError = 0;
infoPtr->nextPtr = tsdPtr->socketList;
tsdPtr->socketList = infoPtr;
return infoPtr;
}
/*
*----------------------------------------------------------------------
*
|
| ︙ | | | ︙ | |
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
|
infoPtr = NewSocketInfo(sock);
/*
* Set up the select mask for connection request events.
*/
EnterCriticalSection(&socketCS);
infoPtr->selectEvents = FD_ACCEPT;
infoPtr->watchEvents |= FD_ACCEPT;
LeaveCriticalSection(&socketCS);
} else {
/*
* Try to bind to a local port, if specified.
*/
|
<
<
|
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
|
infoPtr = NewSocketInfo(sock);
/*
* Set up the select mask for connection request events.
*/
infoPtr->selectEvents = FD_ACCEPT;
infoPtr->watchEvents |= FD_ACCEPT;
} else {
/*
* Try to bind to a local port, if specified.
*/
|
| ︙ | | | ︙ | |
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
|
infoPtr = NewSocketInfo(sock);
/*
* Set up the select mask for read/write events. If the connect
* attempt has not completed, include connect events.
*/
EnterCriticalSection(&socketCS);
infoPtr->selectEvents = FD_READ | FD_WRITE | FD_CLOSE;
if (asyncConnect) {
infoPtr->flags |= SOCKET_ASYNC_CONNECT;
infoPtr->selectEvents |= FD_CONNECT;
}
LeaveCriticalSection(&socketCS);
}
/*
* Register for interest in events in the select mask. Note that this
* automatically places the socket into non-blocking mode.
*/
if (useThreads) {
(void) (*winSock.WSAEventSelect)(infoPtr->socket, socketEvent,
infoPtr->selectEvents);
} else {
(*winSock.ioctlsocket)(sock, FIONBIO, &flag);
(void) (*winSock.WSAAsyncSelect)(infoPtr->socket, tsdPtr->hwnd,
SOCKET_MESSAGE, infoPtr->selectEvents);
}
return infoPtr;
error:
TclWinConvertWSAError((*winSock.WSAGetLastError)());
if (interp != NULL) {
Tcl_AppendResult(interp, "couldn't open socket: ",
Tcl_PosixError(interp), (char *) NULL);
|
<
<
<
<
<
<
|
|
|
|
<
|
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
|
infoPtr = NewSocketInfo(sock);
/*
* Set up the select mask for read/write events. If the connect
* attempt has not completed, include connect events.
*/
infoPtr->selectEvents = FD_READ | FD_WRITE | FD_CLOSE;
if (asyncConnect) {
infoPtr->flags |= SOCKET_ASYNC_CONNECT;
infoPtr->selectEvents |= FD_CONNECT;
}
}
/*
* Register for interest in events in the select mask. Note that this
* automatically places the socket into non-blocking mode.
*/
(*winSock.ioctlsocket)(sock, FIONBIO, &flag);
(void) (*winSock.WSAAsyncSelect)(infoPtr->socket, tsdPtr->hwnd,
SOCKET_MESSAGE, infoPtr->selectEvents);
return infoPtr;
error:
TclWinConvertWSAError((*winSock.WSAGetLastError)());
if (interp != NULL) {
Tcl_AppendResult(interp, "couldn't open socket: ",
Tcl_PosixError(interp), (char *) NULL);
|
| ︙ | | | ︙ | |
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
|
static int
WaitForSocketEvent(infoPtr, events, errorCodePtr)
SocketInfo *infoPtr; /* Information about this socket. */
int events; /* Events to look for. */
int *errorCodePtr; /* Where to store errors? */
{
MSG msg;
int readyEvents;
int result = 1;
int oldMode;
ThreadSpecificData *tsdPtr =
(ThreadSpecificData *)TclThreadDataKeyGet(&dataKey);
/*
* Be sure to disable event servicing so we are truly modal.
*/
oldMode = Tcl_SetServiceMode(TCL_SERVICE_NONE);
if (useThreads) {
while (1) {
readyEvents = GetSocketState(infoPtr);
if (infoPtr->lastError) {
*errorCodePtr = infoPtr->lastError;
result = 0;
break;
} else if (readyEvents & events) {
break;
} else if (infoPtr->flags & SOCKET_ASYNC) {
*errorCodePtr = EWOULDBLOCK;
result = 0;
break;
}
WaitForSingleObject(tsdPtr->wakeEvent, 100);
// Tcl_ServiceAll();
}
} else {
/*
* Reset WSAAsyncSelect so we have a fresh set of events pending.
*/
(void) (*winSock.WSAAsyncSelect)(infoPtr->socket, tsdPtr->hwnd, 0, 0);
(void) (*winSock.WSAAsyncSelect)(infoPtr->socket, tsdPtr->hwnd,
SOCKET_MESSAGE, infoPtr->selectEvents);
while (1) {
/*
* Process all outstanding messages on the socket window.
*/
while (PeekMessage(&msg, tsdPtr->hwnd, 0, 0, PM_REMOVE)) {
DispatchMessage(&msg);
}
if (infoPtr->lastError) {
*errorCodePtr = infoPtr->lastError;
result = 0;
break;
} else if (infoPtr->readyEvents & events) {
break;
} else if (infoPtr->flags & SOCKET_ASYNC) {
*errorCodePtr = EWOULDBLOCK;
result = 0;
break;
}
/*
* Wait until something happens.
*/
WaitMessage();
}
}
(void) Tcl_SetServiceMode(oldMode);
return result;
}
/*
*----------------------------------------------------------------------
*
* Tcl_OpenTcpClient --
|
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<
>
|
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
|
static int
WaitForSocketEvent(infoPtr, events, errorCodePtr)
SocketInfo *infoPtr; /* Information about this socket. */
int events; /* Events to look for. */
int *errorCodePtr; /* Where to store errors? */
{
MSG msg;
int result = 1;
int oldMode;
ThreadSpecificData *tsdPtr =
(ThreadSpecificData *)TclThreadDataKeyGet(&dataKey);
/*
* Be sure to disable event servicing so we are truly modal.
*/
oldMode = Tcl_SetServiceMode(TCL_SERVICE_NONE);
/*
* Reset WSAAsyncSelect so we have a fresh set of events pending.
*/
(void) (*winSock.WSAAsyncSelect)(infoPtr->socket, tsdPtr->hwnd, 0, 0);
(void) (*winSock.WSAAsyncSelect)(infoPtr->socket, tsdPtr->hwnd,
SOCKET_MESSAGE, infoPtr->selectEvents);
while (1) {
/*
* Process all outstanding messages on the socket window.
*/
while (PeekMessage(&msg, tsdPtr->hwnd, 0, 0, PM_REMOVE)) {
DispatchMessage(&msg);
}
if (infoPtr->lastError) {
*errorCodePtr = infoPtr->lastError;
result = 0;
break;
} else if (infoPtr->readyEvents & events) {
break;
} else if (infoPtr->flags & SOCKET_ASYNC) {
*errorCodePtr = EWOULDBLOCK;
result = 0;
break;
}
/*
* Wait until something happens.
*/
WaitMessage();
}
(void) Tcl_SetServiceMode(oldMode);
return result;
}
/*
*----------------------------------------------------------------------
*
* Tcl_OpenTcpClient --
|
| ︙ | | | ︙ | |
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
|
* Set kernel space buffering and non-blocking.
*/
TclSockMinimumBuffers((SOCKET) sock, TCP_BUFFER_SIZE);
infoPtr = NewSocketInfo((SOCKET) sock);
if (infoPtr == NULL) {
return NULL;
}
/*
* Start watching for read/write events on the socket.
*/
infoPtr->selectEvents = FD_READ | FD_CLOSE | FD_WRITE;
if (useThreads) {
(void) (*winSock.WSAEventSelect)(infoPtr->socket, socketEvent,
infoPtr->selectEvents);
} else {
(void) (*winSock.WSAAsyncSelect)(infoPtr->socket, tsdPtr->hwnd,
SOCKET_MESSAGE, infoPtr->selectEvents);
}
wsprintfA(channelName, "sock%d", infoPtr->socket);
infoPtr->channel = Tcl_CreateChannel(&tcpChannelType, channelName,
(ClientData) infoPtr, (TCL_READABLE | TCL_WRITABLE));
Tcl_SetChannelOption(NULL, infoPtr->channel, "-translation", "auto crlf");
return infoPtr->channel;
}
|
<
<
<
<
<
<
<
<
<
|
|
|
<
|
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
|
* Set kernel space buffering and non-blocking.
*/
TclSockMinimumBuffers((SOCKET) sock, TCP_BUFFER_SIZE);
infoPtr = NewSocketInfo((SOCKET) sock);
/*
* Start watching for read/write events on the socket.
*/
infoPtr->selectEvents = FD_READ | FD_CLOSE | FD_WRITE;
(void) (*winSock.WSAAsyncSelect)(infoPtr->socket, tsdPtr->hwnd,
SOCKET_MESSAGE, infoPtr->selectEvents);
wsprintfA(channelName, "sock%d", infoPtr->socket);
infoPtr->channel = Tcl_CreateChannel(&tcpChannelType, channelName,
(ClientData) infoPtr, (TCL_READABLE | TCL_WRITABLE));
Tcl_SetChannelOption(NULL, infoPtr->channel, "-translation", "auto crlf");
return infoPtr->channel;
}
|
| ︙ | | | ︙ | |
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
|
/*
* Clear the ready mask so we can detect the next connection request.
* Note that connection requests are level triggered, so if there is
* a request already pending, a new event will be generated.
*/
infoPtr->readyEvents &= ~(FD_ACCEPT);
if (newSocket == INVALID_SOCKET) {
return;
}
/*
* Win-NT has a misfeature that sockets are inherited in child
* processes by default. Turn off the inherit bit.
|
|
|
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
|
/*
* Clear the ready mask so we can detect the next connection request.
* Note that connection requests are level triggered, so if there is
* a request already pending, a new event will be generated.
*/
infoPtr->readyEvents &= ~(FD_ACCEPT);
if (newSocket == INVALID_SOCKET) {
return;
}
/*
* Win-NT has a misfeature that sockets are inherited in child
* processes by default. Turn off the inherit bit.
|
| ︙ | | | ︙ | |
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
|
newInfoPtr = NewSocketInfo(newSocket);
/*
* Select on read/write events and create the channel.
*/
newInfoPtr->selectEvents = (FD_READ | FD_WRITE | FD_CLOSE);
if (useThreads) {
(void) (*winSock.WSAEventSelect)(newInfoPtr->socket, socketEvent,
newInfoPtr->selectEvents);
} else {
(void) (*winSock.WSAAsyncSelect)(newInfoPtr->socket, tsdPtr->hwnd,
SOCKET_MESSAGE, newInfoPtr->selectEvents);
}
wsprintfA(channelName, "sock%d", newInfoPtr->socket);
newInfoPtr->channel = Tcl_CreateChannel(&tcpChannelType, channelName,
(ClientData) newInfoPtr, (TCL_READABLE | TCL_WRITABLE));
if (Tcl_SetChannelOption(NULL, newInfoPtr->channel, "-translation",
"auto crlf") == TCL_ERROR) {
Tcl_Close((Tcl_Interp *) NULL, newInfoPtr->channel);
return;
|
<
<
<
<
|
|
|
<
|
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
|
newInfoPtr = NewSocketInfo(newSocket);
/*
* Select on read/write events and create the channel.
*/
newInfoPtr->selectEvents = (FD_READ | FD_WRITE | FD_CLOSE);
(void) (*winSock.WSAAsyncSelect)(newInfoPtr->socket, tsdPtr->hwnd,
SOCKET_MESSAGE, newInfoPtr->selectEvents);
wsprintfA(channelName, "sock%d", newInfoPtr->socket);
newInfoPtr->channel = Tcl_CreateChannel(&tcpChannelType, channelName,
(ClientData) newInfoPtr, (TCL_READABLE | TCL_WRITABLE));
if (Tcl_SetChannelOption(NULL, newInfoPtr->channel, "-translation",
"auto crlf") == TCL_ERROR) {
Tcl_Close((Tcl_Interp *) NULL, newInfoPtr->channel);
return;
|
| ︙ | | | ︙ | |
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
|
char *buf; /* Where to store data. */
int toRead; /* Maximum number of bytes to read. */
int *errorCodePtr; /* Where to store error codes. */
{
SocketInfo *infoPtr = (SocketInfo *) instanceData;
int bytesRead;
int error;
int readyEvents;
ThreadSpecificData *tsdPtr =
(ThreadSpecificData *)TclThreadDataKeyGet(&dataKey);
*errorCodePtr = 0;
/*
* Check that WinSock is initialized; do not call it if not, to
|
<
|
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
|
char *buf; /* Where to store data. */
int toRead; /* Maximum number of bytes to read. */
int *errorCodePtr; /* Where to store error codes. */
{
SocketInfo *infoPtr = (SocketInfo *) instanceData;
int bytesRead;
int error;
ThreadSpecificData *tsdPtr =
(ThreadSpecificData *)TclThreadDataKeyGet(&dataKey);
*errorCodePtr = 0;
/*
* Check that WinSock is initialized; do not call it if not, to
|
| ︙ | | | ︙ | |
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
|
if (infoPtr->flags & SOCKET_EOF) {
return 0;
}
/*
* Check to see if the socket is connected before trying to read.
*/
infoPtr->selectEvents |= FD_READ | FD_CLOSE;
if (useThreads) {
(void) (*winSock.WSAEventSelect)(infoPtr->socket, socketEvent,
infoPtr->selectEvents);
}
if ((infoPtr->flags & SOCKET_ASYNC_CONNECT)
&& ! WaitForSocketEvent(infoPtr, FD_CONNECT, 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 read. We have to simulate blocking behavior here
* since we are always using non-blocking sockets.
*/
while (1) {
if (!useThreads) {
(void) (*winSock.WSAAsyncSelect)(infoPtr->socket, tsdPtr->hwnd,
0, 0);
}
bytesRead = (*winSock.recv)(infoPtr->socket, buf, toRead, 0);
infoPtr->readyEvents &= ~(FD_READ);
/*
* Check for end-of-file condition or successful read.
*/
if (bytesRead == 0) {
infoPtr->flags |= SOCKET_EOF;
}
if (bytesRead != SOCKET_ERROR) {
break;
}
/*
* If an error occurs after the FD_CLOSE has arrived,
* then ignore the error and report an EOF.
*/
readyEvents = GetSocketState(infoPtr);
if (readyEvents & FD_CLOSE) {
infoPtr->flags |= SOCKET_EOF;
bytesRead = 0;
break;
}
/*
* Check for error condition or underflow in non-blocking case.
*/
error = (*winSock.WSAGetLastError)();
if ((error != 0) && ((infoPtr->flags & SOCKET_ASYNC)
|| (error != WSAEWOULDBLOCK))) {
TclWinConvertWSAError(error);
*errorCodePtr = Tcl_GetErrno();
bytesRead = -1;
break;
}
/*
* In the blocking case, wait until the file becomes readable
* or closed and try again.
*/
if (!WaitForSocketEvent(infoPtr, FD_READ|FD_CLOSE, errorCodePtr)) {
bytesRead = -1;
break;
}
}
if (!useThreads) {
(void) (*winSock.WSAAsyncSelect)(infoPtr->socket, tsdPtr->hwnd,
SOCKET_MESSAGE, infoPtr->selectEvents);
}
return bytesRead;
}
/*
*----------------------------------------------------------------------
*
* TcpOutputProc --
|
<
<
<
<
<
<
<
<
|
|
<
<
|
<
|
|
<
|
|
<
|
|
<
|
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
|
if (infoPtr->flags & SOCKET_EOF) {
return 0;
}
/*
* Check to see if the socket is connected before trying to read.
*/
if ((infoPtr->flags & SOCKET_ASYNC_CONNECT)
&& ! WaitForSocketEvent(infoPtr, FD_CONNECT, 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 read. We have to simulate blocking behavior here
* since we are always using non-blocking sockets.
*/
while (1) {
(void) (*winSock.WSAAsyncSelect)(infoPtr->socket, tsdPtr->hwnd,
0, 0);
bytesRead = (*winSock.recv)(infoPtr->socket, buf, toRead, 0);
infoPtr->readyEvents &= ~(FD_READ);
/*
* Check for end-of-file condition or successful read.
*/
if (bytesRead == 0) {
infoPtr->flags |= SOCKET_EOF;
}
if (bytesRead != SOCKET_ERROR) {
break;
}
/*
* If an error occurs after the FD_CLOSE has arrived,
* then ignore the error and report an EOF.
*/
if (infoPtr->readyEvents & FD_CLOSE) {
infoPtr->flags |= SOCKET_EOF;
bytesRead = 0;
break;
}
/*
* Check for error condition or underflow in non-blocking case.
*/
error = (*winSock.WSAGetLastError)();
if ((infoPtr->flags & SOCKET_ASYNC) || (error != WSAEWOULDBLOCK)) {
TclWinConvertWSAError(error);
*errorCodePtr = Tcl_GetErrno();
bytesRead = -1;
break;
}
/*
* In the blocking case, wait until the file becomes readable
* or closed and try again.
*/
if (!WaitForSocketEvent(infoPtr, FD_READ|FD_CLOSE, errorCodePtr)) {
bytesRead = -1;
break;
}
}
(void) (*winSock.WSAAsyncSelect)(infoPtr->socket, tsdPtr->hwnd,
SOCKET_MESSAGE, infoPtr->selectEvents);
return bytesRead;
}
/*
*----------------------------------------------------------------------
*
* TcpOutputProc --
|
| ︙ | | | ︙ | |
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
|
if ((infoPtr->flags & SOCKET_ASYNC_CONNECT)
&& ! WaitForSocketEvent(infoPtr, FD_CONNECT, errorCodePtr)) {
return -1;
}
while (1) {
if (!useThreads) {
(void) (*winSock.WSAAsyncSelect)(infoPtr->socket, tsdPtr->hwnd,
0, 0);
}
bytesWritten = (*winSock.send)(infoPtr->socket, buf, toWrite, 0);
if (bytesWritten != SOCKET_ERROR) {
if (!useThreads && (infoPtr->watchEvents & FD_WRITE)) {
/*
* Since Windows won't generate a new write event until we hit
* an overflow condition, we need to force the event loop to
* poll until the condition changes.
*/
Tcl_Time blockTime = { 0, 0 };
Tcl_SetMaxBlockTime(&blockTime);
}
break;
}
/*
* Check for error condition or overflow. In the event of overflow, we
* need to clear the FD_WRITE flag so we can detect the next writable
* event. Note that Windows only sends a new writable event after a
* send fails with WSAEWOULDBLOCK.
*/
error = (*winSock.WSAGetLastError)();
if (error == WSAEWOULDBLOCK) {
infoPtr->readyEvents &= ~(FD_WRITE);
if (infoPtr->flags & SOCKET_ASYNC) {
*errorCodePtr = EWOULDBLOCK;
bytesWritten = -1;
break;
}
|
<
|
|
<
<
<
|
|
|
|
|
>
|
<
|
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
|
if ((infoPtr->flags & SOCKET_ASYNC_CONNECT)
&& ! WaitForSocketEvent(infoPtr, FD_CONNECT, errorCodePtr)) {
return -1;
}
while (1) {
(void) (*winSock.WSAAsyncSelect)(infoPtr->socket, tsdPtr->hwnd,
0, 0);
bytesWritten = (*winSock.send)(infoPtr->socket, buf, toWrite, 0);
if (bytesWritten != SOCKET_ERROR) {
/*
* Since Windows won't generate a new write event until we hit
* an overflow condition, we need to force the event loop to
* poll until the condition changes.
*/
if (infoPtr->watchEvents & FD_WRITE) {
Tcl_Time blockTime = { 0, 0 };
Tcl_SetMaxBlockTime(&blockTime);
}
break;
}
/*
* Check for error condition or overflow. In the event of overflow, we
* need to clear the FD_WRITE flag so we can detect the next writable
* event. Note that Windows only sends a new writable event after a
* send fails with WSAEWOULDBLOCK.
*/
error = (*winSock.WSAGetLastError)();
if (error == WSAEWOULDBLOCK) {
infoPtr->readyEvents &= ~(FD_WRITE);
if (infoPtr->flags & SOCKET_ASYNC) {
*errorCodePtr = EWOULDBLOCK;
bytesWritten = -1;
break;
}
|
| ︙ | | | ︙ | |
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
|
if (!WaitForSocketEvent(infoPtr, FD_WRITE|FD_CLOSE, errorCodePtr)) {
bytesWritten = -1;
break;
}
}
if (!useThreads) {
(void) (*winSock.WSAAsyncSelect)(infoPtr->socket, tsdPtr->hwnd,
SOCKET_MESSAGE, infoPtr->selectEvents);
}
return bytesWritten;
}
/*
*----------------------------------------------------------------------
*
* TcpGetOptionProc --
|
<
|
|
<
|
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
|
if (!WaitForSocketEvent(infoPtr, FD_WRITE|FD_CLOSE, errorCodePtr)) {
bytesWritten = -1;
break;
}
}
(void) (*winSock.WSAAsyncSelect)(infoPtr->socket, tsdPtr->hwnd,
SOCKET_MESSAGE, infoPtr->selectEvents);
return bytesWritten;
}
/*
*----------------------------------------------------------------------
*
* TcpGetOptionProc --
|
| ︙ | | | ︙ | |
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
|
static void
TcpWatchProc(instanceData, mask)
ClientData instanceData; /* The socket state. */
int mask; /* Events of interest; an OR-ed
* combination of TCL_READABLE,
* TCL_WRITABLE and TCL_EXCEPTION. */
{
int readyEvents;
SocketInfo *infoPtr = (SocketInfo *) instanceData;
/*
* Update the watch events mask.
*/
EnterCriticalSection(&socketCS);
infoPtr->watchEvents = 0;
if (mask & TCL_READABLE) {
infoPtr->watchEvents |= (FD_READ|FD_CLOSE|FD_ACCEPT);
}
if (mask & TCL_WRITABLE) {
infoPtr->watchEvents |= (FD_WRITE|FD_CONNECT);
}
LeaveCriticalSection(&socketCS);
/*
* If there are any conditions already set, then tell the notifier to poll
* rather than block.
*/
readyEvents = GetSocketState(infoPtr);
if (readyEvents & infoPtr->watchEvents) {
Tcl_Time blockTime = { 0, 0 };
Tcl_SetMaxBlockTime(&blockTime);
}
}
/*
*----------------------------------------------------------------------
|
<
|
<
<
<
|
|
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
|
static void
TcpWatchProc(instanceData, mask)
ClientData instanceData; /* The socket state. */
int mask; /* Events of interest; an OR-ed
* combination of TCL_READABLE,
* TCL_WRITABLE and TCL_EXCEPTION. */
{
SocketInfo *infoPtr = (SocketInfo *) instanceData;
/*
* Update the watch events mask.
*/
infoPtr->watchEvents = 0;
if (mask & TCL_READABLE) {
infoPtr->watchEvents |= (FD_READ|FD_CLOSE|FD_ACCEPT);
}
if (mask & TCL_WRITABLE) {
infoPtr->watchEvents |= (FD_WRITE|FD_CONNECT);
}
/*
* If there are any conditions already set, then tell the notifier to poll
* rather than block.
*/
if (infoPtr->readyEvents & infoPtr->watchEvents) {
Tcl_Time blockTime = { 0, 0 };
Tcl_SetMaxBlockTime(&blockTime);
}
}
/*
*----------------------------------------------------------------------
|
| ︙ | | | ︙ | |
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
|
ClientData *handlePtr; /* Where to store the handle. */
{
SocketInfo *statePtr = (SocketInfo *) instanceData;
*handlePtr = (ClientData) statePtr->socket;
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* SocketThread --
*
* Use a separate thread to check state for the sockets in each
* thread. Using a window isn't the best way, especially if the
* WinSock2 API is available and working. Also, creating N threads
* per socket like the pipe, console, and serial drivers isn't
* very efficient. Currently, all sockets are tied to one event.
* The overhead involved in creating an event for each socket is
* a concern. But, in order to avoid deadlocks, the call to
* WSAWaitForMultipleEvents() needs to timeout to check for
* status changes which haven't been handled (race condition).
*
* Results:
* 0 on success.
*
* Side effects:
*
*----------------------------------------------------------------------
*/
static DWORD WINAPI
SocketThread(LPVOID arg)
{
SocketInfo *infoPtr;
WSAEVENT events[2];
ThreadSpecificData *tsdPtr;
DWORD result;
/*
* Find the specified socket on the socket list and update its
* eventState flag.
*/
events[0] = killEvent;
events[1] = socketEvent;
while (1) {
result = (*winSock.WSAWaitForMultipleEvents)(2, events, FALSE,
TCL_SOCKET_EVENT_TIMEOUT, FALSE);
switch (result) {
case WSA_WAIT_TIMEOUT:
case (WSA_WAIT_EVENT_0 +1):
/*
* A socket event has fired, determine which socket(s) it was
* and which thread(s) it came from.
*/
EnterCriticalSection(&socketCS);
for (tsdPtr = firstWaitingThread; tsdPtr != NULL;
tsdPtr = tsdPtr->next) {
for (infoPtr = tsdPtr->socketList; infoPtr != NULL;
infoPtr = infoPtr->nextPtr) {
if (GetSocketState(infoPtr) & infoPtr->watchEvents) {
Tcl_ThreadAlert(tsdPtr->threadId);
SetEvent(tsdPtr->wakeEvent);
}
}
}
LeaveCriticalSection(&socketCS);
(*winSock.WSAResetEvent)(socketEvent);
break;
case WSA_WAIT_EVENT_0:
/*
* This thread is supposed to kill itself off
*/
return 0;
break;
default:
/*
* The thread has detected a fatal error
*/
return 1;
break;
}
}
return 0;
}
/*
*----------------------------------------------------------------------
*
* SocketProc --
*
* This function is called when WSAAsyncSelect has been used
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
|
ClientData *handlePtr; /* Where to store the handle. */
{
SocketInfo *statePtr = (SocketInfo *) instanceData;
*handlePtr = (ClientData) statePtr->socket;
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* SocketProc --
*
* This function is called when WSAAsyncSelect has been used
|
| ︙ | | | ︙ | |