Пример #1
0
int CMysqlCallback::encode(easy_request_t* r, void* packet) {
	cout<<"在CMysqlCallback的encode函数中!"<<endl;
    int ret = 0;
    if (NULL == r || NULL == packet)
    {
      cout<<"error!"<<endl;
      ret = -1;
    }
    else
    {
      easy_buf_t* buf = reinterpret_cast<easy_buf_t*>(packet);
      easy_request_addbuf(r, buf);
      cout<<"加入了缓冲区队列!"<<endl;
    }
    return ret;
}
int tdhs_encode(easy_request_t *r, void *data) {
	tdhs_packet_t *packet;
	easy_buf_t *b;
	packet = (tdhs_packet_t *) data;
	tb_assert(packet!=NULL);

	while (packet) {
		tb_assert(packet->wbuff!=NULL);
		tb_assert((packet->command_id_or_response_code==CLIENT_STATUS_MULTI_STATUS)?     \
				packet->length==0:packet->length>0);
		b = packet->wbuff;
		//batch请求和返回的buf只包含header
		tb_assert((packet->command_id_or_response_code==CLIENT_STATUS_MULTI_STATUS||     \
				packet->command_id_or_response_code==REQUEST_TYPE_BATCH)?                \
				(uint32_t)(b->last-b->pos)==TDH_SOCKET_HEADER_LENGTH:                    \
				packet->length==((uint32_t)(b->last-b->pos)-TDH_SOCKET_HEADER_LENGTH));

		// 加入header
		WRITE_PACKET_HEADER_INFO(packet, b->pos)
		easy_request_addbuf(r, b);
		packet = packet->next;
	}
	return EASY_OK;
}
Пример #3
0
    int ObTbnetCallback::encode(easy_request_t *r, void *data)
    {
      int ret = EASY_OK;
      easy_buf_t* b = NULL;
      char* buff = NULL;
      ObPacket* packet = reinterpret_cast<ObPacket*>(data);
      int64_t header_size = 0;
      uint16_t ob_packet_header_size = 0;
#if !defined(_OB_VERSION) || _OB_VERSION<=300
      /* 0.3 ObPacket 头格式: 16 bytes */
      /*         2 bytes                 2 bytes       8 bytes     4 bytes
       *----------------------------------------------------------------------
       *| ob_packet_header_size_== 0 | api_version_ | session_id_ | timeout_ |
       *----------------------------------------------------------------------
       */
      ob_packet_header_size = 0;
      header_size = sizeof(uint16_t)/* ob_packet_header_size_ */ + sizeof(int16_t) /* api_version_ */+ sizeof(int64_t) /* session_id_ */+ sizeof(int32_t)/* timeout_ */;
#elif _OB_VERSION>300
      /* 0.4 ObPacket 头格式: 24 bytes */
      /*         2 bytes            2 bytes       8 bytes     4 bytes   8 bytes     8 bytes
       *--------------------------------------------------------------------------------------
       *| ob_packet_header_size_ | api_version_ | session_id_ | timeout_ |trace_id_|req_sign_|
       *--------------------------------------------------------------------------------------
       */
      ob_packet_header_size = (uint16_t)(sizeof(ob_packet_header_size) + sizeof(int16_t)/* api_version_ */ + sizeof(int64_t) /* session_id_ */ + sizeof(int32_t)/* timeout_ */ + sizeof(uint64_t)/* trace_id_ */ + sizeof(uint64_t) /* req_sign_ */);
      header_size = ob_packet_header_size;
#endif
      packet->set_ob_packet_header_size(ob_packet_header_size);
      int64_t size = packet->get_inner_buffer()->get_position() + OB_TBNET_HEADER_LENGTH + header_size;
      b = reinterpret_cast<easy_buf_t *>(easy_pool_alloc(r->ms->pool,
                                                          static_cast<uint32_t>(sizeof(easy_buf_t) + size)));
      if (NULL == b)
      {
        TBSYS_LOG(WARN, "alloc mem for send buffer failed buf=%p size is %lu", b,
                  sizeof(easy_buf_t) + size);
        ret = EASY_ERROR;
      }
      else
      {
        //skip sizeof(easy_buf_t) bytes
        buff = reinterpret_cast<char *>(b + 1);
        //b->pos = buff;
        //b->end = buff + size;
        //set packet length
        init_easy_buf(b, buff, r, size);
        packet->set_packet_len(static_cast<int>(
                                 packet->get_inner_buffer()->get_position() + header_size));

        if (false == packet->encode(buff, size))
        {
          ret = EASY_ERROR;
          TBSYS_LOG(WARN, "encode failed packet is %p, buff is %p, size is %ld", packet, buff, size);
        }

        if (EASY_OK == ret)
        {
          easy_buf_set_data(r->ms->pool, b, buff, static_cast<uint32_t>(size));
          easy_request_addbuf(r, b);
          //add rpc bytes out statistics
          OB_STAT_INC(COMMON, RPC_BYTES_OUT, size);
          TBSYS_LOG(DEBUG, "encode packet success packet code is %d, c is %s",packet->get_packet_code(),
                    easy_connection_str(r->ms->c));
        }
      }

      return ret;
    }