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; }
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)); }
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))); }
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)); }
carray( boost::array<T,N> & orig) : m_t( orig.c_array() ), m_element_count( N ) { if (m_element_count == 0) m_t = 0; }
data_type operator()() { return data_type(_data.c_array(), size); }
size_t pop(OutputIterator it) { return ringbuffer_base<T>::pop(it, array_.c_array(), max_size_); }
size_t pop(T * ret, size_t size) { return ringbuffer_base<T>::pop(ret, size, array_.c_array(), max_size_); }
ConstIterator push(ConstIterator begin, ConstIterator end) { return ringbuffer_base<T>::push(begin, end, array_.c_array(), max_size_); }
size_t push(T const * t, size_t size) { return ringbuffer_base<T>::push(t, size, array_.c_array(), max_size_); }
bool pop(T & ret) { return ringbuffer_base<T>::pop(ret, array_.c_array(), max_size_); }
bool push(T const & t) { return ringbuffer_base<T>::push(t, array_.c_array(), max_size_); }
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))); }