void Session::Run() { Command cmd; while (true) { mQueue.WaitAndPop(cmd); if (cmd == START) { cmd = NO_CMD; if (mTcpClient.Connect(mHostName, mTcpPort) == true) { Protocol proto; while (mInitialized) { if (mTcpClient.DataWaiting(200U)) { std::string payload; if (mTcpClient.Recv(payload)) { proto.Add(payload); std::string data; while (proto.Parse(data)) { // TLogNetwork("Found one packet with data: " + data); std::vector<Reply> out; std::uint32_t client_uuid = proto.GetDestUuid(); bool ret = mListener.Deliver(proto.GetSourceUuid(), client_uuid, data, out); // Send synchronous data to the server if (ret) { Send(out); } } } } else { //std::cout << "client wait timeout or failure" << std::endl; mInitialized = IsConnected(); // determine origine of failure } } mListener.Signal(net::IEvent::ErrDisconnectedFromServer); TLogNetwork("Client connection closed."); } else { TLogError("Client cannot connect to server."); mListener.Signal(net::IEvent::ErrCannotConnectToServer); } } else if (cmd == EXIT) { std::cout << "Exit client thread" << std::endl; return; } } }
DWORD WINAPI CompletionPortStackListener::WorkerThread(LPVOID obj) { CompletionPortStackListener* instance = (CompletionPortStackListener*)obj; int loopCtr1 = 0; stack<Structs::LP_JOBREQUEST> _jobList = instance->jobList; SOCKET _socket; while (true) { ::WaitForSingleObject(instance->ghHasMessageEvent, INFINITE); ::WaitForSingleObject(instance->ghMutex, INFINITE); loopCtr1++; ::WaitForSingleObject(instance->ghMutex, INFINITE); int sz = instance->jobList.size(); if (sz > 0) { ::WaitForSingleObject(instance->ghMutex2, INFINITE); Structs::LP_JOBREQUEST job = instance->jobList.top(); instance->jobList.pop(); ::ReleaseMutex(instance->ghMutex2); // // if (job == NULL) { continue; } _socket = job->socket; { try { Protocol* p = instance->_protocol; //*itr; if (p == NULL) { continue; } Structs::LP_JOBREQUEST jobresp = new Structs::JOBREQUEST(); jobresp->data = job->data; jobresp->len = job->len; jobresp->socket = job->socket; p->AddMessage(_strdup(jobresp->data)); LPVOID res = p->Parse(); Structs::LP_JOBREQUEST nextJob = (Structs::LP_JOBREQUEST)p->Next(); if (nextJob != NULL) { nextJob->socket = jobresp->socket; instance->inputStage->AddMessage(nextJob); } if (job != NULL) { //ZeroMemory(job, sizeof(Structs::JOBREQUEST)); //delete(job); } } catch (...) { int x = 1; } } } if (instance->jobList.size() == 0) { ::ResetEvent(instance->ghHasMessageEvent); } ::ReleaseMutex(instance->ghMutex); } return 1; }