Пример #1
0
int stream_socket::readv(mutable_buffer const &b)
{
	static const unsigned max_vec_size = 16;
	mutable_buffer::buffer_data_type data = b.get();
	unsigned size=0;
#ifndef BOOSTER_WIN32
	struct iovec vec[max_vec_size];
	for(;size < max_vec_size && size < data.second;size++) {
		vec[size].iov_base = data.first[size].ptr;
		vec[size].iov_len = data.first[size].size;
	}
	for(;;) {
		int ret = ::readv(native(),vec,size);
		if(ret >= 0)
			return ret;
		if(ret < 0 && errno==EINTR)
			continue;
		return ret;
	}
#else // Win32
	WSABUF vec[max_vec_size];
	for(;size < max_vec_size && size < data.second;size++) {
		vec[size].buf = data.first[size].ptr;
		vec[size].len = data.first[size].size;
	}
	DWORD recved=0;
	DWORD flags=0;
	int res = ::WSARecv(native(),vec,size,&recved,&flags,0,0);
	if(res == 0)
		return recved;
	return -1;
#endif
}
Пример #2
0
        int
        marshall(const Msg& inm, mutable_buffer<char> inb)
        {
            int newlen=inb.len()-HEAD_SIZE;
            if(newlen<=0)
                throw std::runtime_error("marshalling:: provided buffer is too small");

            auto it = m_map.find(inm.type());
            if(it==m_map.end())
                throw std::runtime_error("marshalling:: message type not supported");

            *(reinterpret_cast<int*>(inb.addr())) = htonl(inm.type());

            int m_len = it->second->marshall(inm,mutable_buffer<char>(inb.addr()+HEAD_SIZE,newlen));

            *(reinterpret_cast<unsigned int*>(inb.addr()+sizeof(int))) = htonl(m_len);
            return m_len+HEAD_SIZE;
        }
Пример #3
0
void NetworkManager::Interface::ReceivePacket(mutable_buffer buf,
                                              uint32_t len) {
  //FIXME: We should avoid this copy
  auto p = pbuf_alloc(PBUF_LINK, len + ETH_PAD_SIZE, PBUF_POOL);

  kbugon(p == nullptr, "Failed to allocate pbuf");

  auto ptr = buf.addr();
  bool first = true;
  for (auto q = p; q != nullptr; q = q->next) {
    auto add = 0;
    if (first) {
      add = ETH_PAD_SIZE;
      first = false;
    }
    memcpy(static_cast<char*>(q->payload) + add, ptr, q->len - add);
    ptr = static_cast<void*>(static_cast<char*>(ptr) + q->len);
  }

  netif_.input(p, &netif_);
}
Пример #4
0
 const_buffer(const mutable_buffer<Tp> &other)
 : m_addr(other.addr()), m_len(other.len())
 {}