Jpp - 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/JSocket.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 JSocket
24  {
25  public:
26  /**
27  * Default constructor.
28  */
30  JSocket()
31  {}
32 
33 
34  /**
35  * Constructor.
36  *
37  * \param socket socket
38  */
39  JSocketBlocking(const JSocket& socket) :
40  JSocket(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  */
51  int read(char* buffer, const int length)
52  {
53  char* data = buffer;
54  int size = length;
55 
56  while (size != 0) {
57 
58  const int pos = JSocket::read(data, size);
59 
60  if (pos != 0) {
61  data += pos;
62  size -= pos;
63  } else {
64  in_avail(1);
65  }
66  }
67 
68  return length - size;
69  }
70 
71 
72  /**
73  * Write data to socket.
74  *
75  * \param buffer buffer
76  * \param length number of bytes to write
77  */
78  int write(const char* buffer, const int length)
79  {
80  const char* data = buffer;
81  int size = length;
82 
83  while (size != 0) {
84 
85  const int pos = JSocket::write(data, size);
86 
87  if (pos != 0) {
88  data += pos;
89  size -= pos;
90  } else {
91  out_avail(1);
92  }
93  }
94 
95  return length - size;
96  }
97  };
98 }
99 
100 #endif
int write(const char *buffer, const int length)
Write data to socket.
Socket class.
Definition: JSocket.hh:42
JSocketBlocking(const JSocket &socket)
Constructor.
bool in_avail(JTimeval timeout=JTimeval::min()) const
Check availability of input.
Definition: JFile.hh:100
virtual int read(char *buffer, const int length) override
Read data from socket.
Definition: JSocket.hh:300
bool out_avail(JTimeval timeout=JTimeval::min()) const
Check availability of output.
Definition: JFile.hh:113
Blocking socket I/O.
JSocketBlocking()
Default constructor.
int read(char *buffer, const int length)
Read data from socket.
virtual int write(const char *buffer, const int length) override
Write data to socket.
Definition: JSocket.hh:339