SoDaRadio-12.2.0-cut_dependencies:6c82803
Loading...
Searching...
No Matches
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#include <SoDa/Format.hxx>
36
37#include <stdexcept>
38
39namespace SoDa {
40
41 namespace IP {
42
49 public:
57 TransportType transport = TCP) :
58 ServerSocket(portnum, transport) {
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 << SoDa::Format("READ Got ret = %0 errno = %1 ready = %2\n")
89 .addI(gotbytes)
90 .addI(errno)
91 .addC((char) (isReady() ? 'T' : 'F'));
92 if(gotbytes < 0) {
93 if((errno != EWOULDBLOCK) && (errno != EAGAIN)) {
94 std::cerr << SoDa::Format("READ got ret = %0 errno = %1\n")
95 .addI(gotbytes)
96 .addI(errno);
97 perror("READ: ");
98 return -1;
99 }
100 }
101 if(gotbytes == 0) {
102 // a nonblocking read that returns 0 is directed at
103 // a closed socket. An open socket with nothing in it
104 // will return -1 with EWOULDBLOCK. So, if we get here,
105 // the remote connection has been closed.
106 close(conn_socket);
107 ready = false;
108 return -1;
109 }
110 if(gotbytes > 0) {
111 for(int i = 0; i < gotbytes; i++) {
112 inbuf.push(temp_buf[i]);
113 if(temp_buf[i] == '\n') ready_line_count++;
114 }
115 }
116 }
117
118 if(ready_line_count > 0) {
119 // scan the buffer until we get to the newline.
121 char ch;
122 for(unsigned int i = 0; i < maxsize; i++) {
123 ch = inbuf.front(); inbuf.pop();
124 if(ch == '\n') {
125 buf[i] = '\000';
126 return i;
127 }
128 else {
129 buf[i] = ch;
130 }
131 }
132 // if we get here, we ran out of buffer space.
133 buf[maxsize-1] = '\000';
134 // consume the queue up to the newline.
135 for(ch = '\000'; (ch != '\n') && !inbuf.empty(); ) {
136 ch = inbuf.front(); inbuf.pop();
137 }
138 return maxsize;
139 }
140 else {
141 return 0;
142 }
143 }
144
145
146 protected:
151 std::queue<char> inbuf;
152
158
166 };
167 }
168}
169
170
171#endif
The Baseclass for all SoDa objects, and useful commonly used classes.
LineServerSocket(int portnum, TransportType transport=TCP)
constructor
int getLine(char *buf, unsigned int maxsize)
capture a newline-terminated buffer from the socket This is a non-blocking method.
char temp_buf[temp_buf_size]
This is the running buffer that we read into from the socket.
static const unsigned int temp_buf_size
This is the size of the running buffer that we read into from the socket.
int ready_line_count
keep a count of the number of "\n" delimited strings we've seen that haven't yet been returned from g...
std::queue< char > inbuf
Do this the simple way – accumulate characters in a queue then return each "\n" terminated substring ...
ServerSocket(int portnum, TransportType transport=TCP)