| ︙ | | | ︙ | |
1719
1720
1721
1722
1723
1724
1725
1726
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
1766
1767
1768
1769
1770
1771
1772
1773
|
*
* Side effects:
* Consumes input from the socket.
*
*----------------------------------------------------------------------
*/
static int
TcpInputProc(
ClientData instanceData, /* The socket state. */
char *buf, /* Where to store data. */
int toRead, /* Maximum number of bytes to read. */
int *errorCodePtr) /* Where to store error codes. */
{
SocketInfo *infoPtr = instanceData;
int bytesRead;
DWORD error;
ThreadSpecificData *tsdPtr = TclThreadDataKeyGet(&dataKey);
*errorCodePtr = 0;
/*
* Check that WinSock is initialized; do not call it if not, to prevent
* system crashes. This can happen at exit time if the exit handler for
* WinSock ran before other exit handlers that want to use sockets.
*/
if (!SocketsEnabled()) {
*errorCodePtr = EFAULT;
return -1;
}
/*
* First check to see if EOF was already detected, to prevent calling the
* socket stack after the first time EOF is detected.
*/
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
|
|
|
|
|
|
|
1719
1720
1721
1722
1723
1724
1725
1726
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
1766
1767
1768
1769
1770
1771
1772
1773
|
*
* Side effects:
* Consumes input from the socket.
*
*----------------------------------------------------------------------
*/
static size_t
TcpInputProc(
ClientData instanceData, /* The socket state. */
char *buf, /* Where to store data. */
size_t toRead, /* Maximum number of bytes to read. */
int *errorCodePtr) /* Where to store error codes. */
{
SocketInfo *infoPtr = instanceData;
size_t bytesRead;
DWORD error;
ThreadSpecificData *tsdPtr = TclThreadDataKeyGet(&dataKey);
*errorCodePtr = 0;
/*
* Check that WinSock is initialized; do not call it if not, to prevent
* system crashes. This can happen at exit time if the exit handler for
* WinSock ran before other exit handlers that want to use sockets.
*/
if (!SocketsEnabled()) {
*errorCodePtr = EFAULT;
return (size_t)-1;
}
/*
* First check to see if EOF was already detected, to prevent calling the
* socket stack after the first time EOF is detected.
*/
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 (size_t)-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
|
| ︙ | | | ︙ | |
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
|
/*
* Check for error condition or underflow in non-blocking case.
*/
if ((infoPtr->flags & SOCKET_ASYNC) || (error != WSAEWOULDBLOCK)) {
TclWinConvertError(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;
}
}
SendMessage(tsdPtr->hwnd, SOCKET_SELECT, (WPARAM)SELECT, (LPARAM)infoPtr);
return bytesRead;
|
|
|
|
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
|
/*
* Check for error condition or underflow in non-blocking case.
*/
if ((infoPtr->flags & SOCKET_ASYNC) || (error != WSAEWOULDBLOCK)) {
TclWinConvertError(error);
*errorCodePtr = Tcl_GetErrno();
bytesRead = (size_t)-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 = (size_t)-1;
break;
}
}
SendMessage(tsdPtr->hwnd, SOCKET_SELECT, (WPARAM)SELECT, (LPARAM)infoPtr);
return bytesRead;
|
| ︙ | | | ︙ | |
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
|
*
* Side effects:
* Produces output on the socket.
*
*----------------------------------------------------------------------
*/
static int
TcpOutputProc(
ClientData instanceData, /* The socket state. */
const char *buf, /* Where to get data. */
int toWrite, /* Maximum number of bytes to write. */
int *errorCodePtr) /* Where to store error codes. */
{
SocketInfo *infoPtr = instanceData;
int bytesWritten;
DWORD error;
ThreadSpecificData *tsdPtr = TclThreadDataKeyGet(&dataKey);
*errorCodePtr = 0;
/*
* Check that WinSock is initialized; do not call it if not, to prevent
* system crashes. This can happen at exit time if the exit handler for
* WinSock ran before other exit handlers that want to use sockets.
*/
if (!SocketsEnabled()) {
*errorCodePtr = EFAULT;
return -1;
}
/*
* Check to see if the socket is connected before trying to write.
*/
if ((infoPtr->flags & SOCKET_ASYNC_CONNECT)
&& !WaitForSocketEvent(infoPtr, FD_CONNECT, errorCodePtr)) {
return -1;
}
while (1) {
SendMessage(tsdPtr->hwnd, SOCKET_SELECT,
(WPARAM) UNSELECT, (LPARAM) infoPtr);
/* single fd operation: this proc is only called for a connected socket. */
|
|
|
|
|
|
|
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
|
*
* Side effects:
* Produces output on the socket.
*
*----------------------------------------------------------------------
*/
static size_t
TcpOutputProc(
ClientData instanceData, /* The socket state. */
const char *buf, /* Where to get data. */
size_t toWrite, /* Maximum number of bytes to write. */
int *errorCodePtr) /* Where to store error codes. */
{
SocketInfo *infoPtr = instanceData;
size_t bytesWritten;
DWORD error;
ThreadSpecificData *tsdPtr = TclThreadDataKeyGet(&dataKey);
*errorCodePtr = 0;
/*
* Check that WinSock is initialized; do not call it if not, to prevent
* system crashes. This can happen at exit time if the exit handler for
* WinSock ran before other exit handlers that want to use sockets.
*/
if (!SocketsEnabled()) {
*errorCodePtr = EFAULT;
return (size_t)-1;
}
/*
* Check to see if the socket is connected before trying to write.
*/
if ((infoPtr->flags & SOCKET_ASYNC_CONNECT)
&& !WaitForSocketEvent(infoPtr, FD_CONNECT, errorCodePtr)) {
return (size_t)-1;
}
while (1) {
SendMessage(tsdPtr->hwnd, SOCKET_SELECT,
(WPARAM) UNSELECT, (LPARAM) infoPtr);
/* single fd operation: this proc is only called for a connected socket. */
|
| ︙ | | | ︙ | |
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
|
*/
error = WSAGetLastError();
if (error == WSAEWOULDBLOCK) {
infoPtr->readyEvents &= ~(FD_WRITE);
if (infoPtr->flags & SOCKET_ASYNC) {
*errorCodePtr = EAGAIN;
bytesWritten = -1;
break;
}
} else {
TclWinConvertError(error);
*errorCodePtr = Tcl_GetErrno();
bytesWritten = -1;
break;
}
/*
* In the blocking case, wait until the file becomes writable or
* closed and try again.
*/
if (!WaitForSocketEvent(infoPtr, FD_WRITE|FD_CLOSE, errorCodePtr)) {
bytesWritten = -1;
break;
}
}
SendMessage(tsdPtr->hwnd, SOCKET_SELECT, (WPARAM)SELECT, (LPARAM)infoPtr);
return bytesWritten;
|
|
|
|
|
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
|
*/
error = WSAGetLastError();
if (error == WSAEWOULDBLOCK) {
infoPtr->readyEvents &= ~(FD_WRITE);
if (infoPtr->flags & SOCKET_ASYNC) {
*errorCodePtr = EAGAIN;
bytesWritten = (size_t)-1;
break;
}
} else {
TclWinConvertError(error);
*errorCodePtr = Tcl_GetErrno();
bytesWritten = (size_t)-1;
break;
}
/*
* In the blocking case, wait until the file becomes writable or
* closed and try again.
*/
if (!WaitForSocketEvent(infoPtr, FD_WRITE|FD_CLOSE, errorCodePtr)) {
bytesWritten = (size_t)-1;
break;
}
}
SendMessage(tsdPtr->hwnd, SOCKET_SELECT, (WPARAM)SELECT, (LPARAM)infoPtr);
return bytesWritten;
|
| ︙ | | | ︙ | |