コード例 #1
0
ファイル: UdpRpcChannel.cpp プロジェクト: adanselm/pbRPCpp
    UdpRpcChannel::UdpRpcChannel(const string& serverAddr, const string& serverPort)
    : stop_( false ),
    serverAddr_(serverAddr),
    serverPort_(serverPort),
    io_service_initializer_( new IoServiceInitializer() ),
    socket_( io_service_initializer_->get_io_service() ) {
        
        udp::resolver resolver(io_service_initializer_->get_io_service() );
        udp::resolver::query query(serverAddr_, serverPort_);
        boost::system::error_code error;

        udp::resolver::iterator iter = resolver.resolve(query, error);

        if (error || iter == udp::resolver::iterator()) {
            GOOGLE_LOG(FATAL) << "fail to resolve address " << serverAddr_ << ":" << serverPort_;
        } else {
            remoteEndpoint_ = *iter;

            socket_.open(remoteEndpoint_.protocol(), error);
            if( error ) {
                GOOGLE_LOG( ERROR ) << "fail to open the UDP socket";
            } else {
                socket_.set_option(udp::socket::reuse_address(true), error);
                socket_.set_option(udp::socket::send_buffer_size( RpcMessage::MAX_UDP_SIZE ), error);
                socket_.set_option(udp::socket::receive_buffer_size( RpcMessage::MAX_UDP_SIZE ), error);
                startRead();
            }
        }
    }
コード例 #2
0
ファイル: BaseRpcChannel.cpp プロジェクト: adanselm/pbRPCpp
	void BaseRpcChannel::processResponse( const string& responseMsg ) {
		try {
			istringstream in( responseMsg );
			int msgType = Util::readInt( in );

			switch( msgType ) {
			case RpcMessage::RESPONSE_MSG: 
				{                            
					string callId;
					RpcController controller;
					shared_ptr<Message> response;
					//parse the response
					RpcMessage::parseResponseFrom( in, callId, controller, response );
					GOOGLE_LOG( INFO ) << "received a response message, callId:" << callId;
					//if the response is still not timeout
					shared_ptr<ResponseParam> respParam = waitingResponses_->erase( callId );
					if( respParam ) {                            
						timer_->cancel( callId );
						RpcController* pController = dynamic_cast<RpcController*>( respParam->controller );
						if( pController ) {
							copyController( *pController, controller );
						}
						if( respParam->response && response ) {
							copyMessage( *(respParam->response), *response );
						}

						if( respParam->completed ) {
							
							{
								boost::lock_guard<boost::mutex> lock(mMutex);
								*(respParam->completed) = true;
							}
							mCondVariable.notify_all();
						}

						if( respParam->done ) {
							respParam->done->Run();
						}

						if( pController ) {
							pController->complete();
						}

					}
				}
				break;
			}

		}catch( const std::exception& ex) {
			GOOGLE_LOG( ERROR ) << "catch exception:" << ex.what() ;
		} catch( ... ) {
			GOOGLE_LOG( ERROR ) << "catch unknown exception";
		}
	}
コード例 #3
0
ファイル: printer.cpp プロジェクト: marmalade/protocolbuffers
void Printer::Print(const map<string, string>& variables, const char* text) {
  int size = strlen(text);
  int pos = 0;  // The number of bytes we've written so far.

  for (int i = 0; i < size; i++) {
    if (text[i] == '\n') {
      // Saw newline.  If there is more text, we may need to insert an indent
      // here.  So, write what we have so far, including the '\n'.
      WriteRaw(text + pos, i - pos + 1);
      pos = i + 1;

      // Setting this true will cause the next WriteRaw() to insert an indent
      // first.
      at_start_of_line_ = true;

    } else if (text[i] == variable_delimiter_) {
      // Saw the start of a variable name.

      // Write what we have so far.
      WriteRaw(text + pos, i - pos);
      pos = i + 1;

      // Find closing delimiter.
      const char* end = strchr(text + pos, variable_delimiter_);
      if (end == NULL) {
        GOOGLE_LOG(DFATAL) << " Unclosed variable name.";
        end = text + pos;
      }
      int endpos = end - text;

      string varname(text + pos, endpos - pos);
      if (varname.empty()) {
        // Two delimiters in a row reduce to a literal delimiter character.
        WriteRaw(&variable_delimiter_, 1);
      } else {
        // Replace with the variable's value.
        map<string, string>::const_iterator iter = variables.find(varname);
        if (iter == variables.end()) {
          GOOGLE_LOG(DFATAL) << " Undefined variable: " << varname;
        } else {
          WriteRaw(iter->second.data(), iter->second.size());
        }
      }

      // Advance past this variable.
      i = endpos;
      pos = endpos + 1;
    }
  }

  // Write the rest.
  WriteRaw(text + pos, size - pos);
}
コード例 #4
0
ファイル: UdpRpcChannel.cpp プロジェクト: adanselm/pbRPCpp
    void UdpRpcChannel::handlePacketWrite(const boost::system::error_code& ec,
            std::size_t bytes_transferred,
            shared_ptr<string> buf,
            boost::function< void (bool, const string&) > resultCb) {

        if (ec) {
            GOOGLE_LOG(ERROR) << "fail to send packet to server";
            resultCb(false, "fail to send packet to server");
        } else {
            GOOGLE_LOG(INFO) << "succeed to send " << bytes_transferred << " bytes to server";
            resultCb(true, "success to send the packet to server");
        }
    }
コード例 #5
0
ファイル: TcpRpcChannel.cpp プロジェクト: adanselm/pbRPCpp
    void TcpRpcChannel::handleDataWrite(const boost::system::error_code& ec,
            std::size_t bytes_transferred,
            shared_ptr<string> buf,
            boost::function< void (bool, const string&) > resultCb) {

        if (ec) {
            GOOGLE_LOG(ERROR) << "fail to send message to server";
            resultCb(false, "fail to connect to server");
        } else {
            resultCb( true, "send data to server successfully");
            GOOGLE_LOG(INFO) << "success to send " << bytes_transferred << " bytes message to server";
        }

    }
コード例 #6
0
ファイル: ShmRpcServer.cpp プロジェクト: adanselm/pbRPCpp
 void ShmRpcServer::Run() {
   if( ! inQueue_->startCreate(segmentName_ + "-c2s", boost::bind( &ShmRpcServer::messageReceived, this, _1))
       || ! outQueue_->startCreate(segmentName_ + "-s2c") )
   {
     GOOGLE_LOG(FATAL) << "fail to connect to segment " << segmentName_;
   }
 }
コード例 #7
0
ファイル: printer.cpp プロジェクト: marmalade/protocolbuffers
void Printer::Outdent() {
  if (indent_.empty()) {
    GOOGLE_LOG(DFATAL) << " Outdent() without matching Indent().";
    return;
  }

  indent_.resize(indent_.size() - 2);
}
コード例 #8
0
ファイル: TcpRpcChannel.cpp プロジェクト: adanselm/pbRPCpp
 void TcpRpcChannel::serverConnected(const boost::system::error_code& ec, tcp::resolver::iterator iter) {
     if (ec) {
         if (iter == tcp::resolver::iterator()) {
             GOOGLE_LOG(ERROR) << "fail to connect to server " << serverAddr_ << ":" << serverPort_;
             connectTried_ = true;
             startConnect();
         } else {
             doConnect(++iter);
         }
     } else {
         //the connection to server is established
         GOOGLE_LOG(INFO) << "connect to server " << serverAddr_ << ":" << serverPort_ << " successfully";
         boost::system::error_code error;
         sock_.set_option(tcp::socket::reuse_address(true), error);
         startRead();
         connectTried_ = true;
     }
 }
コード例 #9
0
ファイル: TcpRpcChannel.cpp プロジェクト: adanselm/pbRPCpp
    void TcpRpcChannel::dataReceived(const boost::system::error_code& ec,
            std::size_t bytes_transferred) {
        if (ec) {
            GOOGLE_LOG(ERROR) << "fail to receive data from server";
            startConnect();
            return;
        }

        GOOGLE_LOG(INFO) << "received " << bytes_transferred << " bytes from server";
        receivedMsg_.append(msgBuffer_, bytes_transferred);

        startRead();

        string msg;

        while (extractMessage(msg)) {
            responseReceived(msg);
        }

    }
コード例 #10
0
ファイル: TcpRpcChannel.cpp プロジェクト: adanselm/pbRPCpp
    void TcpRpcChannel::doConnect(tcp::resolver::iterator iter) {

        //try to connect to server
        if (iter != tcp::resolver::iterator()) {
            sock_.async_connect(*iter, boost::bind(&TcpRpcChannel::serverConnected, this, _1, iter));
        } else {
            GOOGLE_LOG(ERROR) << "fail to connect to server " << serverAddr_ << ":" << serverPort_;
            connectTried_ = true;
            startConnect();
        }
    }
コード例 #11
0
ファイル: ShmRpcServer.cpp プロジェクト: adanselm/pbRPCpp
    void ShmRpcServer::messageReceived(const string& msg)
    {
      try {
        GOOGLE_LOG(INFO) << "a message is read from client with " << msg.length() << " bytes";
        size_t pos = 0;

        char ch = Util::readChar(msg, pos);
        int n = Util::readInt(msg, pos);

        if (ch != 'R' || n + pos != msg.length()) {
          GOOGLE_LOG(ERROR) << "invalid message received from client";
        } else {
          BaseRpcServer::messageReceived(1, msg.substr(pos));
        }
      } catch (const std::exception& ex) {
        GOOGLE_LOG(ERROR) << "caught exception:" << ex.what();
      } catch (...) {
        GOOGLE_LOG(ERROR) << "caught unknown exception";
      }
    }
コード例 #12
0
ファイル: UdpRpcChannel.cpp プロジェクト: adanselm/pbRPCpp
    void UdpRpcChannel::startRead() {

        if( stop_ ) {
            return;
        }
        
        GOOGLE_LOG( INFO ) << "start to read from server";
        
        socket_.async_receive_from(boost::asio::buffer(msgBuffer_, sizeof ( msgBuffer_)), senderEndpoint_, boost::bind(&UdpRpcChannel::packetReceived, this, _1, _2));

    }
コード例 #13
0
ファイル: ShmRpcServer.cpp プロジェクト: adanselm/pbRPCpp
    void ShmRpcServer::sendResponse(int /*clientId*/, const string& msg)
    {
      if (stop_) {
        GOOGLE_LOG(INFO) << "server is stopped, no message will be sent to client";
        return;
      }

      if( ! outQueue_->isConnected() )
      {
        GOOGLE_LOG(FATAL) << "Output segment is not connected anymore";
        return;
      }

      shared_ptr<string> s( new string(RpcMessage::serializeNetPacket(msg)) );
      GOOGLE_LOG(INFO) << "start to send " << s->length() << " bytes back to client";

      if( outQueue_->sendMessage(*s) )
        GOOGLE_LOG(INFO) << "success to send response message back to client.";
      else
        GOOGLE_LOG(ERROR) << "fail to send response message back to client";

    }
コード例 #14
0
ファイル: TcpRpcChannel.cpp プロジェクト: adanselm/pbRPCpp
    void TcpRpcChannel::sendMessage(const string& msg, boost::function< void (bool, const string&) > resultCb) {
        if (stop_) {
            return;
        }

        shared_ptr<string> s( new string(RpcMessage::serializeNetPacket(msg)) );

        GOOGLE_LOG(INFO) << "start to send a message to server with " << s->length() << " bytes";
        boost::asio::async_write(sock_,
                boost::asio::buffer(s->data(), s->length()),
                boost::asio::transfer_all(),
                boost::bind(&TcpRpcChannel::handleDataWrite, this, _1, _2, s, resultCb));

    }
コード例 #15
0
ファイル: UdpRpcChannel.cpp プロジェクト: adanselm/pbRPCpp
    void UdpRpcChannel::sendMessage(const string& msg, boost::function< void (bool, const string&) > resultCb) {
        if( stop_ ) {
            return;
        }
                
        shared_ptr<string> s( new string( RpcMessage::serializeNetPacket( msg ) ) );

        GOOGLE_LOG( INFO ) << "start to send message to server with " << s->length() << " bytes";
        
        socket_.async_send_to(boost::asio::buffer(s->data(), s->length()),
                remoteEndpoint_,
                boost::bind(&UdpRpcChannel::handlePacketWrite, this, _1, _2, s, resultCb));

    }
コード例 #16
0
PojoGeneratorHelper::JavaType PojoGeneratorHelper::GetJavaType(const FieldDescriptor* field) {
	switch (field->type()) {
		case FieldDescriptor::TYPE_INT32:
		case FieldDescriptor::TYPE_UINT32:
		case FieldDescriptor::TYPE_SINT32:
		case FieldDescriptor::TYPE_FIXED32:
		case FieldDescriptor::TYPE_SFIXED32:
			return JAVATYPE_INT;

		case FieldDescriptor::TYPE_INT64:
		case FieldDescriptor::TYPE_UINT64:
		case FieldDescriptor::TYPE_SINT64:
		case FieldDescriptor::TYPE_FIXED64:
		case FieldDescriptor::TYPE_SFIXED64:
			return JAVATYPE_LONG;

		case FieldDescriptor::TYPE_FLOAT:
			return JAVATYPE_FLOAT;

		case FieldDescriptor::TYPE_DOUBLE:
			return JAVATYPE_DOUBLE;

		case FieldDescriptor::TYPE_BOOL:
			return JAVATYPE_BOOLEAN;

		case FieldDescriptor::TYPE_STRING:
			return JAVATYPE_STRING;

		case FieldDescriptor::TYPE_BYTES:
			return JAVATYPE_BYTES;

		case FieldDescriptor::TYPE_ENUM:
			return JAVATYPE_ENUM;

		case FieldDescriptor::TYPE_GROUP:
		case FieldDescriptor::TYPE_MESSAGE:
			return JAVATYPE_MESSAGE;

			// No default because we want the compiler to complain if any new
			// types are added.
	}

	GOOGLE_LOG(FATAL) << "Can't get here.";
	return JAVATYPE_INT;
}
コード例 #17
0
const char* PojoGeneratorHelper::PrimitiveTypeName(JavaType type) {
	switch (type) {
		case JAVATYPE_INT    : return "int";
		case JAVATYPE_LONG   : return "long";
		case JAVATYPE_FLOAT  : return "float";
		case JAVATYPE_DOUBLE : return "double";
		case JAVATYPE_BOOLEAN: return "boolean";
		case JAVATYPE_STRING : return "java.lang.String";
		case JAVATYPE_BYTES  : return "com.google.protobuf.ByteString";
		case JAVATYPE_ENUM   : return NULL;
		case JAVATYPE_MESSAGE: return NULL;

							   // No default because we want the compiler to complain if any new
							   // JavaTypes are added.
	}

	GOOGLE_LOG(FATAL) << "Can't get here.";
	return NULL;
}
コード例 #18
0
ファイル: gzip_stream.cpp プロジェクト: JamShan/Phoenix3D_2.1
// implements ZeroCopyOutputStream ---------------------------------
bool GzipOutputStream::Next(void** data, int* size) {
  if ((zerror_ != Z_OK) && (zerror_ != Z_BUF_ERROR)) {
    return false;
  }
  if (zcontext_.avail_in != 0) {
    zerror_ = Deflate(Z_NO_FLUSH);
    if (zerror_ != Z_OK) {
      return false;
    }
  }
  if (zcontext_.avail_in == 0) {
    // all input was consumed. reset the buffer.
    zcontext_.next_in = static_cast<Bytef*>(input_buffer_);
    zcontext_.avail_in = input_buffer_length_;
    *data = input_buffer_;
    *size = input_buffer_length_;
  } else {
    // The loop in Deflate should consume all avail_in
    GOOGLE_LOG(DFATAL) << "Deflate left bytes unconsumed";
  }
  return true;
}
コード例 #19
0
ファイル: TcpRpcChannel.cpp プロジェクト: adanselm/pbRPCpp
    void TcpRpcChannel::startConnect() {
        //if already stopped
        if (stop_) {
            return;
        }


        boost::system::error_code ec;
        //close the socket at first 
        sock_.close(ec);

        //start to resolve the address
        tcp::resolver resolver(io_service_initializer_->get_io_service());
        tcp::resolver::query query(serverAddr_, serverPort_);
        tcp::resolver::iterator iter = resolver.resolve(query, ec);

        if (ec) {
            GOOGLE_LOG(ERROR) << "fail a resolve server address " << serverAddr_ << ":" << serverPort_;
        } else {
            receivedMsg_.clear();
            doConnect(iter);
        }
    }
コード例 #20
0
ファイル: UdpRpcChannel.cpp プロジェクト: adanselm/pbRPCpp
    void UdpRpcChannel::packetReceived(const boost::system::error_code& ec, std::size_t bytes_transferred) {
        if (stop_) {
            return;
        }

        if (senderEndpoint_ != remoteEndpoint_) {
            GOOGLE_LOG(ERROR) << "received a packet not from server, discard it";
            return;
        }
        string s(msgBuffer_, bytes_transferred);

        try {
            string resp_msg;
            
            while( RpcMessage::extractNetPacket( s, resp_msg ) ) {
                responseReceived( resp_msg );
            }
        } catch (...) {

        }
        startRead();

    }
コード例 #21
0
 RemoteCopyingFile::~RemoteCopyingFile() {
     if (!Close()) {
       GOOGLE_LOG(ERROR) << "close() failed: " << strerror(_errno);
     }
 }