Java 类com.google.protobuf.GeneratedMessageLite 实例源码

项目:sctalk    文件:IMSocketManager.java   
/**
 * todo check exception
 * */
public void sendRequest(GeneratedMessageLite requset,int sid,int cid,Packetlistener packetlistener){
    int seqNo = 0;
    try{
        //组装包头 header
        com.mogujie.tt.protobuf.base.Header header = new DefaultHeader(sid, cid);
        int bodySize = requset.getSerializedSize();
        header.setLength(SysConstant.PROTOCOL_HEADER_LENGTH + bodySize);
        seqNo = header.getSeqnum();
        listenerQueue.push(seqNo,packetlistener);
        boolean sendRes = msgServerThread.sendRequest(requset,header);
    }catch (Exception e){
        if(packetlistener !=null){
            packetlistener.onFaild();
        }
        listenerQueue.pop(seqNo);
        logger.e("#sendRequest#channel is close!");
    }
}
项目:sctalk    文件:SocketThread.java   
/**
   * @param requset
   * @param header
   * @return
   */
  public boolean sendRequest(GeneratedMessageLite requset,Header header){
      int bodySize = requset.getSerializedSize();

if (null != channel) {
    byte[] bytes = requset.toByteArray();
    byte[] hbytes = header.encode().array();

    ByteBuf byteBuf = channel.alloc().buffer(SysConstant.PROTOCOL_HEADER_LENGTH + bodySize);

    byteBuf.writeBytes(hbytes);
    byteBuf.writeBytes(bytes);

          /**底层的状态要提前判断,netty抛出的异常上层catch不到*/
          boolean isW = channel.isWritable();
          boolean isC  = channel.isOpen();
          if(!(isW && isC)){
              throw  new RuntimeException("#sendRequest#channel is close!");
          }
    //channel.writeAndFlush(buffer.getOrignalBuffer());
    channel.writeAndFlush(byteBuf);
          logger.d("packet#send ok");
          return true;
      } else {
          logger.e("packet#send failed");
          return false;
      }
  }
项目:osm-lib    文件:PBFOutput.java   
/** @param block is either a PrimitiveBlock or a HeaderBlock */
private void writeOneBlob(GeneratedMessageLite block) {

    // FIXME lotsa big copies going on here

    String blobTypeString;
    if (block instanceof Osmformat.HeaderBlock) {
        blobTypeString = "OSMHeader";
    } else if (block instanceof Osmformat.PrimitiveBlock) {
        blobTypeString = "OSMData";
    } else {
        throw new AssertionError("block must be either a header block or a primitive block.");
    }

    Fileformat.Blob.Builder blobBuilder = Fileformat.Blob.newBuilder();
    byte[] serializedBlock = block.toByteArray();
    byte[] deflatedBlock = new byte[serializedBlock.length];
    int deflatedSize = deflate(serializedBlock, deflatedBlock);
    if (deflatedSize < 0) {
        LOG.debug("Deflate did not reduce the size of a block. Saving it uncompressed.");
        blobBuilder.setRaw(ByteString.copyFrom(serializedBlock));
    } else {
        blobBuilder.setZlibData(ByteString.copyFrom(deflatedBlock, 0, deflatedSize));
        blobBuilder.setRawSize(serializedBlock.length);
    }
    byte[] serializedBlob = blobBuilder.build().toByteArray();

    Fileformat.BlobHeader blobHeader = Fileformat.BlobHeader.newBuilder()
            .setType(blobTypeString).setDatasize(serializedBlob.length).build();
    byte[] serializedBlobHeader = blobHeader.toByteArray();
    try {
        // "Returns a big-endian representation of value in a 4-element byte array"
        downstream.write(Ints.toByteArray(serializedBlobHeader.length));
        downstream.write(serializedBlobHeader);
        downstream.write(serializedBlob);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

}
项目:sctalk    文件:IMSocketManager.java   
/**-------------------------------功能方法--------------------------------------*/

    public void sendRequest(GeneratedMessageLite requset,int sid,int cid){
        sendRequest(requset,sid,cid,null);
    }