HRESULT CServer::OnConnectionClosed() { // retrieve socket CCOMSocketServer::Socket *pSocket = m_pSocketServer->GetSocket(); if (pSocket) { ISocket *pISocket = reinterpret_cast<ISocket *>(pSocket->GetUserPtr()); if (pISocket) { Fire_OnConnectionClosed(pISocket); pISocket->Release(); } pSocket->SetUserPtr(0); return S_OK; } return Error(L"Internal error: failed to obtain ISocket"); }
void SocketOutputStream::write(const XalanDOMString& s, XalanDOMString::size_type offset, XalanDOMString::size_type length) { //printf(stderr,"write(DOMString='%s',offset=%d,length=%s", s.c_str(), offset, length); m_socket->write((const char*)(s.c_str()+offset), length*sizeof(XalanDOMChar)); }
void SocketOutputStream::write(XalanDOMChar c) { //printf(stderr, "write(%c)", c); m_socket->write((const char*)&c, sizeof(XalanDOMChar)); }
void SocketOutputStream::write(const char* s, size_t offset, size_t length) { //wprintf(stderr,TEXT("write(char*='%s',offset=%d,length=%d)\n"),s,offset,length); m_socket->write(s + offset, length); }
unsigned sendData(const UdpPermitToSendMsg &permit, bool isLocal, TokenBucket *bucket, bool &moreRequested, unsigned &maxPackets) { moreRequested = false; maxPackets = permit.max_data; PointerArray toSend; unsigned totalSent = cleanRetryData(permit, toSend); while (toSend.length() < maxPackets && dataQueued()) { DataBuffer *buffer = popQueuedData(); if (buffer) // Aborted slave queries leave NULL records on queue { UdpPacketHeader *header = (UdpPacketHeader*) buffer->data; toSend.append(buffer); totalSent += header->length; #ifdef __linux__ if (isLocal && (totalSent> 100000)) break; #endif } } maxPackets = toSend.length(); for (unsigned idx = 0; idx < maxPackets; idx++) { DataBuffer *buffer = (DataBuffer *) toSend.item(idx); UdpPacketHeader *header = (UdpPacketHeader*) buffer->data; bool isRetry = (header->udpSequence != 0); if (isRetry) { if (checkTraceLevel(TRACE_RETRY_DATA, 1)) DBGLOG("UdpSender: Resending packet to destination node %u sequence %u", permit.destNodeIndex, header->udpSequence); atomic_inc(&packetsRetried); } else header->udpSequence = nextUdpSequence(); unsigned length = header->length; if (bucket) { MTIME_SECTION(timer, "bucket_wait"); bucket->wait((length / 1024)+1); } try { if (udpSendCompletedInData) { if (idx == maxPackets-1) { // MORE - is this safe ? Any other thread looking at the data right now? Don't _think_ so... if (false && dataQueued()) // Causes some problems because no flow control info gets through at all { moreRequested = true; header->udpSequence |= (UDP_SEQUENCE_COMPLETE|UDP_SEQUENCE_MORE); } else header->udpSequence |= UDP_SEQUENCE_COMPLETE; } } #ifdef _SIMULATE_LOST_PACKETS if (isRetry || (header->udpSequence % 100) != 0) #endif data_socket->write(buffer->data, length); header->udpSequence &= ~UDP_SEQUENCE_BITS; } catch(IException *e) { StringBuffer s; DBGLOG("UdpSender: write exception - write(%p, %u) - %s", buffer->data, length, e->errorMessage(s).str()); e->Release(); } catch(...) { DBGLOG("UdpSender: write exception - unknown exception"); } if (!isRetry && maxRetryData) { unsigned slot = (retryDataIdx + retryDataCount) % maxRetryData; if (retryDataCount < maxRetryData) retryDataCount++; else { if (udpTraceLevel > 0) DBGLOG("Overflow in resend packet buffer for destination node %u - discarding packet sequence %u", permit.destNodeIndex, header->udpSequence); ::Release(retryData[slot]); } retryData[slot] = buffer; } else { ::Release(buffer); } } return totalSent; }
void DoServerConnect() { #if defined( USE_MPI ) ISocket *pSocket = CreateIPSocket(); if ( !pSocket ) { printf( "Error creating a socket.\n" ); assert( false ); return; } else if ( !pSocket->BindToAny( VMPI_SERVICE_PORT ) ) { printf( "Error binding a socket to port %d.\n", VMPI_SERVICE_PORT ); assert( false ); return; } printf( "Waiting for jobs...\n" ); while ( 1 ) { // Any incoming packets? char data[2048]; CIPAddr ipFrom; int len = pSocket->RecvFrom( data, sizeof( data ), &ipFrom ); if ( len > 3 ) { bf_read buf( data, len ); if ( buf.ReadByte() == VMPI_PROTOCOL_VERSION ) { if ( buf.ReadByte() == VMPI_LOOKING_FOR_WORKERS ) { // Read the listen port. int iListenPort = buf.ReadLong(); static char ipString[128]; _snprintf( ipString, sizeof( ipString ), "%d.%d.%d.%d:%d", ipFrom.ip[0], ipFrom.ip[1], ipFrom.ip[2], ipFrom.ip[3], iListenPort ); int argc = 3; char *testargv[3]; testargv[0] = "<supposedly the executable name!>"; testargv[1] = "-mpi_worker"; testargv[2] = ipString; char **argv = testargv; if ( MPI_Init( &argc, &argv ) ) { assert( false ); } MPI_Comm_rank( MPI_COMM_WORLD, &myProcId ); int nProcs; MPI_Comm_size( MPI_COMM_WORLD, &nProcs ); if ( nProcs != 2 ) { assert( false ); } break; } } } Sleep( 100 ); } pSocket->Release(); #else // Try to connect, or listen. ITCPListenSocket *pListen = CreateTCPListenSocket( g_iPortNum ); if ( !pListen ) { assert( false ); } printf( "Server listening...\n" ); CIPAddr addr; ITCPSocket *pTCPSocket = TCPSocket_ListenForOneConnection( pListen, &addr, 50000 ); if ( !pTCPSocket ) { assert( false ); } pListen->Release(); printf( "Server connected...\n "); g_pSocket = pTCPSocket; #endif }
int main(int ac, char **av) { std::string s; std::string token; size_t pos; Instruction instruct(Instruction::KO); ISocket *servTcp = getClient(); /* try { servTcp->attachOnReceive(&recvHandler); instruct.setInstruct(Instruction::CONNEXION); instruct.addName("fabY"); servTcp->writePacket(Packet::pack(instruct)); #ifdef _WIN_32 Sleep(100); #else usleep(10000); #endif instruct.setInstruct(Instruction::CREATE_ROOM); servTcp->writePacket(Packet::pack(instruct)); #ifdef _WIN_32 Sleep(100); #else usleep(10000); #endif instruct.setInstruct(Instruction::START_GAME); servTcp->writePacket(Packet::pack(instruct)); #ifdef _WIN_32 Sleep(100); #else usleep(10000); #endif game(); } catch (BBException &err) { std::cerr << err.what() << std::endl; system("pause"); } */ while (s != "quit") { instruct.eraseNames(); std::getline(std::cin, s); if ((pos = s.find(" ")) != s.npos) { token = s.substr(0, pos); if (token == "connect") instruct.setInstruct(Instruction::CONNEXION); else if (token == "join") instruct.setInstruct(Instruction::JOIN_ROOM); else if (token == "create") instruct.setInstruct(Instruction::CREATE_ROOM); s.erase(0, pos + 1); if (s.size() > 0) { instruct.addName(s); servTcp->writePacket(Packet::pack(instruct)); } } else if (s == "roomusers") { instruct.setInstruct(Instruction::GETALLUSERSINROOM); servTcp->writePacket(Packet::pack(instruct)); } else if (s == "users") { instruct.setInstruct(Instruction::GETALLUSERNAMES); servTcp->writePacket(Packet::pack(instruct)); } else if (s == "rooms") { instruct.setInstruct(Instruction::GETALLROOMNAMES); servTcp->writePacket(Packet::pack(instruct)); } else if (s == "disconnect") { instruct.setInstruct(Instruction::DECONNEXION); servTcp->writePacket(Packet::pack(instruct)); } else if (s == "leave") { instruct.setInstruct(Instruction::LEAVE_ROOM); servTcp->writePacket(Packet::pack(instruct)); } else if (s == "start") { instruct.setInstruct(Instruction::START_GAME); servTcp->writePacket(Packet::pack(instruct)); game(); break; } //servUdp->writePacket(Packet::pack<std::string>(s)); } servTcp->cancel(); #ifdef _WIN_32 Sleep(1000); #else sleep(1); #endif delete servTcp; return (0); }
void Record::Read( ISocket& socket ) { unsigned char type = 0; socket.Recieve( &type, sizeof( unsigned char ) ); //std::cout << "Type: " << (int)type << std::endl; mType = static_cast<ProtocolType>( type ); socket.Recieve( &mVersion.Major, sizeof( unsigned char ) ); socket.Recieve( &mVersion.Minor, sizeof( unsigned char ) ); //std::cout << "Version: " << (int)mVersion.Major << "." << (int)mVersion.Minor << std::endl; socket.Recieve( &mLength, sizeof( unsigned short ) ); mLength = ntohs( mLength ); //std::cout << "Length: " << (int)mLength << std::endl; mData.resize( mLength ); socket.Recieve( &mData[0], sizeof( unsigned char ) * mLength ); //std::cout << mEncryption->Name() << std::endl; if( mEncryption->Name() != "None" ){ //std::cout << "Decrypting with : " << mEncryption->Name() << std::endl; mData = mEncryption->Decrypt( mData ); // Check Padding unsigned int pos = mData.size() - 1; const unsigned char nPadding = mData[pos--]; std::vector<unsigned char> aPadding( nPadding ); for( unsigned int i = 0; i < nPadding; i++ ) aPadding[i] = mData[pos--]; mData.erase( mData.begin() + pos + 1, mData.end() ); // Check MAC pos = mData.size() - mEncryption->HashSize(); std::vector<unsigned char> aMAC( mEncryption->HashSize() ); for( unsigned int i = 0; i < mEncryption->HashSize(); i++ ) aMAC[i] = mData[pos++]; mData.erase( mData.end() - mEncryption->HashSize(), mData.end() ); /* Calculate MAC */ std::vector<unsigned char> signiture; Insert( signiture, htonll( RecordsRecieved ) ); Insert( signiture, (unsigned char) mType ); Insert( signiture, (unsigned char) mVersion.Major ); Insert( signiture, (unsigned char) mVersion.Minor ); Insert( signiture, htons( (unsigned short) mData.size() ) ); signiture.insert( signiture.end(), mData.begin(), mData.end() ); std::vector<unsigned char> hash = mEncryption->CalculateHash( signiture ); RecordsRecieved++; bool bDiff = false; for( unsigned int i = 0; i < mEncryption->HashSize(); i++ ){ if( hash[i] != aMAC[i] ) { bDiff = true; break; } } if( bDiff == true ) { std::cout << "Records Differ" << std::endl; std::cout << "Mac Should Be: " << std::dec << (int)aMAC.size() << std::endl; for( unsigned int i = 0 ; i < aMAC.size(); i++ ){ std::cout << std::hex << std::setw( 2 ) << std::setfill('0') << (int) aMAC[i] << " ";; if( (i + 1) % 16 == 0 ) std::cout << std::endl; } std::cout << std::endl; std::cout << "Mac Is: " << std::dec << (int)hash.size() << std::endl; for( unsigned int i = 0 ; i < hash.size(); i++ ){ std::cout << std::hex << std::setw( 2 ) << std::setfill('0') << (int) hash[i] << " ";; if( (i + 1) % 16 == 0 ) std::cout << std::endl; } std::cout << std::endl; throw std::exception(); } } //std::cout << "Recieved: " << std::dec << (int)mData.size() << std::endl; //for( unsigned int i = 0 ; i < mData.size(); i++ ){ // std::cout << std::hex << std::setw( 2 ) << std::setfill('0') << (int) mData[i] << " ";; // if( (i + 1) % 16 == 0 ) std::cout << std::endl; //} //std::cout << std::endl << std::endl; }
void Record::Write( ISocket& socket ) const { socket.Send( &mType, sizeof( unsigned char ) ); socket.Send( &mVersion.Major, sizeof( unsigned char ) ); socket.Send( &mVersion.Minor, sizeof( unsigned char ) ); if( mEncryption->Name() != "None" ) { std::vector<unsigned char> dData( mData.begin(), mData.end() ); /* Calculate MAC */ std::vector<unsigned char> signiture; Insert( signiture, htonll( RecordsSent ) ); Insert( signiture, (unsigned char) mType ); Insert( signiture, (unsigned char) mVersion.Major ); Insert( signiture, (unsigned char) mVersion.Minor ); Insert( signiture, htons( (unsigned short) mData.size() ) ); signiture.insert( signiture.end(), mData.begin(), mData.end() ); std::vector<unsigned char> hash = mEncryption->CalculateHash( signiture ); RecordsSent++; dData.insert( dData.end(), hash.begin(), hash.end() ); /* Calculate Padding */ unsigned int size = dData.size(); unsigned int remainder = 16 - (size - (16 * std::floor( dData.size() / 16.0 ))); if( remainder != 0 ){ remainder--; for( unsigned int i = 0; i < remainder; i++ ){ dData.push_back( remainder ); } } dData.push_back( remainder ); std::vector<unsigned char> eData = mEncryption->Encrypt( dData ); unsigned short length = htons( eData.size() ); socket.Send( &length, sizeof( unsigned short ) ); socket.Send( &eData[0], sizeof( unsigned char ) * eData.size() ); //std::cout << "Encrypted: " << std::dec << (int)eData.size() << std::endl; //for( unsigned int i = 0 ; i < eData.size(); i++ ){ // std::cout << std::hex << std::setw( 2 ) << std::setfill('0') << (int) eData[i] << " ";; // if( (i + 1) % 16 == 0 ) std::cout << std::endl; //} //std::cout << std::endl; }else{ unsigned short length = htons( mLength ); socket.Send( &length, sizeof( unsigned short ) ); socket.Send( &mData[0], sizeof( unsigned char ) * mLength ); //std::cout << "Sent: " << std::dec << (int)mData.size() << std::endl; //for( unsigned int i = 0 ; i < mData.size(); i++ ){ // std::cout << std::hex << std::setw( 2 ) << std::setfill('0') << (int) mData[i] << " ";; // if( (i + 1) % 16 == 0 ) std::cout << std::endl; //} //std::cout << std::endl; } //std::cout << std::endl; }