void JNetwork::ThreadProc(void* param) { JNetwork* pThis = reinterpret_cast<JNetwork*>(param); JSocket* pSocket = NULL; if (pThis->serverIP.size()) { DebugTrace("Starting Client Thread"); pThis->socket = new JSocket(pThis->serverIP); if(pThis->socket->isConnected()) pSocket = pThis->socket; } else { DebugTrace("Starting Server Thread"); pThis->socket = new JSocket(); // Wait for some client pSocket = pThis->socket->Accept(); } while(pSocket && pSocket->isConnected()) { char buff[4096]; { int len = pSocket->Read(buff, sizeof(buff)); if(len > 0) { boost::mutex::scoped_lock l(pThis->receiveMutex); DebugTrace("receiving " << len << " bytes : " << buff); pThis->received.str(pThis->received.str() + buff); DebugTrace("received " << pThis->received.str().size() << " bytes : " << pThis->received.str()); } } { boost::mutex::scoped_lock l(pThis->sendMutex); if(!pThis->toSend.str().empty()) { DebugTrace("sending " << pThis->toSend.str().size() << " bytes : " << pThis->toSend.str()); pSocket->Write((char*)pThis->toSend.str().c_str(), pThis->toSend.str().size()+1); pThis->toSend.str(""); } } boost::this_thread::sleep(boost::posix_time::milliseconds(1)); } DebugTrace("Quitting Thread"); }
void JNetwork::ThreadProc(void* param) { JNetwork* pThis = reinterpret_cast<JNetwork*>(param); JSocket* pSocket = NULL; if (pThis->serverIP.size()) { DebugTrace("Starting Client Thread"); pThis->socket = new JSocket(pThis->serverIP); if(pThis->socket->isConnected()) pSocket = pThis->socket; } else { DebugTrace("Starting Server Thread"); pThis->socket = new JSocket(); // Wait for some client pSocket = pThis->socket->Accept(); } while(pSocket && pSocket->isConnected()) { char buff[1024]; { boost::mutex::scoped_lock l(pThis->receiveMutex); int len = pSocket->Read(buff, sizeof(buff)); if(len) { DebugTrace("receiving " << len << " bytes : " << buff); pThis->received << buff; } // Checking for some command to execute size_t found = pThis->received.str().find("Command"); if(found != string::npos) { map<string, processCmd>::iterator ite = sCommandMap.find((pThis->received.str()).substr(0, found) + "Command"); if(ite != sCommandMap.end()) { DebugTrace("begin of command received : "<< pThis->received.str() ); DebugTrace("begin of command toSend : "<< pThis->toSend.str() ); boost::mutex::scoped_lock l(pThis->sendMutex); pThis->toSend << pThis->received.str().substr(0, found) + "Response "; pThis->received.str(""); processCmd theMethod = (ite)->second; theMethod(pThis->received, pThis->toSend); DebugTrace("end of command received : "<< pThis->received.str() ); DebugTrace("end of command toSend : "<< pThis->toSend.str() ); } } // Checking for some response to execute found = pThis->received.str().find("Response"); if(found != string::npos) { map<string, processCmd>::iterator ite = sCommandMap.find((pThis->received.str()).substr(0, found) + "Response"); if(ite != sCommandMap.end()) { DebugTrace("begin of response received : "<< pThis->received.str() ); DebugTrace("begin of response toSend : "<< pThis->toSend.str() ); boost::mutex::scoped_lock l(pThis->sendMutex); string aString; pThis->received >> aString; processCmd theMethod = (ite)->second; theMethod(pThis->received, pThis->toSend); pThis->received.str(""); DebugTrace("end of response received : "<< pThis->received.str() ); DebugTrace("end of response toSend : "<< pThis->toSend.str() ); } } }
JSocketOutputStream::JSocketOutputStream(const JSocket& sock) : JDescriptor(sock.getFd()) {}