1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
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
49
50
51
52
53
54
|
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
/*
* 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.36.2.10 2009/04/27 22:10:28 ferrieux Exp $
* RCS: @(#) $Id: tclWinSock.c,v 1.36.2.11 2010/09/24 17:53:14 andreas_kupries Exp $
*
* -----------------------------------------------------------------------
*
* General information on how this module works.
*
* - Each Tcl-thread with its sockets maintains an internal window to receive
* socket messages from the OS.
*
* - To ensure that message reception is always running this window is
* actually owned and handled by an internal thread. This we call the
* co-thread of Tcl's thread.
*
* - The whole structure is set up by InitSockets() which is called for each
* Tcl thread. The implementation of the co-thread is in SocketThread(),
* and the messages are handled by SocketProc(). The connection between
* both is not directly visible, it is done through a Win32 window class.
* This class is initialized by InitSockets() as well, and used in the
* creation of the message receiver windows.
*
* - An important thing to note is that *both* thread and co-thread have
* access to the list of sockets maintained in the private TSD data of the
* thread. The co-thread was given access to it upon creation through the
* new thread's client-data.
*
* Because of this dual access the TSD data contains an OS mutex, the
* "socketListLock", to mediate exclusion between thread and co-thread.
*
* The co-thread's access is all in SocketProc(). The thread's access is
* through SocketEventProc() (1) and the functions called by it.
*
* (Ad 1) This is the handler function for all queued socket events, which
* all the OS messages are translated to through the EventSource (2)
* driven by the OS messages.
*
* (Ad 2) The main functions for this are SocketSetupProc() and
* SocketCheckProc().
*/
#include "tclWinInt.h"
/*
* Make sure to remove the redirection defines set in tclWinPort.h
* that is in use in other sections of the core, except for us.
|
1596
1597
1598
1599
1600
1601
1602
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
|
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
|
+
+
+
+
+
+
+
+
+
+
|
*/
len = sizeof(SOCKADDR_IN);
newSocket = winSock.accept(infoPtr->socket, (SOCKADDR *)&addr,
&len);
/*
* Protect access to sockets (acceptEventCount, readyEvents) in socketList
* by the lock. Fix for SF Tcl Bug 3056775.
*/
WaitForSingleObject(tsdPtr->socketListLock, INFINITE);
/*
* 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.
*/
if (newSocket == INVALID_SOCKET) {
infoPtr->acceptEventCount = 0;
infoPtr->readyEvents &= ~(FD_ACCEPT);
SetEvent(tsdPtr->socketListLock);
return;
}
/*
* It is possible that more than one FD_ACCEPT has been sent, so an extra
* count must be kept. Decrement the count, and reset the readyEvent bit
* if the count is no longer > 0.
*/
infoPtr->acceptEventCount--;
if (infoPtr->acceptEventCount <= 0) {
infoPtr->readyEvents &= ~(FD_ACCEPT);
}
SetEvent(tsdPtr->socketListLock);
/*
* Win-NT has a misfeature that sockets are inherited in child
* processes by default. Turn off the inherit bit.
*/
SetHandleInformation( (HANDLE) newSocket, HANDLE_FLAG_INHERIT, 0 );
|