示例#1
0
boost::shared_ptr<NetPacket>
receiveMessage(tcp::socket &socket)
{
	boost::shared_ptr<NetPacket> tmpPacket;

	do {
		// This is necessary, because we use TCP.
		// Packets may be received in multiple chunks or
		// several packets may be received at once.
		if (recBufPos >= NET_HEADER_SIZE) {
			// Read the size of the packet (first 4 bytes in network byte order).
			uint32_t nativeVal;
			memcpy(&nativeVal, recBuf.c_array(), sizeof(uint32_t));
			size_t packetSize = ntohl(nativeVal);
			if (packetSize > MAX_PACKET_SIZE) {
				recBufPos = 0;
				cout << "Packet too large" << endl;
				return boost::shared_ptr<NetPacket>();
			} else if (recBufPos >= packetSize + NET_HEADER_SIZE) {
				try {
					tmpPacket = NetPacket::Create(&recBuf.c_array()[NET_HEADER_SIZE], packetSize);
					if (tmpPacket) {
						recBufPos -= (packetSize + NET_HEADER_SIZE);
						if (recBufPos) {
							memmove(recBuf.c_array(), recBuf.c_array() + packetSize + NET_HEADER_SIZE, recBufPos);
						}
					}
				} catch (const exception &) {
					// Reset buffer on error.
					recBufPos = 0;
					cout << "Packet creation failed" << endl;
					return boost::shared_ptr<NetPacket>();
				}
			}
		}

		if (!tmpPacket) {
			recBufPos += socket.receive(boost::asio::buffer(recBuf.c_array() + recBufPos, BUF_SIZE - recBufPos));
			if (recBufPos == 0) {
				cout << "Receive failed" << endl;
				return boost::shared_ptr<NetPacket>();
			}
		}
	} while (!tmpPacket);

	return tmpPacket;
}
示例#2
0
inline mutable_buffer_container_1 buffer(boost::array<Pod_Type, N>& data,
    std::size_t max_size_in_bytes)
{
  return mutable_buffer_container_1(
      mutable_buffer(data.c_array(),
        data.size() * sizeof(Pod_Type) < max_size_in_bytes
        ? data.size() * sizeof(Pod_Type) : max_size_in_bytes));
}
示例#3
0
inline typename detail::buffer_types<Pod_Type>::container_type
buffer(boost::array<Pod_Type, N>& data)
{
  typedef typename asio::detail::buffer_types<Pod_Type>::buffer_type
    buffer_type;
  typedef typename asio::detail::buffer_types<Pod_Type>::container_type
    container_type;
  return container_type(
      buffer_type(data.c_array(), data.size() * sizeof(Pod_Type)));
}
示例#4
0
inline typename detail::buffer_types<Pod_Type>::container_type
buffer(boost::array<Pod_Type, N>& data, std::size_t max_size_in_bytes)
{
  typedef typename asio::detail::buffer_types<Pod_Type>::buffer_type
    buffer_type;
  typedef typename asio::detail::buffer_types<Pod_Type>::container_type
    container_type;
  return container_type(
      buffer_type(data.c_array(),
        data.size() * sizeof(Pod_Type) < max_size_in_bytes
        ? data.size() * sizeof(Pod_Type) : max_size_in_bytes));
}
示例#5
0
 carray( boost::array<T,N> & orig)
 : m_t( orig.c_array() ), m_element_count( N ) {
     if (m_element_count == 0)
         m_t = 0;
 }
示例#6
0
 data_type operator()()
 {
   return data_type(_data.c_array(), size);
 }
示例#7
0
 size_t pop(OutputIterator it)
 {
     return ringbuffer_base<T>::pop(it, array_.c_array(), max_size_);
 }
示例#8
0
 size_t pop(T * ret, size_t size)
 {
     return ringbuffer_base<T>::pop(ret, size, array_.c_array(), max_size_);
 }
示例#9
0
 ConstIterator push(ConstIterator begin, ConstIterator end)
 {
     return ringbuffer_base<T>::push(begin, end, array_.c_array(), max_size_);
 }
示例#10
0
 size_t push(T const * t, size_t size)
 {
     return ringbuffer_base<T>::push(t, size, array_.c_array(), max_size_);
 }
示例#11
0
 bool pop(T & ret)
 {
     return ringbuffer_base<T>::pop(ret, array_.c_array(), max_size_);
 }
示例#12
0
 bool push(T const & t)
 {
     return ringbuffer_base<T>::push(t, array_.c_array(), max_size_);
 }
示例#13
0
inline mutable_buffer_container_1 buffer(boost::array<Pod_Type, N>& data)
{
  return mutable_buffer_container_1(
      mutable_buffer(data.c_array(), data.size() * sizeof(Pod_Type)));
}