Jpp  17.1.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JSocketNonblocking.hh
Go to the documentation of this file.
1 #ifndef __JNET__JSOCKETNONBLOCKINGIO__
2 #define __JNET__JSOCKETNONBLOCKINGIO__
3 
4 #include "JNet/JSocket.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 JSocket,
120  public JSocketInputBuffer
121  {
122  public:
123  /**
124  * Constructor.
125  *
126  * \param socket socket
127  */
129  JSocket(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 
163 
164  /**
165  * Non-blocking socket writer.
166  * This class can be used in case of non-blocking I/O
167  * (see method JSocket::setNonBlocking()).
168  */
170  public JSocket,
171  public JSocketOutputBuffer
172  {
173  public:
174  /**
175  * Constructor.
176  *
177  * \param socket socket
178  */
180  JSocket(socket),
182  {}
183 
184 
185  /**
186  * Continuation of non-blocking write method.
187  *
188  * \return status
189  */
191  {
192  if (status == IO_BUSY) {
193 
194  ++counter;
195 
196  const int pos = JSocket::write(__data, __size);
197 
198  __data += pos;
199  __size -= pos;
200 
201  } else {
202 
203  THROW(JSocketException, "Calling socket non-blocking write() while not busy.");
204  }
205 
206  if (__size == 0) {
207  status = IO_READY;
208  }
209 
210  return status;
211  }
212  };
213 }
214 
215 #endif
JSocketBuffer()
Default constructor.
void set(const JSocketBuffer< JElement_t > &buffer)
Initialise buffer.
JSocketBuffer< const char > JSocketOutputBuffer
JSocketBuffer< char > JSocketInputBuffer
Auxiliary class for non-blocking socket I/O.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
Non-blocking socket reader.
Socket class.
Definition: JSocket.hh:43
void reset()
Reset.
virtual int read(char *buffer, const int length) override
Read data from socket.
Definition: JSocket.hh:301
Non-blocking socket writer.
JSocketNonblockingWriter(const JSocket &socket)
Constructor.
JStatus_t write()
Continuation of non-blocking write method.
void set(JElement_t *buffer, const int length)
Initialise buffer.
JStatus_t read()
Continuation of non-blocking read method.
JSocketNonblockingReader(const JSocket &socket)
Constructor.
Exception for socket.
Definition: JException.hh:450
int getSize() const
Get size of pending data.
virtual int write(const char *buffer, const int length) override
Write data to socket.
Definition: JSocket.hh:340
JSocketBuffer(JElement_t *buffer, const int length)
Constructor.
Auxiliary class for non-blocking socket I/O.