bool SocketWire::Base::read_from_socket()

in rd-cpp/src/rd_framework_cpp/src/main/wire/SocketWire.cpp [192:246]


bool SocketWire::Base::read_from_socket(Buffer::word_t* res, int32_t msglen) const
{
	int32_t ptr = 0;
	while (ptr < msglen)
	{
		RD_ASSERT_MSG(hi >= lo, "hi >= lo")

		int32_t rest = msglen - ptr;
		int32_t available = static_cast<int32_t>(hi - lo);

		if (available > 0)
		{
			int32_t copylen = (std::min)(rest, available);
			std::copy(lo, lo + copylen, res + ptr);
			lo += copylen;
			ptr += copylen;
		}
		else
		{
			if (hi == receiver_buffer.end())
			{
				hi = lo = receiver_buffer.begin();
			}
			logger->trace("{}: receive started", this->id);
			int32_t read = socket_provider->Receive(static_cast<int32_t>(receiver_buffer.end() - hi), &*hi);
			if (read == -1)
			{
				auto err = socket_provider->GetSocketError();
				if (err == CSimpleSocket::SocketInvalidSocket)
				{
					logger->info("{}: socket was shut down for receiving", this->id);
					return false;
				}
				logger->error("{}: error has occurred while receiving", this->id);
				return false;
			}
			if (read == 0)
			{
				logger->info("{}: socket was shut down for receiving", this->id);
				return false;
			}
			hi += read;
			if (read > 0)
			{
				logger->trace("{}: receive finished: {} bytes read", this->id, read);
			}
		}
	}
	if (ptr != msglen)
	{
		logger->error("read invalid number of bytes from socket, expected: {}, actual: {}", msglen, ptr);
		assert(false);
	}
	return true;
}