Exemplo n.º 1
0
/**
 * 创建一easy_hash_t
 */
easy_hash_t *easy_hash_create(easy_pool_t *pool, uint32_t size, int offset)
{
    easy_hash_t         *table;
    easy_hash_list_t    **buckets;
    uint32_t            n;

    // 2 ^ m
    n = 4;
    size &= 0x7fffffff;

    while(size > n) n <<= 1;

    // alloc
    buckets = (easy_hash_list_t **)easy_pool_calloc(pool, n * sizeof(easy_hash_list_t *));
    table = (easy_hash_t *)easy_pool_alloc(pool, sizeof(easy_hash_t));

    if (table == NULL || buckets == NULL)
        return NULL;

    table->buckets = buckets;
    table->size = n;
    table->mask = n - 1;
    table->count = 0;
    table->offset = offset;
    table->seqno = 1;
    easy_list_init(&table->list);

    return table;
}
Exemplo n.º 2
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;
    }
Exemplo n.º 3
0
void* CMysqlCallback::decode(easy_message_t* m) {
	cout<<"在CMysqlCallback的decode函数中!"<<endl;
//	int ret;
//	CMysqlSPRPacket packet;
//	char *buffer=(char *)malloc(2*1024*1024);
//	ObDataBuffer out_buffer(buffer,2*1024*1024);
//	ret=packet.serialize(out_buffer.get_data(),out_buffer.get_capacity(),out_buffer.get_position());
////	ret=write_data(m->fd,out_buffer.get_data(),out_buffer.get_position());
//	return &packet;

    uint32_t pkt_len = 0;
    uint8_t pkt_seq = 0;
    uint8_t pkt_type = 0;
    CMysqlCommandPacket* packet = 0;
    char* buffer = 0;
    int32_t len = 0;

    len = static_cast<int32_t>(m->input->last - m->input->pos);

    CMysqlUtil::get_uint3(m->input->pos, pkt_len);
    CMysqlUtil::get_uint1(m->input->pos, pkt_seq);
    cout<<"SQL: "<<m->input->pos<<endl;

//    message has enough buffer
    if (pkt_len <= m->input->last - m->input->pos)
    {
      CMysqlUtil::get_uint1(m->input->pos, pkt_type);
//      利用message带的pool进行应用层内存的分配
      buffer = reinterpret_cast<char*>(easy_pool_alloc(m->pool,static_cast<uint32_t>(sizeof(CMysqlCommandPacket) + pkt_len)));
      packet = new(buffer)CMysqlCommandPacket();
      packet->set_header(pkt_len, pkt_seq);
      cout<<"pkt_seq: "<<pkt_seq<<endl;
      packet->set_type(pkt_type);
      packet->set_receive_ts(0);
      memcpy(buffer + sizeof(CMysqlCommandPacket), m->input->pos, pkt_len - 1);
      m->input->pos += pkt_len-1;

//      packet->get_command().assign(buffer + sizeof(ObMySQLCommandPacket), pkt_len - 1);
//        if (PACKET_RECORDER_FLAG)
//        {
//          // record the packet to FIFO stream if required
//          ObMySQLServer* server = reinterpret_cast<ObMySQLServer*>(m->c->handler->user_data);
//          ObMySQLCommandPacketRecord record;
//          record.socket_fd_ = m->c->fd;
//          record.cseq_ = m->c->seq;
//          record.addr_ = m->c->addr;
//          record.pkt_length_ = pkt_len;
//          record.pkt_seq_ = pkt_seq;
//          record.cmd_type_ = pkt_type;
//          struct iovec buffers[2];
//          buffers[0].iov_base = &record;
//          buffers[0].iov_len = sizeof(record);
//          buffers[1].iov_base = m->input->pos;
//          buffers[1].iov_len = pkt_len - 1;
//          int err = OB_SUCCESS;
//          if (OB_SUCCESS != (err = server->get_packet_recorder().push(buffers, 2)))
//          {
//            TBSYS_LOG(WARN, "failed to record MySQL packet, err=%d", err);
//          }
//        }
//        m->input->pos += pkt_len - 1;
    }
//    else
//    {
//      m->next_read_len = static_cast<int>(pkt_len - (m->input->last - m->input->pos));
//      TBSYS_LOG(DEBUG, "not enough data in message, packet length = %u, data in message is %ld",
//                pkt_len, m->input->last - m->input->pos);
//      m->input->pos -= OB_MYSQL_PACKET_HEADER_SIZE;
//    }
    return packet;
}