SoDaRadio-5.0.3-master:8901fb5
LineSocket.hxx
Go to the documentation of this file.
1 /*
2  Copyright (c) 2016, Matthew H. Reilly (kb1vc)
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are
7  met:
8 
9  Redistributions of source code must retain the above copyright
10  notice, this list of conditions and the following disclaimer.
11  Redistributions in binary form must reproduce the above copyright
12  notice, this list of conditions and the following disclaimer in
13  the documentation and/or other materials provided with the
14  distribution.
15 
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 
29 #ifndef LINESOCKET_HDR
30 #define LINESOCKET_HDR
31 #include "SoDaBase.hxx"
32 #include "IPSockets.hxx"
33 #include "UDSockets.hxx"
34 #include "Command.hxx"
35 
36 
37 #include <stdexcept>
38 
39 namespace SoDa {
40 
41  namespace IP {
42 
48  class LineServerSocket : public ServerSocket {
49  public:
57  TransportType transport = TCP) :
58  ServerSocket(portnum, transport) {
59  ready_line_count = 0;
60 
61  // we want to use the nonblocking read.
63  }
64 
71  static const unsigned int temp_buf_size = 128;
72 
84  int getLine(char * buf, unsigned int maxsize) {
85  if(ready_line_count == 0) {
86  // get a buffer;
87  int gotbytes = read(conn_socket, temp_buf, temp_buf_size);
88  std::cerr << boost::format("READ Got ret = %d errno = %d ready = %c\n")
89  % gotbytes % errno % ((char) (isReady() ? 'T' : 'F'));
90  if(gotbytes < 0) {
91  if((errno != EWOULDBLOCK) && (errno != EAGAIN)) {
92  std::cerr << boost::format("READ got ret = %d errno = %d\n")
93  % gotbytes % errno;
94  perror("READ: ");
95  return -1;
96  }
97  }
98  if(gotbytes == 0) {
99  // a nonblocking read that returns 0 is directed at
100  // a closed socket. An open socket with nothing in it
101  // will return -1 with EWOULDBLOCK. So, if we get here,
102  // the remote connection has been closed.
103  close(conn_socket);
104  ready = false;
105  return -1;
106  }
107  if(gotbytes > 0) {
108  for(int i = 0; i < gotbytes; i++) {
109  inbuf.push(temp_buf[i]);
110  if(temp_buf[i] == '\n') ready_line_count++;
111  }
112  }
113  }
114 
115  if(ready_line_count > 0) {
116  // scan the buffer until we get to the newline.
118  char ch;
119  for(unsigned int i = 0; i < maxsize; i++) {
120  ch = inbuf.front(); inbuf.pop();
121  if(ch == '\n') {
122  buf[i] = '\000';
123  return i;
124  }
125  else {
126  buf[i] = ch;
127  }
128  }
129  // if we get here, we ran out of buffer space.
130  buf[maxsize-1] = '\000';
131  // consume the queue up to the newline.
132  for(ch = '\000'; (ch != '\n') && !inbuf.empty(); ) {
133  ch = inbuf.front(); inbuf.pop();
134  }
135  return maxsize;
136  }
137  else {
138  return 0;
139  }
140  }
141 
142 
143  protected:
148  std::queue<char> inbuf;
149 
155 
163  };
164  }
165 }
166 
167 
168 #endif
The Baseclass for all SoDa objects, and useful commonly used classes.
int getLine(char *buf, unsigned int maxsize)
capture a newline-terminated buffer from the socket This is a non-blocking method.
Definition: LineSocket.hxx:84
LineServerSocket(int portnum, TransportType transport=TCP)
constructor
Definition: LineSocket.hxx:56
std::queue< char > inbuf
Do this the simple way – accumulate characters in a queue then return each "\n" terminated substring...
Definition: LineSocket.hxx:148
static const unsigned int temp_buf_size
This is the size of the running buffer that we read into from the socket.
Definition: LineSocket.hxx:71
char temp_buf[temp_buf_size]
This is the running buffer that we read into from the socket.
Definition: LineSocket.hxx:162
This is an IP socket that returns newline-delimited strings arriving on the socket.
Definition: LineSocket.hxx:48
int ready_line_count
keep a count of the number of "\n" delimited strings we&#39;ve seen that haven&#39;t yet been returned from g...
Definition: LineSocket.hxx:154