void __cdecl CJabberProto::FileReceiveThread(filetransfer *ft) { ThreadData info(this, NULL); debugLogA("Thread started: type=file_receive server='%s' port='%d'", ft->httpHostName, ft->httpPort); ft->type = FT_OOB; NETLIBOPENCONNECTION nloc = { 0 }; nloc.cbSize = sizeof(nloc); nloc.szHost = ft->httpHostName; nloc.wPort = ft->httpPort; info.s = (HANDLE)CallService(MS_NETLIB_OPENCONNECTION, (WPARAM)m_hNetlibUser, (LPARAM)&nloc); if (info.s == NULL) { debugLogA("Connection failed (%d), thread ended", WSAGetLastError()); ProtoBroadcastAck(ft->std.hContact, ACKTYPE_FILE, ACKRESULT_FAILED, ft, 0); } else { ft->s = info.s; char buffer[JABBER_NETWORK_BUFFER_SIZE]; int datalen = mir_snprintf(buffer, "GET /%s HTTP/1.1\r\nHost: %s\r\n\r\n", ft->httpPath, ft->httpHostName); info.send(buffer, datalen); ft->state = FT_CONNECTING; debugLogA("Entering file_receive recv loop"); datalen = 0; while (ft->state != FT_DONE && ft->state != FT_ERROR) { int recvResult, bytesParsed; debugLogA("Waiting for data..."); recvResult = info.recv(buffer+datalen, JABBER_NETWORK_BUFFER_SIZE-datalen); if (recvResult <= 0) break; datalen += recvResult; bytesParsed = FileReceiveParse(ft, buffer, datalen); if (bytesParsed < datalen) memmove(buffer, buffer+bytesParsed, datalen-bytesParsed); datalen -= bytesParsed; } ft->s = NULL; if (ft->state == FT_DONE || (ft->state == FT_RECEIVING && ft->std.currentFileSize == 0)) ft->complete(); debugLogA("Thread ended: type=file_receive server='%s'", ft->httpHostName); } delete ft; }
void __cdecl CJabberProto::FileReceiveThread( filetransfer* ft ) { char* buffer; int datalen; ThreadData info( this, JABBER_SESSION_NORMAL ); Log( "Thread started: type=file_receive server='%s' port='%d'", ft->httpHostName, ft->httpPort ); ft->type = FT_OOB; if (( buffer=( char* )mir_alloc( JABBER_NETWORK_BUFFER_SIZE )) == NULL ) { Log( "Cannot allocate network buffer, thread ended" ); JSendBroadcast( ft->std.hContact, ACKTYPE_FILE, ACKRESULT_FAILED, ft, 0 ); delete ft; return; } NETLIBOPENCONNECTION nloc = { 0 }; nloc.cbSize = sizeof( nloc ); nloc.cbSize = sizeof( NETLIBOPENCONNECTION ); nloc.szHost = ft->httpHostName; nloc.wPort = ft->httpPort; info.s = ( HANDLE ) JCallService( MS_NETLIB_OPENCONNECTION, ( WPARAM ) m_hNetlibUser, ( LPARAM )&nloc ); if ( info.s == NULL ) { Log( "Connection failed ( %d ), thread ended", WSAGetLastError()); JSendBroadcast( ft->std.hContact, ACKTYPE_FILE, ACKRESULT_FAILED, ft, 0 ); mir_free( buffer ); delete ft; return; } ft->s = info.s; info.send( "GET /%s HTTP/1.1\r\nHost: %s\r\n\r\n", ft->httpPath, ft->httpHostName ); ft->state = FT_CONNECTING; Log( "Entering file_receive recv loop" ); datalen = 0; while ( ft->state != FT_DONE && ft->state != FT_ERROR ) { int recvResult, bytesParsed; Log( "Waiting for data..." ); recvResult = info.recv( buffer+datalen, JABBER_NETWORK_BUFFER_SIZE-datalen ); if ( recvResult <= 0 ) break; datalen += recvResult; bytesParsed = FileReceiveParse( ft, buffer, datalen ); if ( bytesParsed < datalen ) memmove( buffer, buffer+bytesParsed, datalen-bytesParsed ); datalen -= bytesParsed; } ft->s = NULL; if ( ft->state==FT_DONE || ( ft->state==FT_RECEIVING && ft->std.currentFileSize < 0 )) ft->complete(); Log( "Thread ended: type=file_receive server='%s'", ft->httpHostName ); mir_free( buffer ); delete ft; }