Jpp 19.3.0-rc.3
the software that should make you happy
Loading...
Searching...
No Matches
JSocketChannel.hh
Go to the documentation of this file.
1#ifndef __JNET__JSOCKETCHANNEL__
2#define __JNET__JSOCKETCHANNEL__
3
4#include <vector>
5#include <string.h>
6
7#include "JLang/JException.hh"
8#include "JIO/JByteArrayIO.hh"
10
11
12/**
13 * \author mdejong
14 */
15
16namespace JNET {}
17namespace JPP { using namespace JNET; }
18
19namespace JNET {
20
24
25
26 /**
27 * Get size of data, including the header.
28 * This method should be implemented for each template class for read operations.
29 *
30 * \param prefix prefix
31 * \return number of bytes
32 */
33 template<class JPrefix_t>
34 int getSizeOfPacket(const JPrefix_t& prefix);
35
36
37 /**
38 * Set size of data, including the header.
39 * This method should be implemented for each template class for write operations.
40 *
41 * \param size number of bytes
42 * \param prefix prefix
43 */
44 template<class JPrefix_t>
45 void setSizeOfPacket(const int size, JPrefix_t& prefix);
46
47
48 /**
49 * Auxiliary class for socket channel.
50 */
52 public:
53
54 enum JType { IO_PREFIX = 1, IO_DATA = 2 };
55
56
57 /**
58 * Reset channel.
59 */
60 void reset()
61 {
63 }
64
65
66 protected:
68 };
69
70
71 /**
72 * Socket input channel.
73 *
74 * This class can be used to read a data packet from a socket without blocking and
75 * to buffer these data for subsequent user read operations.
76 *
77 * The template argument corresponds to a header that precedes any data.
78 * This header acts as a protocol specifier that is used to determine the size of the data packet.
79 * To this end, the method getSizeOfPacket() should be implemented for each header type.
80 *
81 * The reading from the socket may be non-blocking (depending on the configuration of the socket).
82 * The status of this object should be checked for completion of the data packet.
83 * The method reset() should be called before reading the next data packet.
84 */
85 template<class JPrefix_t>
88 public JSocketChannel,
89 public JByteArrayReader
90 {
91 public:
92
93
95
96
97 /**
98 * Constructor.
99 *
100 * \param socket socket
101 */
106 {
107 reset();
108 }
109
110
111 /**
112 * Interruptable read method.
113 *
114 * \return status
115 */
117 {
118 if (type == IO_PREFIX) {
119
120 if (isReset()) {
121 set((char*) &prefix, sizeof(JPrefix_t));
122 }
123
124 if (isBusy()) {
126 }
127
128 if (isReady()) {
129
130 buffer.clear();
131
133
134 memcpy(buffer.data(), &prefix, sizeof(JPrefix_t));
135
136 set(buffer.data() + sizeof(JPrefix_t),
137 buffer.size() - sizeof(JPrefix_t));
138
139 type = IO_DATA;
140 }
141 }
142
143 if (type == IO_DATA) {
144
145 if (isBusy()) {
147 }
148
149 if (isReady()) {
150 static_cast<JByteArrayReader&>(*this) = JByteArrayReader(buffer.data(), buffer.size());
151 }
152 }
153
155 }
156
157
158 /**
159 * Reset channel.
160 * After a reset, the socket channel is ready to receive a new data packet.
161 */
162 void reset()
163 {
165 JSocketChannel ::reset();
166
167 buffer.clear();
168 }
169
170
172
173 private:
175 };
176
177
178 /**
179 * Socket output channel.
180 *
181 * This class can be used to user write to an internal buffer, to format these data as
182 * a packet and to write this packet to the socket without blocking.
183 *
184 * The template argument corresponds to a header that precedes any data.
185 * This header acts as a protocol specifier that is used to determine the size of the data packet.
186 * To this end, the method setSizeOfPacket() should be implemented for each header type.
187 *
188 * The wroting to the socket may be non-blocking (depending on the configuration of the socket).
189 * The status of this object should be checked for completion of the data packet.
190 * The method reset() should be called before writing the next data packet.
191 */
192 template<class JPrefix_t>
195 public JSocketChannel,
196 public JByteArrayWriter
197 {
198 public:
199
201
202
203 /**
204 * Constructor.
205 *
206 * \param socket socket
207 */
212 {
213 reset();
214 }
215
216
217 /**
218 * Interruptable write method.
219 *
220 * \return status
221 */
223 {
224 if (type == IO_PREFIX) {
225
226 const int length = tellp();
227
228 setSizeOfPacket(length, prefix);
229
230 // overwrite prefix
231
232 seekp(0);
233
234 JByteArrayWriter::write((char*) &prefix, sizeof(JPrefix_t));
235
236 seekp(length);
237
238 type = IO_DATA;
239
240 set(data(), tellp());
241 }
242
243 if (isBusy()) {
245 }
246
248 }
249
250
251 /**
252 * Reset channel.
253 * After a reset, the socket channel is ready to receive a new data packet.
254 */
255 void reset()
256 {
258 JSocketChannel ::reset();
259
260 // reserve space for prefix
261
262 resize(sizeof(JPrefix_t));
263 seekp (sizeof(JPrefix_t));
264 }
265
266
268 };
269}
270
271#endif
Exceptions.
Byte array binary input.
virtual int read(char *buffer, const int length) override
Read byte array.
JByteArrayReader()
Default constructor.
Byte array binary output.
int tellp() const
Get write position.
void seekp(const int pos)
Set write position.
virtual int write(const char *buffer, const int length) override
Write byte array.
Exception for socket channel.
ControlHost prefix.
Definition JPrefix.hh:33
void set(const JSocketBuffer< JElement_t > &buffer)
Initialise buffer.
Auxiliary class for socket channel.
void reset()
Reset channel.
Socket input channel.
void reset()
Reset channel.
std::vector< char > buffer
JSocketInputChannel(JTCPSocket &socket)
Constructor.
JStatus_t read()
Interruptable read method.
Non-blocking socket reader.
JStatus_t read()
Continuation of non-blocking read method.
Non-blocking socket writer.
JStatus_t write()
Continuation of non-blocking write method.
Socket output channel.
JSocketOutputChannel(JTCPSocket &socket)
Constructor.
void reset()
Reset channel.
JStatus_t write()
Interruptable write method.
JStatus_t getStatus() const
Get status of I/O.
bool isReady() const
Check ready status.
bool isBusy() const
Check busy status.
bool isReset() const
Check reset status.
TCP socket.
Definition JTCPSocket.hh:30
int getSizeOfPacket(const KM3NETDAQ::JDAQAbstractPreamble &preamble)
Get size of packeet.
void setSizeOfPacket(const int size, JPrefix_t &prefix)
Set total size of internet packet.
Definition JLigier.cc:51
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).