SoDaRadio-5.0.3-master:8901fb5
IPSockets.hxx
Go to the documentation of this file.
1 /*
2  Copyright (c) 2012, 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 IPSOCKETS_HDR
30 #define IPSOCKETS_HDR
31 #include "SoDaBase.hxx"
32 #include "Command.hxx"
33 
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <stdio.h>
37 #include <netinet/in.h>
38 #include <netdb.h>
39 #include <fcntl.h>
40 
41 #include <stdexcept>
42 
43 namespace SoDa {
44  namespace IP {
45 
46  class ReadTimeoutExc : public std::runtime_error {
47  public:
48  ReadTimeoutExc(std::string sockname) :
49  std::runtime_error((boost::format("Read operation on %s socket timed out") % sockname).str()) {
50  }
51  };
52 
53  enum TransportType { UDP, TCP };
54 
55  class NetSocket {
56  public:
58  timeout.tv_sec = 0;
59  timeout.tv_usec = 5;
60  }
61 
62 
63  int put(const void * ptr, unsigned int size);
64  int get(void * ptr, unsigned int size);
65 
66  int putRaw(const void * ptr, unsigned int size);
67 
76  int getRaw(const void * ptr, unsigned int size, unsigned int usec_timeout = 0);
77 
81  void setNonBlocking() {
82  int x = fcntl(conn_socket, F_GETFL, 0);
83  fcntl(conn_socket, F_SETFL, (x | O_NONBLOCK));
84  non_blocking_mode = true;
85  }
86 
87  void setBlocking() {
88  int x = fcntl(conn_socket, F_GETFL, 0);
89  fcntl(conn_socket, F_SETFL, (x & ~O_NONBLOCK));
90  non_blocking_mode = false;
91  }
92 
93  int server_socket, conn_socket, portnum;
94  struct sockaddr_in server_address, client_address;
96 
97  struct timeval timeout;
98  private:
99  int loopWrite(int fd, const void * ptr, unsigned int nbytes);
100  };
101 
102  class ServerSocket : public NetSocket {
103  public:
104  ServerSocket(int portnum, TransportType transport = TCP);
105  ~ServerSocket() { close(conn_socket); close(server_socket); }
106  bool isReady();
107 
108  int get(void *ptr, unsigned int size) {
109  int rv = NetSocket::get(ptr, size);
110  if(rv < 0) ready = false;
111  return rv;
112  }
113  int put(const void *ptr, unsigned int size) {
114  if(!ready) return 0;
115  int rv = NetSocket::put(ptr, size);
116  if(rv < 0) ready = false;
117  return rv;
118  }
119  protected:
120  bool ready;
121  };
122 
123  class ClientSocket : public NetSocket {
124  public:
125  ClientSocket(const char * hostname, int portnum, TransportType transport = TCP);
126  ~ClientSocket() { close(conn_socket); }
127  private:
128  struct hostent * server;
129  };
130  }
131 }
132 
133 
134 #endif
The Baseclass for all SoDa objects, and useful commonly used classes.
STL namespace.
int put(const void *ptr, unsigned int size)
Definition: IPSockets.hxx:113
int put(const void *ptr, unsigned int size)
Definition: IPSockets.cxx:164
ReadTimeoutExc(std::string sockname)
Definition: IPSockets.hxx:48
struct hostent * server
Definition: IPSockets.hxx:128
int get(void *ptr, unsigned int size)
Definition: IPSockets.cxx:177