43 dgram_over_stream::dgram_over_stream(stream_client_socket socket)
44 : inner(std::unique_ptr<stream_client_socket>(
45 new stream_client_socket(std::move(socket)))) {
49 dgram_over_stream::dgram_over_stream(
50 std::unique_ptr<stream_client_socket> inner_)
51 : inner(std::move(inner_)) {
67 int enabled_ = int(!enabled);
68 inner->set_sock_opt(IPPROTO_TCP, TCP_NODELAY, (
const char*)&enabled_,
73 return sndmsg(msg.c_str(), msg.size());
84 if (expected <= dst->size()) dst->resize(expected);
86 size_t to_receive = dst->size();
89 while (received < to_receive) {
90 ssize_t result = receive_bytes(to_receive - received);
95 "dgram_over_stream::rcvmsg(): Could not receive message!",
98 dst->replace(received, result, RECV_BUF);
100 received += (size_t)result;
104 ssize_t rest = expected - to_receive;
106 rest -= receive_bytes(rest);
118 return sndmsg(static_cast<const void*>(msg.data()), msg.size());
132 if (expected <= dst->size()) dst->resize(expected);
134 size_t to_receive = dst->size();
136 std::vector<uint8_t>::iterator dst_iter = dst->begin();
138 while (received < to_receive) {
139 ssize_t result = receive_bytes(to_receive - received);
144 "dgram_over_stream::rcvmsg(): Could not receive message!",
147 for (ssize_t i = 0; i < result; i++, dst_iter++)
148 *dst_iter = RECV_BUF[i];
154 ssize_t rest = expected - to_receive;
156 rest -= receive_bytes(rest);
168 encode_uint32(uint32_t(len), prefix_buffer);
169 ssize_t result = inner->snd(prefix_buffer, FRAMING_PREFIX_LENGTH, 0);
171 if (result < 0)
return result;
173 result = inner->snd(buf, len, 0);
175 if (result < 0)
return result;
190 size_t to_receive = len < expected ? len : expected;
193 while (received < to_receive) {
194 ssize_t result = receive_bytes(to_receive - received);
199 "dgram_over_stream::rcvmsg(): Could not receive message!",
202 memcpy(dst, RECV_BUF, result);
203 dst = static_cast<void*>(static_cast<char*>(dst) + result);
208 ssize_t rest = expected - to_receive;
210 rest -= receive_bytes(rest);
216 ssize_t dgram_over_stream::receive_bytes(
size_t n) {
217 if (n == 0)
return 0;
220 ssize_t rest_len = n > RECV_BUF_SIZE ? RECV_BUF_SIZE : n;
223 while (rest_len > 0) {
224 ssize_t recvd = inner->rcv(RECV_BUF + pos, rest_len, 0);
226 if (recvd <= 0)
return n - rest_len;
245 inner->rcv(prefix_buffer + pos, FRAMING_PREFIX_LENGTH, 0);
249 "dgram_over_stream::receive_header(): Could " 250 "not receive length prefix!",
256 return decode_uint32(prefix_buffer);
Contains libsocket elements.
void enable_nagle(bool enable) const
Set TCP_NODELAY to !enabled on the underlying socket.
uint32_t receive_header(void)
Receive and decode length header.
ssize_t rcvmsg(void *dst, size_t len)
Receive a message and store the first len bytes into buf.
This class is instantiated and thrown when an error occurs. If there's an error somewhere in libsocke...
ssize_t sndmsg(const void *buf, size_t len)
Send the message in buf with length len as one frame.