static Waifu2x::eWaifu2xError readProtoBinary(const boost::filesystem::path &path, ::google::protobuf::Message* proto) { boost::iostreams::stream<boost::iostreams::file_descriptor_source> is; try { is.open(path, std::ios_base::in | std::ios_base::binary); } catch (...) { return Waifu2x::eWaifu2xError_FailedOpenModelFile; } if (!is) return Waifu2x::eWaifu2xError_FailedOpenModelFile; std::vector<char> tmp; if (!readFile(is, tmp)) return Waifu2x::eWaifu2xError_FailedParseModelFile; google::protobuf::io::ArrayInputStream input(tmp.data(), tmp.size()); google::protobuf::io::CodedInputStream coded_input(&input); coded_input.SetTotalBytesLimit(kProtoReadBytesLimit, 536870912); const bool success = proto->ParseFromCodedStream(&coded_input); if (!success) return Waifu2x::eWaifu2xError_FailedParseModelFile; return Waifu2x::eWaifu2xError_OK; }
void download_p2p(int sockfd){ int count; char bufferFST[4]; count = recv(sockfd, bufferFST, 4, MSG_PEEK); if(count == -1) perror("Recv with error"); google::protobuf::uint32 pkg_size = readHdr(bufferFST); int byteCount; file::File request_file; char buffer[pkg_size + HDR_SIZE]; if( ( byteCount = recv(sockfd, (void *)buffer, pkg_size + HDR_SIZE, MSG_WAITALL) ) == -1 ){ perror("Error recviving data"); } google::protobuf::io::ArrayInputStream ais(buffer, pkg_size + HDR_SIZE); google::protobuf::io::CodedInputStream coded_input(&ais); coded_input.ReadVarint32(&pkg_size); google::protobuf::io::CodedInputStream::Limit msgLimit = coded_input.PushLimit(pkg_size); request_file.ParseFromCodedStream(&coded_input); for(auto it=file_sets.begin(); it!=file_sets.end(); ++it){ if( it-> first == request_file.file_name() && !it->second.empty() ){ sendCheck(sockfd, true); send_download_info(sockfd, it->second); return; } } sendCheck(sockfd, false); return; }
GameEngineMessage* NetUtils::ReadBody( net::Socket* socket, google::protobuf::uint32 size ) { int bytecount; GameEngineMessage* payload = new GameEngineMessage(); char buffer [ size + 4 ];//size of the payload and hdr //Read the entire buffer including the hdr bytecount = socket->ReceiveWaitAll( (void *)buffer, 4 + size ); printf( "Second read byte count is %d\n", bytecount ); //Assign ArrayInputStream with enough memory google::protobuf::io::ArrayInputStream ais( buffer, size + 4 ); CodedInputStream coded_input( &ais ); //Read an unsigned integer with Varint encoding, truncating to 32 bits. coded_input.ReadVarint32( &size ); //After the message's length is read, PushLimit() is used to prevent the CodedInputStream //from reading beyond that length. Limits are used when parsing length-delimited //embedded messages google::protobuf::io::CodedInputStream::Limit msgLimit = coded_input.PushLimit( size ); //De-Serialize payload->ParseFromCodedStream( &coded_input ); //Once the embedded message has been parsed, PopLimit() is called to undo the limit coded_input.PopLimit( msgLimit ); //Print sender of message printf( "Message from %s\n", socket->GetSenderAddress().ToString().c_str() ); //Print the message printf( "Message is %s\n", payload->DebugString().c_str() ); return payload; }
google::protobuf::uint32 readHdr(char *buf){ google::protobuf::uint32 size; google::protobuf::io::ArrayInputStream ais(buf,4); google::protobuf::io::CodedInputStream coded_input(&ais); coded_input.ReadVarint32(&size); //Decode the HDR and get the size //cout<<"size of payload is "<<size<<endl; return size; }
google::protobuf::uint32 NetUtils::ReadHeader( char *buf ) { google::protobuf::uint32 size; google::protobuf::io::ArrayInputStream ais( buf,4 ); CodedInputStream coded_input( &ais ); coded_input.ReadVarint32( &size );//Decode the HDR and get the size printf( "Size of payload is %d\n", size ); return size; }
void readBody(int csock, google::protobuf::uint32 siz){ int byteCount; login::Login login; char buffer[siz+4]; if( ( byteCount = recv(csock, (void *)buffer, 4+siz, MSG_WAITALL) )== -1 ){ fprintf(stderr, "Error receiving data %d\n", errno); } google::protobuf::io::ArrayInputStream ais(buffer,siz+4); google::protobuf::io::CodedInputStream coded_input(&ais); coded_input.ReadVarint32(&siz); google::protobuf::io::CodedInputStream::Limit msgLimit = coded_input.PushLimit(siz); login.ParseFromCodedStream(&coded_input); coded_input.PopLimit(msgLimit); std::cout << login.DebugString(); }
login::DeleteInfo readDeleteInfo(int csock, google::protobuf::uint32 size){ //recv login int byteCount; login::DeleteInfo info; char buffer[size + HDR_SIZE]; if( ( byteCount = recv(csock, (void *)buffer, size + HDR_SIZE, MSG_WAITALL) )== -1 ){ perror("Error recviving data"); } google::protobuf::io::ArrayInputStream ais(buffer, size + HDR_SIZE); google::protobuf::io::CodedInputStream coded_input(&ais); coded_input.ReadVarint32(&size); google::protobuf::io::CodedInputStream::Limit msgLimit = coded_input.PushLimit(size); info.ParseFromCodedStream(&coded_input); return info; }
bool readCheck(int csock, google::protobuf::uint32 size){ //recv check int byteCount; check::Check check; char buffer[size + HDR_SIZE]; if( ( byteCount = recv(csock, (void *)buffer, size + HDR_SIZE, MSG_WAITALL) )== -1 ){ perror("Error recviving data"); } google::protobuf::io::ArrayInputStream ais(buffer, size + HDR_SIZE); google::protobuf::io::CodedInputStream coded_input(&ais); coded_input.ReadVarint32(&size); google::protobuf::io::CodedInputStream::Limit msgLimit = coded_input.PushLimit(size); check.ParseFromCodedStream(&coded_input); //parse check if( check.check() == true ) return true; return false; }
void regist_check(int sockfd){ //recv regist int count; char bufferFST[4]; count = recv(sockfd, bufferFST, 4, MSG_PEEK); if(count == -1) perror("Recv with error"); google::protobuf::uint32 size = readHdr(bufferFST); int byteCount; regist::Regist regist; char buffer[size + HDR_SIZE]; if( ( byteCount = recv(sockfd, (void *)buffer, size + HDR_SIZE, MSG_WAITALL) ) == -1 ){ perror("Error recviving data"); } google::protobuf::io::ArrayInputStream ais(buffer, size + HDR_SIZE); google::protobuf::io::CodedInputStream coded_input(&ais); coded_input.ReadVarint32(&size); google::protobuf::io::CodedInputStream::Limit msgLimit = coded_input.PushLimit(size); regist.ParseFromCodedStream(&coded_input); coded_input.PopLimit(msgLimit); //std::cout << regist.DebugString(); //check id and password Data::Data newRegist; newRegist.set_id( regist.id() ); newRegist.set_password( regist.passwd() ); for(int i=0; i<loginData.logindata_size(); ++i){ if( loginData.logindata(i).id() == newRegist.id() ){ sendCheck(sockfd, false); return; } } Data::Data* data = loginData.add_logindata(); data -> set_id(regist.id()); data -> set_password(regist.passwd()); update(); sendCheck(sockfd, true); }
ACTION readAction(int csock, google::protobuf::uint32 size){ //recv action int byteCount; action::Action action; char buffer[size + HDR_SIZE]; if( ( byteCount = recv(csock, (void *)buffer, size + HDR_SIZE, MSG_WAITALL) )== -1 ){ perror("Error recviving data"); } google::protobuf::io::ArrayInputStream ais(buffer, size + HDR_SIZE); google::protobuf::io::CodedInputStream coded_input(&ais); coded_input.ReadVarint32(&size); google::protobuf::io::CodedInputStream::Limit msgLimit = coded_input.PushLimit(size); action.ParseFromCodedStream(&coded_input); //parse action if( action.action() == "login" ) return LOGIN; if( action.action() == "regist" ) return REGIST; if( action.action() == "deleteaccount" ) return DELETEACCOUNT; if( action.action() == "searchinfo" ) return SEARCHINFO; if( action.action() == "download" ) return DOWNLOAD; if( action.action() == "chat" ) return CHAT; if( action.action() == "logout" ) return LOGOUT; if( action.action() == "sendfileinfo" ) return RECVFILEINFO; if( action.action() == "onlineinfo" ) return ONLINEINFO; if( action.action() == "portrequest" ) return PORTREQUEST; if( action.action() == "messagetopeer" ) return PEERMESSAGE; if( action.action() == "askfile" ) return ASKFILE; return UNDEFINE; }
Packet Packet::Read(ClientService client) { Packet p; byte header[6]; long len = client.socket->Receive(header, 6); if (len > 0) { google::protobuf::io::CodedInputStream coded_input(header, (int)len); coded_input.ReadRaw(&p.ServiceId, 1); coded_input.ReadVarint32(&p.MethodId); byte requestIdTemp[2]; coded_input.ReadRaw(requestIdTemp, 2); p.RequestId = requestIdTemp[0] | (requestIdTemp[1] << 8); if (p.ServiceId != 0xfe) coded_input.ReadVarint64(&p.Unknown); coded_input.ReadVarint32(&p.PayloadLength); if (p.PayloadLength > 0) { p.Payload = new byte[p.PayloadLength]; //coded_input.ReadRaw(p.Payload, p.PayloadLength); client.socket->Receive(p.Payload, p.PayloadLength); } } else p.valid = false; return p; }
void chat(int sockfd){ // recv name int count; char bufferFST[4]; count = recv(sockfd, bufferFST, 4, MSG_PEEK); if(count == -1) perror("Recv with error"); google::protobuf::uint32 pkg_size = readHdr(bufferFST); int byteCount; online::OnlinePerson person; char buffer[pkg_size + HDR_SIZE]; if( ( byteCount = recv(sockfd, (void *)buffer, pkg_size + HDR_SIZE, MSG_WAITALL) ) == -1 ){ perror("Error recviving data"); } google::protobuf::io::ArrayInputStream ais(buffer, pkg_size + HDR_SIZE); google::protobuf::io::CodedInputStream coded_input(&ais); coded_input.ReadVarint32(&pkg_size); google::protobuf::io::CodedInputStream::Limit msgLimit = coded_input.PushLimit(pkg_size); person.ParseFromCodedStream(&coded_input); // check whether online for(int i=0; i<loginData.logindata_size(); ++i){ if( loginData.logindata(i).online() == true ){ if( loginData.logindata(i).id() != "" && loginData.logindata(i).id() == person.name() ){ sendCheck(sockfd, true); send_port(sockfd, loginData.logindata(i).port(), loginData.logindata(i).ip()); return; } } } sendCheck(sockfd, false); printf("!ok\n"); }
void recv_file_info(int sockfd, std::map<std::string, std::set<std::string> > &file_sets, login::Login user){ int count; char bufferFST[4]; count = recv(sockfd, bufferFST, 4, MSG_PEEK); if(count == -1) perror("Recv with error"); google::protobuf::uint32 pkg_size = readHdr(bufferFST); int byteCount; file::Files files; char buffer[pkg_size + HDR_SIZE]; if( ( byteCount = recv(sockfd, (void *)buffer, pkg_size + HDR_SIZE, MSG_WAITALL) ) == -1 ){ perror("Error recviving data"); } google::protobuf::io::ArrayInputStream ais(buffer, pkg_size + HDR_SIZE); google::protobuf::io::CodedInputStream coded_input(&ais); coded_input.ReadVarint32(&pkg_size); google::protobuf::io::CodedInputStream::Limit msgLimit = coded_input.PushLimit(pkg_size); files.ParseFromCodedStream(&coded_input); printf("%sFile update: \n", ANSI_COLOR_GREEN); for(int i=0; i<files.files_size(); ++i){ file::File file = files.files(i); if( file_sets.find( file.file_name() ) == file_sets.end() ){ file_sets[file.file_name()].insert(user.id()); std::cout << file.file_name() << ":"; print_file(file_sets[file.file_name()]); } else{ file_sets[file.file_name()].insert(user.id()); std::cout << file.file_name() << ":"; print_file(file_sets[file.file_name()]); } } printf("%s\n", ANSI_COLOR_RESET); update(); }