Jpp  18.3.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JSocketBlocking.hh
Go to the documentation of this file.
1 #ifndef __JNET__JSOCKETBLOCKING__
2 #define __JNET__JSOCKETBLOCKING__
3 
4 #include "JNet/JTCPSocket.hh"
5 
6 
7 /**
8  * \author mdejong
9  */
10 
11 namespace JNET {}
12 namespace JPP { using namespace JNET; }
13 
14 namespace JNET {
15 
16 
17  /**
18  * Blocking socket I/O.
19  * This class implements blocking I/O regardless of the configuration
20  * of the socket (see method JSocket::setNonBlocking()).
21  */
23  public JTCPSocket
24  {
25  public:
26  /**
27  * Default constructor.
28  */
30  JTCPSocket()
31  {}
32 
33 
34  /**
35  * Constructor.
36  *
37  * \param socket socket
38  */
39  JSocketBlocking(const JTCPSocket& socket) :
40  JTCPSocket(socket)
41  {}
42 
43 
44 
45  /**
46  * Read data from socket.
47  *
48  * \param buffer buffer
49  * \param length number of bytes to read
50  * \return number of bytes read
51  */
52  int read(char* buffer, const int length) override
53  {
54  char* data = buffer;
55  int size = length;
56 
57  while (size != 0) {
58 
59  const int pos = JSocket::read(data, size);
60 
61  if (pos != 0) {
62  data += pos;
63  size -= pos;
64  } else {
65  in_avail(1);
66  }
67  }
68 
69  return length - size;
70  }
71 
72 
73  /**
74  * Write data to socket.
75  *
76  * \param buffer buffer
77  * \param length number of bytes to write
78  * \return number of bytes written
79  */
80  int write(const char* buffer, const int length) override
81  {
82  const char* data = buffer;
83  int size = length;
84 
85  while (size != 0) {
86 
87  const int pos = JSocket::write(data, size);
88 
89  if (pos != 0) {
90  data += pos;
91  size -= pos;
92  } else {
93  out_avail(1);
94  }
95  }
96 
97  return length - size;
98  }
99  };
100 }
101 
102 #endif
int write(const char *buffer, const int length) override
Write data to socket.
bool in_avail(JTimeval timeout=JTimeval::min()) const
Check availability of input.
Definition: JFile.hh:106
virtual int read(char *buffer, const int length) override
Read data from socket.
Definition: JSocket.hh:183
int read(char *buffer, const int length) override
Read data from socket.
bool out_avail(JTimeval timeout=JTimeval::min()) const
Check availability of output.
Definition: JFile.hh:119
TCP socket.
Definition: JTCPSocket.hh:25
Blocking socket I/O.
JSocketBlocking(const JTCPSocket &socket)
Constructor.
JSocketBlocking()
Default constructor.
virtual int write(const char *buffer, const int length) override
Write data to socket.
Definition: JSocket.hh:222