libsocket
dgramoverstream.hpp
Go to the documentation of this file.
1 #ifndef LIBSOCKET_DGRAMOVERSTREAM_H_7854202d13e741e98bb3b084eb3d6bc0
2 #define LIBSOCKET_DGRAMOVERSTREAM_H_7854202d13e741e98bb3b084eb3d6bc0
3 
4 #include "exception.hpp"
5 #include "framing.hpp"
6 #include "socket.hpp"
7 #include "streamclient.hpp"
8 
9 #include <memory>
10 #include <string>
11 #include <vector>
12 
13 #include <netinet/ip.h>
14 #include <netinet/tcp.h>
15 
23 /*
24  The committers of the libsocket project, all rights reserved
25  (c) 2016, dermesser <lbo@spheniscida.de>
26 
27  Redistribution and use in source and binary forms, with or without
28  modification, are permitted provided that the following conditions are met:
29 
30  1. Redistributions of source code must retain the above copyright notice,
31  this list of conditions and the following disclaimer.
32  2. Redistributions in binary form must reproduce the above copyright notice,
33  this list of conditions and the following disclaimer in the documentation
34  and/or other materials provided with the distribution.
35 
36  THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS “AS IS” AND ANY
37  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
38  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
40  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
41  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
45  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46 
47 */
48 
49 namespace libsocket {
81  public:
82  dgram_over_stream(void) = delete;
83  dgram_over_stream(const dgram_over_stream&) = delete;
85  dgram_over_stream(std::unique_ptr<stream_client_socket> inner);
86 
87  void enable_nagle(bool enable) const;
88 
89  ssize_t sndmsg(const void* buf, size_t len);
90  ssize_t rcvmsg(void* dst, size_t len);
91 
92  ssize_t sndmsg(const std::string& msg);
93  ssize_t rcvmsg(std::string* dst);
94 
95  ssize_t sndmsg(const std::vector<uint8_t>& msg);
96  ssize_t rcvmsg(std::vector<uint8_t>* dst);
97 
98  private:
99  static const size_t RECV_BUF_SIZE = 256;
100 
101  // The underlying stream.
102  std::unique_ptr<stream_client_socket> inner;
103  char prefix_buffer[FRAMING_PREFIX_LENGTH];
104  char RECV_BUF[RECV_BUF_SIZE];
105 
106  ssize_t receive_bytes(size_t);
107  uint32_t receive_header(void);
108 };
109 } // namespace libsocket
110 
111 #endif
Contains libsocket elements.
Definition: dgramclient.hpp:41
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.
Declarations of framing functions.
Wraps a stream socket and provides a message-based API on top of it.
Generic socket for active stream sockets (TCP client sockets, UNIX Stream clients)
ssize_t sndmsg(const void *buf, size_t len)
Send the message in buf with length len as one frame.