Pol  Revision:cb584c9
sckutil.cpp
Go to the documentation of this file.
1 
7 #include "sckutil.h"
8 
9 #include "esignal.h"
10 #include "strutil.h"
11 #include "wnsckt.h"
12 
13 namespace Pol
14 {
15 namespace Clib
16 {
17 bool readline( Socket& sck, std::string& s, bool* timeout_exit, unsigned int timeout_secs,
18  unsigned maxlen )
19 {
20  if ( timeout_exit )
21  *timeout_exit = false;
22  s = "";
23  unsigned char ch;
24  unsigned timeouts_left = timeout_secs / 2;
25  while ( !exit_signalled && sck.connected() )
26  {
27  if ( sck.recvbyte( &ch, 2000 ) )
28  {
29  timeouts_left = timeout_secs / 2;
30  if ( isprint( ch ) )
31  {
32  s.append( 1, ch );
33  if ( maxlen && s.length() > maxlen )
34  {
35  sck.close();
36  return false;
37  }
38  }
39  else
40  {
41  if ( ch == '\n' )
42  return true;
43  }
44  }
45  else
46  {
47  if ( timeout_secs && !--timeouts_left )
48  {
49  if ( timeout_exit )
50  *timeout_exit = true;
51  return false;
52  }
53  }
54  }
55  return false;
56 }
57 
58 void writeline( Socket& sck, const std::string& s )
59 {
60  sck.send( (void*)s.c_str(), static_cast<unsigned int>( s.length() ) );
61  sck.send( "\r\n", 2 );
62 }
63 
64 bool readstring( Socket& sck, std::string& s, unsigned int interchar_secs, unsigned length )
65 {
66  s = "";
67  unsigned char ch;
68  unsigned timeouts_left = interchar_secs / 2;
69  while ( !exit_signalled && sck.connected() )
70  {
71  if ( sck.recvbyte( &ch, 2000 ) )
72  {
73  timeouts_left = interchar_secs / 2;
74 
75  s.append( 1, ch );
76  if ( s.length() == length )
77  {
78  return true;
79  }
80  }
81  else
82  {
83  if ( !--timeouts_left )
84  return false;
85  }
86  }
87  return false;
88 }
89 }
90 }
bool connected() const
Definition: wnsckt.cpp:308
bool recvbyte(unsigned char *byte, unsigned int waitms)
Definition: wnsckt.cpp:354
void writeline(Socket &sck, const std::string &s)
Definition: sckutil.cpp:58
bool readline(Socket &sck, std::string &s, bool *timeout_exit, unsigned int timeout_secs, unsigned maxlen)
Definition: sckutil.cpp:17
Definition: berror.cpp:12
void send(const void *data, unsigned length)
Definition: wnsckt.cpp:553
std::atomic< bool > exit_signalled
bool readstring(Socket &sck, std::string &s, unsigned int interchar_secs, unsigned length)
Definition: sckutil.cpp:64