📄 tclwinsock.c
字号:
}/* *---------------------------------------------------------------------- * * SocketExitHandler -- * * Callback invoked during exit clean up to delete the socket * communication window and to release the WinSock DLL. * * Results: * None. * * Side effects: * None. * *---------------------------------------------------------------------- */ /* ARGSUSED */static voidSocketExitHandler(clientData) ClientData clientData; /* Not used. */{ if (winSock.hInstance) { DestroyWindow(winSock.hwnd); UnregisterClass("TclSocket", TclWinGetTclInstance()); (*winSock.WSACleanup)(); FreeLibrary(winSock.hInstance); winSock.hInstance = NULL; } Tcl_DeleteEventSource(SocketSetupProc, SocketCheckProc, NULL); initialized = 0; hostnameInitialized = 0;}/* *---------------------------------------------------------------------- * * TclHasSockets -- * * This function determines whether sockets are available on the * current system and returns an error in interp if they are not. * Note that interp may be NULL. * * Results: * Returns TCL_OK if the system supports sockets, or TCL_ERROR with * an error in interp. * * Side effects: * None. * *---------------------------------------------------------------------- */intTclHasSockets(interp) Tcl_Interp *interp;{ if (!initialized) { InitSockets(); } if (winSock.hInstance != NULL) { return TCL_OK; } if (interp != NULL) { Tcl_AppendResult(interp, "sockets are not available on this system", NULL); } return TCL_ERROR;}/* *---------------------------------------------------------------------- * * SocketSetupProc -- * * This procedure is invoked before Tcl_DoOneEvent blocks waiting * for an event. * * Results: * None. * * Side effects: * Adjusts the block time if needed. * *---------------------------------------------------------------------- */voidSocketSetupProc(data, flags) ClientData data; /* Not used. */ int flags; /* Event flags as passed to Tcl_DoOneEvent. */{ SocketInfo *infoPtr; Tcl_Time blockTime = { 0, 0 }; if (!(flags & TCL_FILE_EVENTS)) { return; } /* * Check to see if there is a ready socket. If so, poll. */ for (infoPtr = socketList; infoPtr != NULL; infoPtr = infoPtr->nextPtr) { if (infoPtr->readyEvents & infoPtr->watchEvents) { Tcl_SetMaxBlockTime(&blockTime); break; } }}/* *---------------------------------------------------------------------- * * SocketCheckProc -- * * This procedure is called by Tcl_DoOneEvent to check the socket * event source for events. * * Results: * None. * * Side effects: * May queue an event. * *---------------------------------------------------------------------- */static voidSocketCheckProc(data, flags) ClientData data; /* Not used. */ int flags; /* Event flags as passed to Tcl_DoOneEvent. */{ SocketInfo *infoPtr; SocketEvent *evPtr; 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 = 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 -- * * This procedure is called by Tcl_ServiceEvent when a socket event * reaches the front of the event queue. This procedure is * responsible for notifying the generic channel code. * * Results: * Returns 1 if the event was handled, meaning it should be removed * from the queue. Returns 0 if the event was not handled, meaning * it should stay on the queue. The only time the event isn't * handled is if the TCL_FILE_EVENTS flag bit isn't set. * * Side effects: * Whatever the channel callback procedures do. * *---------------------------------------------------------------------- */static intSocketEventProc(evPtr, flags) Tcl_Event *evPtr; /* Event to service. */ 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; if (!(flags & TCL_FILE_EVENTS)) { return 0; } /* * Find the specified socket on the socket list. */ for (infoPtr = 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 * have to watch out for the channel being deleted out from under * us. This may cause a redundant trip through the event loop, but * it's simpler than trying to do unwind protection. */ Tcl_Time blockTime = { 0, 0 }; Tcl_SetMaxBlockTime(&blockTime); mask |= TCL_READABLE; } else if (events & FD_READ) { fd_set readFds; struct timeval timeout; /* * 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, winSock.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, winSock.hwnd, SOCKET_MESSAGE, infoPtr->selectEvents); infoPtr->readyEvents &= ~(FD_READ); } } if (events & FD_WRITE) { mask |= TCL_WRITABLE; } if (mask) { Tcl_NotifyChannel(infoPtr->channel, mask); } return 1;}/* *---------------------------------------------------------------------- * * TcpBlockProc -- * * Sets a socket into blocking or non-blocking mode. * * Results: * 0 if successful, errno if there was an error. * * Side effects: * None. * *---------------------------------------------------------------------- */static intTcpBlockProc(instanceData, mode) ClientData instanceData; /* The socket to block/un-block. */ int mode; /* TCL_MODE_BLOCKING or * TCL_MODE_NONBLOCKING. */{ SocketInfo *infoPtr = (SocketInfo *) instanceData; if (mode == TCL_MODE_NONBLOCKING) { infoPtr->flags |= SOCKET_ASYNC; } else { infoPtr->flags &= ~(SOCKET_ASYNC); } return 0;}/* *---------------------------------------------------------------------- * * TcpCloseProc -- * * This procedure is called by the generic IO level to perform * channel type specific cleanup on a socket based channel * when the channel is closed. * * Results: * 0 if successful, the value of errno if failed. * * Side effects: * Closes the socket. * *---------------------------------------------------------------------- */ /* ARGSUSED */static intTcpCloseProc(instanceData, interp) ClientData instanceData; /* The socket to close. */ Tcl_Interp *interp; /* Unused. */{ SocketInfo *infoPtr = (SocketInfo *) instanceData; SocketInfo **nextPtrPtr; int errorCode = 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 (winSock.hInstance != NULL) { /* * Clean up the OS socket handle. The default Windows setting * for a socket is SO_DONTLINGER, which does a graceful shutdown * in the background. */ if ((*winSock.closesocket)(infoPtr->socket) == SOCKET_ERROR) { TclWinConvertWSAError((*winSock.WSAGetLastError)()); errorCode = Tcl_GetErrno(); } } /* * Remove the socket from socketList. */ for (nextPtrPtr = &socketList; (*nextPtrPtr) != NULL; nextPtrPtr = &((*nextPtrPtr)->nextPtr)) { if ((*nextPtrPtr) == infoPtr) { (*nextPtrPtr) = infoPtr->nextPtr; break; } } ckfree((char *) infoPtr); return errorCode;}/* *---------------------------------------------------------------------- * * NewSocketInfo -- * * This function allocates and initializes a new SocketInfo * structure. * * Results: * Returns a newly allocated SocketInfo. * * Side effects: * Adds the socket to the global socket list. * *---------------------------------------------------------------------- */static SocketInfo *NewSocketInfo(socket) SOCKET socket;{ SocketInfo *infoPtr; infoPtr = (SocketInfo *) ckalloc((unsigned) sizeof(SocketInfo)); infoPtr->socket = socket; infoPtr->flags = 0; infoPtr->watchEvents = 0; infoPtr->readyEvents = 0; infoPtr->selectEvents = 0; infoPtr->acceptProc = NULL; infoPtr->lastError = 0; infoPtr->nextPtr = socketList; socketList = infoPtr; return infoPtr;}/* *---------------------------------------------------------------------- * * CreateSocket -- * * This function opens a new socket and initializes the * SocketInfo structure.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -