ByteArrayPtr StreamSocket::recvBuf() { size_t size = recvSize(); ByteArrayPtr ret = ByteArrayPtr(new ByteArray(size)); is_.read(&(*ret)[0], size); if (is_.bad()) { throw std::string("recvBuf() error"); } return ret; }
std::string StreamSocket::recvMsg() { size_t length = recvSize(); if (length > MAX_CONTROL_MESSAGE_SIZE) { std::stringstream ss; ss << "length > MAX_CONTROL_MESSAGE_SIZE "; ss << length; throw ss.str(); } char buf2[length]; is_.read(buf2, length); if (is_.bad()) { throw std::string("recvMsg() error"); } return std::string(buf2, length); }
AWE::uint32 TCP_Transfer_ServerThread::runThread() { AWE::Socket::TCP::Protocol* pTCP = AWE::Socket::TCP::create(); CPPUNIT_ASSERT( pTCP ); // listen on our port AWE::Socket::Address kLocalAddress; CPPUNIT_ASSERT( AWE::Socket::Error::convertNativeError( pTCP->resolveLocalAddress( &kLocalAddress, 0/*SocketTestCase::s_iServPort*/, (SocketTestCase::s_bTestIP4 ? AF_INET : AF_INET6) ) ) == AWE::Socket::Error::Success ); CPPUNIT_ASSERT( AWE::Socket::Error::convertNativeError( pTCP->createSocket( kLocalAddress.getAddressFamily() ) ) == AWE::Socket::Error::Success ); AWE::Socket::StreamProtocolHost< AWE::Socket::TCP::Protocol > kHost( pTCP ); CPPUNIT_ASSERT( AWE::Socket::Error::convertNativeError( kHost.bind( kLocalAddress ) ) == AWE::Socket::Error::Success ); CPPUNIT_ASSERT( AWE::Socket::Error::convertNativeError( pTCP->getBoundAddress( &kLocalAddress ) ) == AWE::Socket::Error::Success ); AWE::Core::String sAddress; AWE::uint16 iPort( 0 ); kLocalAddress.getAddress( &sAddress ); kLocalAddress.getPort( &g_iServerPort ); iPort = g_iServerPort; std::cout << "[Server] Listening for data on IP \"" << sAddress << "\"" << " port " << g_iServerPort << std::endl << std::flush; SocketTestCase::s_kSemaphore.release(); CPPUNIT_ASSERT( AWE::Socket::Error::convertNativeError( kHost.listen() ) == AWE::Socket::Error::Success ); AWE::Socket::Address kRemoteAddress; AWE::Socket::TCP::Protocol* pRemoteConn = 0; CPPUNIT_ASSERT( AWE::Socket::Error::convertNativeError( kHost.accept( &pRemoteConn, &kRemoteAddress ) ) == AWE::Socket::Error::Success ); CPPUNIT_ASSERT( pRemoteConn ); kRemoteAddress.getAddress( &sAddress ); kRemoteAddress.getPort( &iPort ); std::cout << "[Server] Received connection from IP \"" << sAddress << "\"" << " port " << iPort << std::endl << std::flush; // receive some data const AWE::uint32 dataSize( 100 ); AWE::int8 pData[ dataSize ]; memset( pData, 0, dataSize ); AWE::uint32 recvSize( pRemoteConn->receive( pData, dataSize ) ); // check we received the correct text CPPUNIT_ASSERT( recvSize == g_sText.length() ); CPPUNIT_ASSERT( strcmp( g_sText.c_str(), pData ) == 0 ); std::cout << "[Server] Received \"" << pData << "\"" << std::endl << std::flush; #if defined( WAIT_BEFORE_DESTROY ) // release our mutex so the client can run // <<-- Unlock SocketTestCase::s_kSemaphore.release(); #endif CPPUNIT_ASSERT( AWE::Socket::Error::convertNativeError( pRemoteConn->closeSocket() ) == AWE::Socket::Error::Success ); CPPUNIT_ASSERT( AWE::Socket::Error::convertNativeError( pTCP->closeSocket() ) == AWE::Socket::Error::Success ); AWE_SAFE_DELETE( pRemoteConn ); AWE_SAFE_DELETE( pTCP ); return 0; }