diff --git a/src/friend_server/fsclient.cc b/src/friend_server/fsclient.cc index f97b789ba..8118d44b1 100644 --- a/src/friend_server/fsclient.cc +++ b/src/friend_server/fsclient.cc @@ -20,6 +20,8 @@ * * ******************************************************************************/ +#include + #include "pqi/pqithreadstreamer.h" #include "retroshare/rspeers.h" #include "retroshare/rsnotify.h" @@ -29,6 +31,8 @@ #include "pqi/pqifdbin.h" #include "pqi/pqiproxy.h" +#define DEBUG_FSCLIENT + bool FsClient::requestFriends(const std::string& address,uint16_t port, const std::string& proxy_address,uint16_t proxy_port, uint32_t reqs, @@ -127,7 +131,7 @@ void FsClient::handleServerResponse(RsFriendServerServerResponseItem *item,std:: bool FsClient::sendItem(const std::string& server_address,uint16_t server_port, const std::string& proxy_address,uint16_t proxy_port, - RsItem *item,std::list& response) + RsFriendServerItem *item,std::list& response) { // open a connection @@ -145,6 +149,9 @@ bool FsClient::sendItem(const std::string& server_address,uint16_t server_port, return 1; } + int flags=1; + setsockopt(CreateSocket,SOL_SOCKET,TCP_NODELAY,(char*)&flags,sizeof(flags)); + ipOfServer.sin_family = AF_INET; ipOfServer.sin_port = htons(proxy_port); ipOfServer.sin_addr.s_addr = inet_addr(proxy_address.c_str()); @@ -182,26 +189,26 @@ bool FsClient::sendItem(const std::string& server_address,uint16_t server_port, pqithreadstreamer p(this,rss,RsPeerId(),bio,BIN_FLAGS_READABLE | BIN_FLAGS_WRITEABLE | BIN_FLAGS_NO_CLOSE); p.start(); - uint32_t ss; - p.SendItem(item,ss); - - RsDbg() << "Item sent. Waiting for response..." ; + RsDbg() << "Sending item. size=" << fss->size(item) << ". Waiting for response..." ; // Now attempt to read and deserialize anything that comes back from that connexion until it gets closed by the server. + uint32_t ss; + p.SendItem(item,ss); + while(true) { p.tick(); // ticks bio - RsItem *item = GetItem(); + RsItem *ritem = GetItem(); #ifdef DEBUG_FSCLIENT RsDbg() << "Ticking for response..."; #endif - if(item) + if(ritem) { - response.push_back(item); + response.push_back(ritem); std::cerr << "Got a response item: " << std::endl; - std::cerr << *item << std::endl; + std::cerr << *ritem << std::endl; RsDbg() << "End of transmission. " ; break; @@ -232,6 +239,8 @@ bool FsClient::checkProxyConnection(const std::string& onion_address,uint16_t po RsErr() << "Socket not created"; return false; } + int flags=1; + setsockopt(CreateSocket,SOL_SOCKET,TCP_NODELAY,(char*)&flags,sizeof(flags)); ipOfServer.sin_family = AF_INET; ipOfServer.sin_port = htons(proxy_port); diff --git a/src/friend_server/fsclient.h b/src/friend_server/fsclient.h index a3796dbfb..c8db2d764 100644 --- a/src/friend_server/fsclient.h +++ b/src/friend_server/fsclient.h @@ -48,7 +48,7 @@ protected: private: bool sendItem(const std::string &server_address, uint16_t server_port, const std::string &proxy_address, uint16_t proxy_port, - RsItem *item, std::list &response); + RsFriendServerItem *item, std::list &response); void handleServerResponse(RsFriendServerServerResponseItem *item, std::map &friend_certificates); diff --git a/src/pqi/pqifdbin.cc b/src/pqi/pqifdbin.cc index 2c422827d..1d3fd41ce 100644 --- a/src/pqi/pqifdbin.cc +++ b/src/pqi/pqifdbin.cc @@ -24,6 +24,10 @@ #include "util/rsfile.h" #include "pqi/pqifdbin.h" +#ifdef DEBUG_FS_BIN +#include "util/rsprint.h" +#endif + RsFdBinInterface::RsFdBinInterface(int file_descriptor, bool is_socket) : mCLintConnt(file_descriptor),mIsSocket(is_socket),mIsActive(false) { @@ -99,7 +103,7 @@ int RsFdBinInterface::read_pending() if(readbytes == 0) { RsDbg() << "Reached END of the stream!" ; - RsDbg() << "Closing!" ; + RsDbg() << "Closing socket!" ; close(); return mTotalInBufferBytes; @@ -125,8 +129,8 @@ int RsFdBinInterface::read_pending() if(readbytes > 0) { #ifdef DEBUG_FS_BIN - RsDbg() << "Received the following bytes: " << RsUtil::BinToHex( reinterpret_cast(inBuffer),readbytes,50) << std::endl; - RsDbg() << "Received the following bytes: " << std::string(inBuffer,readbytes) << std::endl; + RsDbg() << "Received the following bytes: size=" << readbytes << " len=" << RsUtil::BinToHex( reinterpret_cast(inBuffer),readbytes,50) << std::endl; + RsDbg() << "Received the following bytes: size=" << readbytes << " len=" << std::string(inBuffer,readbytes) << std::endl; #endif void *ptr = malloc(readbytes); @@ -248,6 +252,9 @@ int RsFdBinInterface::readdata(void *data, int len) if(in_buffer.empty()) { mTotalInBufferBytes -= total_len; +#ifdef DEBUG_FS_BIN + std::cerr << "RsFdBinInterface -- READ --- len=" << total_len << " data=" << RsUtil::BinToHex((uint8_t*)data,total_len)<< std::endl; +#endif return total_len; } @@ -255,16 +262,22 @@ int RsFdBinInterface::readdata(void *data, int len) if(total_len + in_buffer.front().second > len) { - memcpy(&(static_cast(data)[total_len]),in_buffer.front().first,len - total_len); + int bytes_in = len - total_len; + int bytes_out = in_buffer.front().second - bytes_in; - void *ptr = malloc(in_buffer.front().second - (len - total_len)); - memcpy(ptr,&(static_cast(in_buffer.front().first)[len - total_len]),in_buffer.front().second - (len - total_len)); + memcpy(&(static_cast(data)[total_len]),in_buffer.front().first,bytes_in); + + void *ptr = malloc(bytes_out); + memcpy(ptr,&(static_cast(in_buffer.front().first)[bytes_in]),bytes_out); free(in_buffer.front().first); in_buffer.front().first = ptr; - in_buffer.front().second -= len-total_len; + in_buffer.front().second -= bytes_in; mTotalInBufferBytes -= len; +#ifdef DEBUG_FS_BIN + std::cerr << "RsFdBinInterface -- READ --- len=" << len << " data=" << RsUtil::BinToHex((uint8_t*)data,len)<< std::endl; +#endif return len; } else // copy everything @@ -278,6 +291,9 @@ int RsFdBinInterface::readdata(void *data, int len) } } mTotalInBufferBytes -= len; +#ifdef DEBUG_FS_BIN + std::cerr << "RsFdBinInterface -- READ --- len=" << len << " data=" << RsUtil::BinToHex((uint8_t*)data,len)<< std::endl; +#endif return len; } @@ -285,6 +301,9 @@ int RsFdBinInterface::senddata(void *data, int len) { // shouldn't we better send in multiple packets, similarly to how we read? +#ifdef DEBUG_FS_BIN + std::cerr << "RsFdBinInterface -- SENDING --- len=" << len << " data=" << RsUtil::BinToHex((uint8_t*)data,len)<< std::endl; +#endif if(len == 0) { RsErr() << "Calling FsBioInterface::senddata() with null size or null data pointer"; @@ -332,9 +351,11 @@ bool RsFdBinInterface::cansend(uint32_t) int RsFdBinInterface::close() { RsDbg() << "Stopping network interface" << std::endl; + if(moretoread(0) || moretowrite(0)) + RsWarn() << "Interface still has " << mTotalInBufferBytes << " / " << mTotalOutBufferBytes << "bytes in/out buffers" << std::endl; + mIsActive = false; mCLintConnt = 0; - clean(); return 1; }