예제 #1
0
// @pymethod |PyCWinThread|CreateThread|Creates the actual thread behind the thread object.
static PyObject *
ui_thread_create_thread(PyObject *self, PyObject *args)
{
	DWORD createFlags = 0;
	UINT stackSize = 0;
	if (!PyArg_ParseTuple(args, "|li:CreateThread", &createFlags, &stackSize))
		return NULL;
	CWinThread *pThread = GetCWinThreadPtr(self);
	if (!pThread) return NULL;
	PyEval_InitThreads();
	GUI_BGN_SAVE;
	BOOL ok = pThread->CreateThread(createFlags, stackSize);
	GUI_END_SAVE;
	if (!ok)
		RETURN_ERR("CreateThread failed");
	RETURN_NONE;
}
예제 #2
0
UINT AFX_CDECL WebSocketListeningFunc(LPVOID pThis)
{
	DbgSetThreadName("WebSocketListening");

	srand(time(NULL));
	InitThreadLocale();

	SOCKET hSocket = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, 0);
	if (INVALID_SOCKET != hSocket)
	{
		SOCKADDR_IN stAddr;
		stAddr.sin_family = AF_INET;
		stAddr.sin_port = htons(thePrefs.GetWSPort());
		if (thePrefs.GetBindAddrA())
			stAddr.sin_addr.S_un.S_addr = inet_addr(thePrefs.GetBindAddrA());
		else
			stAddr.sin_addr.S_un.S_addr = INADDR_ANY;

		if (!bind(hSocket, (sockaddr*)&stAddr, sizeof(stAddr)) && !listen(hSocket, SOMAXCONN))
		{
			HANDLE hEvent = CreateEvent(NULL, FALSE, TRUE, NULL);
			if (hEvent)
			{
				if (!WSAEventSelect(hSocket, hEvent, FD_ACCEPT))
				{
					HANDLE pWait[] = { hEvent, s_hTerminate };
					while (WAIT_OBJECT_0 == WaitForMultipleObjects(2, pWait, FALSE, INFINITE))
					{
						for (;;)
						{
							struct sockaddr_in their_addr;
                            int sin_size = sizeof(struct sockaddr_in);

							SOCKET hAccepted = accept(hSocket,(struct sockaddr *)&their_addr, &sin_size);
							if (INVALID_SOCKET == hAccepted)
								break;

							if (thePrefs.GetAllowedRemoteAccessIPs().GetCount() > 0)
							{
								bool bAllowedIP = false;
								for (int i = 0; i < thePrefs.GetAllowedRemoteAccessIPs().GetCount(); i++)
								{
									if (their_addr.sin_addr.S_un.S_addr == thePrefs.GetAllowedRemoteAccessIPs()[i])
									{
										bAllowedIP = true;
										break;
									}
								}
								if (!bAllowedIP) {
									LogWarning(_T("Web Interface: Rejected connection attempt from %s"), ipstr(their_addr.sin_addr.S_un.S_addr));
									VERIFY( !closesocket(hAccepted) );
									break;
								}
							}

							if(thePrefs.GetWSIsEnabled())
							{
								SocketData *pData = new SocketData;
								pData->hSocket = hAccepted;
								pData->pThis = pThis;
								pData->incomingaddr=their_addr.sin_addr;
								
								// - do NOT use Windows API 'CreateThread' to create a thread which uses MFC/CRT -> lot of mem leaks!
								// - 'AfxBeginThread' could be used here, but creates a little too much overhead for our needs.
								CWinThread* pAcceptThread = new CWinThread(WebSocketAcceptedFunc, (LPVOID)pData);
								if (!pAcceptThread->CreateThread())
								{
									delete pAcceptThread;
									pAcceptThread = NULL;
									VERIFY( !closesocket(hAccepted) );
									hAccepted = NULL;
								}
							}
							else
							{
								VERIFY( !closesocket(hAccepted) );
								hAccepted = NULL;
							}
						}
					}
				}
				VERIFY( CloseHandle(hEvent) );
				hEvent = NULL;
			}
		}
		VERIFY( !closesocket(hSocket) );
		hSocket = NULL;
	}

	return 0;
}
예제 #3
0
UINT AFX_CDECL WebSocketListeningFunc(LPVOID pThis)
{
    DbgSetThreadName("WebSocketListening");

    srand(time(NULL));
    InitThreadLocale();

//	WSADATA stData;
//	if (!WSAStartup(MAKEWORD(1, 1), &stData))
    {
        SOCKET hSocket = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, 0);
        if (INVALID_SOCKET != hSocket)
        {
            SOCKADDR_IN stAddr;
            stAddr.sin_family = AF_INET;
            stAddr.sin_port = htons(thePrefs.GetWSPort());
            stAddr.sin_addr.S_un.S_addr = INADDR_ANY;

            if (!bind(hSocket, (sockaddr*)&stAddr, sizeof(stAddr)) && !listen(hSocket, SOMAXCONN))
            {
                HANDLE hEvent = CreateEvent(NULL, FALSE, TRUE, NULL);
                if (hEvent)
                {
                    if (!WSAEventSelect(hSocket, hEvent, FD_ACCEPT))
                    {
                        HANDLE pWait[] = { hEvent, s_hTerminate };
                        while (WAIT_OBJECT_0 == WaitForMultipleObjects(2, pWait, FALSE, INFINITE))
                        {
                            for (;;)
                            {
                                struct sockaddr_in their_addr;
                                int sin_size = sizeof(struct sockaddr_in);

                                SOCKET hAccepted = accept(hSocket,(struct sockaddr *)&their_addr, &sin_size);
                                if (INVALID_SOCKET == hAccepted)
                                    break;

                                if(thePrefs.GetWSIsEnabled())
                                {
                                    SocketData *pData = new SocketData;
                                    pData->hSocket = hAccepted;
                                    pData->pThis = pThis;
                                    pData->incomingaddr=their_addr.sin_addr;

                                    // - do NOT use Windows API 'CreateThread' to create a thread which uses MFC/CRT -> lot of mem leaks!
                                    // - 'AfxBeginThread' could be used here, but creates a little too much overhead for our needs.
                                    CWinThread* pAcceptThread = new CWinThread(WebSocketAcceptedFunc, (LPVOID)pData);
                                    if (!pAcceptThread->CreateThread()) {
                                        delete pAcceptThread;
                                        pAcceptThread = NULL;
                                        VERIFY( !closesocket(hSocket) );
                                    }
                                }
                                else
                                    VERIFY( !closesocket(hSocket) );
                            }
                        }
                    }
                    VERIFY( CloseHandle(hEvent) );
                }
            }
            VERIFY( !closesocket(hSocket) );
        }
//		VERIFY( !WSACleanup() );
    }

    return 0;
}