Example #1
0
ssize_t proto_h16::calc_size(MessageBlock& mb) const
{
	uint32_t sz;
	if (mb.size() < sizeof(sz)) {
		size_t left = sizeof(sz) - mb.size();
		memcpy(&sz, mb.rptr(), mb.size());

		MessageBlock* cont = mb.cont();
		if (cont != NULL && cont->size() >= left) {
			memcpy((byte *)&sz + mb.size(), cont->rptr(), left);
		}
		else {
			return 0;
		}
	}
	else {
		memcpy(&sz, mb.rptr(), sizeof(sz));
	}

	if (sz >= head_size() && sz >= min_ && sz <= max_)
		return sz;

	errno = EINVAL;
	return -1;
}
Example #2
0
int Connection::split_block(size_t msize)
{
	//MessageRequest* sub;
	int retval = -1;
#if 1
	assert(0);
#else
	// TODO: need it?
	if (mreq_->size() > msize) {
		sub = mreq_->extract(msize);		
	}
	else {
		sub = mreq_;
		MessageBlock* mb = sub->cont();
		size_t left = msize - mreq_->size();
		while (left > 0) {
			if (left < mb->size()) {
				mreq_ = new MessageRequest(mb->dblock_);
				break;
			}
			else if (left == mb->size()) {
				mreq_ = new MessageRequest(mb->cont());
				mb->cont(NULL);
			}
			else {
				left -= mb->size();
				continue;
			}
		}
	}
	

	if (sub != NULL) {
		if ((retval = sched_->on_message(sub)) < 0) {
			SYSLOG_ERROR("%s drop sub message(len=%d, data=%s)",
				name(), (unsigned long)sub->tsize(),
				beyondy::toString((unsigned char *)sub->rptr(), std::min(16, sub->tsize())).c_str(),
				errno);
		}
	}
	else {
		retval = -1;
		SYSLOG_ERROR("%s extract sub message failed: %d", name(), errno);
	}
#endif
	return retval;
}