Пример #1
0
static inline rpacket_t rpk_create_by_wpacket(struct wpacket *w)
{
	rpacket_t r = (rpacket_t)ALLOC(rpacket_allocator,sizeof(*r));
	r->binbuf = NULL;
	r->binbufpos = 0;
    PACKET_BUF(r) = buffer_acquire(NULL,PACKET_BUF(w));
    r->readbuf = buffer_acquire(NULL,PACKET_BUF(w));
    PACKET_BEGINPOS(r) = PACKET_BEGINPOS(w);
    MSG_NEXT(r) = NULL;
    MSG_TYPE(r) = MSG_RPACKET;
    PACKET_RAW(r) = PACKET_RAW(w);
    if(PACKET_RAW(r))
	{
		r->len = w->data_size;
        r->rpos =PACKET_BEGINPOS(w);
	}
	else
	{
		//这里的len只记录构造时wpacket的len,之后wpacket的写入不会影响到rpacket的len
		r->len = w->data_size - sizeof(r->len);
        r->rpos = (PACKET_BEGINPOS(r) + sizeof(r->len))%PACKET_BUF(r)->capacity;
        if(r->rpos < PACKET_BEGINPOS(r))//base->begin_pos)
			r->readbuf = buffer_acquire(r->readbuf,r->readbuf->next);
	}
	r->data_remain = r->len;
	return r;
}
Пример #2
0
rpacket_t rpk_create_skip(rpacket_t other,uint32_t skipsize)
{
    if(!other || PACKET_RAW(other) || other->len <= skipsize)
        return NULL;
    rpacket_t r = (rpacket_t)ALLOC(rpacket_allocator,sizeof(*r));
    PACKET_RAW(r) = 0;
    //首先要到正确的读位置
    uint32_t move_size = skipsize+sizeof(r->len);
    buffer_t buf = PACKET_BUF(other);
    uint32_t pos = PACKET_BEGINPOS(other);
    while(move_size){
        uint32_t s = buf->size - pos;
        if(s > move_size) s = move_size;
        move_size -= s;
        pos += s;
        if(pos >= buf->size){
            buf = buf->next;
            pos = 0;
        }
    }

    r->binbuf = NULL;
    r->binbufpos = 0;
    PACKET_BUF(r) = buffer_acquire(NULL,buf);
    r->readbuf = buffer_acquire(NULL,buf);
    r->len = other->len-skipsize;
    r->data_remain = r->len;
    r->rpos = PACKET_BEGINPOS(r) = pos;
    MSG_NEXT(r) = NULL;
    MSG_TYPE(r) = MSG_RPACKET;
    return r;
}
Пример #3
0
rpacket_t rpk_create(buffer_t b,
                     uint32_t pos,
                     uint32_t pk_len,
                     uint8_t is_raw)
{
	rpacket_t r = (rpacket_t)ALLOC(rpacket_allocator,sizeof(*r));
	struct packet *base = (struct packet*)r;
	r->binbuf = NULL;
	r->binbufpos = 0;
    PACKET_BUF(r) = buffer_acquire(NULL,b);
	r->readbuf = buffer_acquire(NULL,b);
	r->len = pk_len;
	r->data_remain = r->len;
    PACKET_BEGINPOS(r) = pos;
    MSG_NEXT(r) = NULL;
    MSG_TYPE(r) = MSG_RPACKET;
	if(is_raw)
		r->rpos = pos;
	else
		r->rpos = (pos + sizeof(r->len))%base->buf->capacity;
	if(r->rpos < base->begin_pos)
		r->readbuf = buffer_acquire(r->readbuf,r->readbuf->next);

	base->raw = is_raw;
	return r;
}
Пример #4
0
int find_master_input(zloop_t *loop, zmq_pollitem_t *item, void *arg) {
    maneater_client *cli = (maneater_client *)arg;
    zframe_t *incoming = zframe_recv(cli->local_socket);
    unsigned char *data = zframe_data(incoming);
    int size = zframe_size(incoming);
    msgpack_unpacked msg;
    size_t off;
    MSG_NEXT(&msg, data, size, &off);
    assert(msg.data.type == MSGPACK_OBJECT_POSITIVE_INTEGER);
    uint64_t msgid = msg.data.via.u64;

    int ret = 0, i = 0;

    if (msgid == MID_IS_MASTER) {
        MSG_NEXT(&msg, data, size, &off);
        assert(msg.data.type == MSGPACK_OBJECT_RAW);
        char *host;
        COPY_STRING(host, msg.data.via.raw.ptr);

        cli->master = host;

        cli->master_socket = NULL;
        for (i=0; i < cli->num_hosts; i++) {
            if (!strcmp(cli->hosts[i], host)) {
                cli->master_socket = cli->sockets[i];
                break;
            }
        }

        assert(cli->master_socket);
        struct timeval tim;
        gettimeofday(&tim, NULL);
        cli->last_tick = tim.tv_sec;

        zclock_log("got master: %s", host);

        ret = -1;
    }

    zframe_destroy(&incoming);
    return ret;
}
Пример #5
0
static inline rpacket_t rpk_create_by_rpacket(rpacket_t other)
{
	rpacket_t r = (rpacket_t)ALLOC(rpacket_allocator,sizeof(*r));
    r->binbuf = NULL;
	r->binbufpos = 0;
    PACKET_BUF(r) = buffer_acquire(NULL,PACKET_BUF(other));
	r->readbuf = buffer_acquire(NULL,other->readbuf);
	r->len = other->len;
    r->data_remain = other->data_remain;
    PACKET_BEGINPOS(r) = PACKET_BEGINPOS(other);
    MSG_NEXT(r) = NULL;
    MSG_TYPE(r) = MSG_RPACKET;
    PACKET_RAW(r) = PACKET_RAW(other);

	r->rpos = other->rpos;
	return r;
}
Пример #6
0
//发送相关函数
static inline st_io *prepare_send(struct connection *c)
{
	int32_t i = 0;
    wpacket_t w = (wpacket_t)llist_head(&c->send_list);
	buffer_t b;
	uint32_t pos;
	st_io *O = NULL;
	uint32_t buffer_size = 0;
	uint32_t size = 0;
	uint32_t send_size_remain = MAX_SEND_SIZE;
	while(w && i < MAX_WBAF && send_size_remain > 0)
	{
		pos = w->base.begin_pos;
		b = w->base.buf;
		buffer_size = w->data_size;
		while(i < MAX_WBAF && b && buffer_size && send_size_remain > 0)
		{
			c->wsendbuf[i].iov_base = b->buf + pos;
			size = b->size - pos;
			size = size > buffer_size ? buffer_size:size;
			size = size > send_size_remain ? send_size_remain:size;
			buffer_size -= size;
			send_size_remain -= size;
			c->wsendbuf[i].iov_len = size;
			++i;
			b = b->next;
			pos = 0;
		}
        if(send_size_remain > 0) w = (wpacket_t)MSG_NEXT(w);//(wpacket_t)w->base.next.next;
	}
	if(i){
		c->send_overlap.m_super.iovec_count = i;
		c->send_overlap.m_super.iovec = c->wsendbuf;
		O = (st_io*)&c->send_overlap;
	}
	return O;

}