Jpp  19.1.0-rc.1
the software that should make you happy
JSocketNonblocking.hh
Go to the documentation of this file.
1 #ifndef __JNET__JSOCKETNONBLOCKINGIO__
2 #define __JNET__JSOCKETNONBLOCKINGIO__
3 
4 #include "JNet/JTCPSocket.hh"
5 #include "JNet/JSocketStatus.hh"
6 
7 
8 /**
9  * \author mdejong
10  */
11 
12 namespace JNET {}
13 namespace JPP { using namespace JNET; }
14 
15 namespace JNET {
16 
17 
18  /**
19  * Auxiliary class for non-blocking socket I/O.
20  */
21  template<class JElement_t>
22  class JSocketBuffer :
23  public JSocketStatus
24  {
25  protected:
26  /**
27  * Default constructor.
28  */
30  JSocketStatus(),
31  __data(NULL),
32  __size(0)
33  {}
34 
35 
36  public:
37  /**
38  * Constructor.
39  *
40  * \param buffer I/O data
41  * \param length number of bytes
42  */
43  JSocketBuffer(JElement_t* buffer, const int length) :
44  JSocketStatus(),
45  __data(buffer),
46  __size(length)
47  {}
48 
49 
50  /**
51  * Get size of pending data.
52  *
53  * \return number of bytes
54  */
55  int getSize() const
56  {
57  return __size;
58  }
59 
60 
61  /**
62  * Initialise buffer.
63  *
64  * \param buffer buffer
65  */
66  void set(const JSocketBuffer<JElement_t>& buffer)
67  {
68  set(buffer.__data, buffer.__size);
69  }
70 
71 
72  /**
73  * Initialise buffer.
74  *
75  * \param buffer I/O data
76  * \param length number of bytes
77  */
78  void set(JElement_t* buffer, const int length)
79  {
80  if (status == IO_BUSY) {
81  THROW(JSocketException, "Set socket buffer I/O while busy.");
82  }
83 
84  status = (length != 0 ? IO_BUSY : IO_READY);
85  counter = 0;
86  __data = buffer;
87  __size = length;
88  }
89 
90 
91  /**
92  * Reset
93  */
94  void reset()
95  {
97 
98  __data = NULL;
99  __size = 0;
100  }
101 
102 
103  protected:
104  JElement_t* __data;
105  int __size;
106  };
107 
108 
111 
112 
113  /**
114  * Non-blocking socket reader.
115  * This class can be used in case of non-blocking I/O
116  * (see method JSocket::setNonBlocking()).
117  */
119  public JTCPSocket,
120  public JSocketInputBuffer
121  {
122  public:
123  /**
124  * Constructor.
125  *
126  * \param socket socket
127  */
129  JTCPSocket(socket),
131  {}
132 
133 
134  /**
135  * Continuation of non-blocking read method.
136  *
137  * \return status
138  */
140  {
141  if (status == IO_BUSY) {
142 
143  ++counter;
144 
145  const int pos = JSocket::read(__data, __size);
146 
147  __data += pos;
148  __size -= pos;
149 
150  } else {
151 
152  THROW(JSocketException, "Calling socket non-blocking read() while not busy.");
153  }
154 
155  if (__size == 0) {
156  status = IO_READY;
157  }
158 
159  return status;
160  }
161 
162  private:
163  using JSocket::read;
164  };
165 
166 
167  /**
168  * Non-blocking socket writer.
169  * This class can be used in case of non-blocking I/O
170  * (see method JSocket::setNonBlocking()).
171  */
173  public JTCPSocket,
174  public JSocketOutputBuffer
175  {
176  public:
177  /**
178  * Constructor.
179  *
180  * \param socket socket
181  */
183  JTCPSocket(socket),
185  {}
186 
187 
188  /**
189  * Continuation of non-blocking write method.
190  *
191  * \return status
192  */
194  {
195  if (status == IO_BUSY) {
196 
197  ++counter;
198 
199  const int pos = JSocket::write(__data, __size);
200 
201  __data += pos;
202  __size -= pos;
203 
204  } else {
205 
206  THROW(JSocketException, "Calling socket non-blocking write() while not busy.");
207  }
208 
209  if (__size == 0) {
210  status = IO_READY;
211  }
212 
213  return status;
214  }
215 
216  private:
217  using JSocket::write;
218  };
219 }
220 
221 #endif
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
Exception for socket.
Definition: JException.hh:468
Auxiliary class for non-blocking socket I/O.
JSocketBuffer()
Default constructor.
void set(const JSocketBuffer< JElement_t > &buffer)
Initialise buffer.
JSocketBuffer(JElement_t *buffer, const int length)
Constructor.
void set(JElement_t *buffer, const int length)
Initialise buffer.
int getSize() const
Get size of pending data.
Non-blocking socket reader.
JSocketNonblockingReader(const JTCPSocket &socket)
Constructor.
JStatus_t read()
Continuation of non-blocking read method.
Non-blocking socket writer.
JStatus_t write()
Continuation of non-blocking write method.
JSocketNonblockingWriter(const JTCPSocket &socket)
Constructor.
Auxiliary class for non-blocking socket I/O.
void reset()
Reset.
virtual int read(char *buffer, const int length) override
Read data from socket.
Definition: JSocket.hh:198
virtual int write(const char *buffer, const int length) override
Write data to socket.
Definition: JSocket.hh:237
TCP socket.
Definition: JTCPSocket.hh:30
JSocketBuffer< char > JSocketInputBuffer
JSocketBuffer< const char > JSocketOutputBuffer
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).