コード例 #1
0
void inboundLoop(socket_ptr sock, string_ptr prompt)
{
    int bytesRead = 0;
    char readBuf[1024] = {0};

    for(;;)
    {
        if(sock->available())
        {
            bytesRead = sock->read_some(buffer(readBuf, inputSize));
            string_ptr msg(new string(readBuf, bytesRead));

            messageQueue->push(msg);
        }

        boost::this_thread::sleep( boost::posix_time::millisec(1000));
    }
}
コード例 #2
0
ファイル: client.cpp プロジェクト: hanchen999/OS94
void inboundLoop(socket_ptr sock, string_ptr prompt)
{
    std::size_t bytesRead = 0;
    char readBuf[1024] = {0};
    
    for(;;)
    {
        if(sock->is_open() && sock->available())
        {
            bytesRead = sock->read_some(buffer(readBuf, inputSize));
            string_ptr msg(new string(readBuf, bytesRead));
            
            messageQueue->push(msg);
            cout << "messages received correctly" << endl;
        }
        
        boost::this_thread::sleep( boost::posix_time::millisec(50));
    }
}
コード例 #3
0
ファイル: server.cpp プロジェクト: LudwikJaniuk/chat_test
void requestLoop()
{
	for(;;)
	{
		if(!clientList->empty())
		{
			// Poorly designed loop, client sockets 
			// should alert the server when they have new messages; 
			// the server shouldn't poll the clientList while holding a lock
			mtx.lock();
			for(auto& clientSock : *clientList)
			{
				if(clientSock->available())
				{
					char readBuf[bufSize] = {0};

					int bytesRead = clientSock->read_some(buffer(readBuf, bufSize));

					string_ptr msg(new string(readBuf, bytesRead));

					if(clientSentExit(msg))
					{
						disconnectClient(clientSock);
						break;
					}

					clientMap_ptr cm(new clientMap);
					cm->insert(pair<socket_ptr, string_ptr>(clientSock, msg));

					messageQueue->push(cm);

					cout << "ChatLog: " << *msg << endl;
				}
			}
			mtx.unlock();
		}

		boost::this_thread::sleep( boost::posix_time::millisec(sleepLen::lon));
	}
}