SoDaRadio-5.0.3-master:8901fb5
UDSockets.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 UDSOCKETS_HDR
30 #define UDSOCKETS_HDR
31 #include "SoDaBase.hxx"
32 #include "MultiMBox.hxx"
33 #include "Command.hxx"
34 
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <stdio.h>
38 #include <netinet/in.h>
39 #include <netdb.h>
40 #include <sys/un.h>
41 
42 #include <string>
43 
44 namespace SoDa {
45  namespace UD { // Unix Domain sockets.
46  class NetSocket {
47  public:
49  timeout.tv_sec = 0;
50  timeout.tv_usec = 5;
51  }
52 
53 
54  int put(const void * ptr, unsigned int size);
55  int get(void * ptr, unsigned int size);
56 
58  struct sockaddr_un server_address, client_address;
59 
60  struct timeval timeout;
61  private:
62  int loopWrite(int fd, const void * ptr, unsigned int nbytes);
63  };
64 
65  class ServerSocket : public NetSocket {
66  public:
67  ServerSocket(const std::string & path);
69  close(conn_socket);
70  close(server_socket);
71  unlink(mailbox_pathname.c_str());
72  }
73  bool isReady();
74 
75  int get(void *ptr, unsigned int size) {
76  int rv = NetSocket::get(ptr, size);
77  if(rv < 0) ready = false;
78  return rv;
79  }
80  int put(const void *ptr, unsigned int size) {
81  if(!ready) return 0;
82  int rv = NetSocket::put(ptr, size);
83  if(rv < 0) ready = false;
84  return rv;
85  }
86  private:
87  bool ready;
88  std::string mailbox_pathname;
89  };
90 
91  class ClientSocket : public NetSocket {
92  public:
93  ClientSocket(const std::string & path, int startup_timeout_count = 1);
94  ~ClientSocket() { close(conn_socket); }
95  private:
96  struct hostent * server;
97  };
98  }
99 }
100 
101 
102 #endif
The Baseclass for all SoDa objects, and useful commonly used classes.
struct timeval timeout
Definition: UDSockets.hxx:60
std::string mailbox_pathname
Definition: UDSockets.hxx:88
int loopWrite(int fd, const void *ptr, unsigned int nbytes)
Definition: UDSockets.cxx:140
struct sockaddr_un server_address client_address
Definition: UDSockets.hxx:58
int put(const void *ptr, unsigned int size)
Definition: UDSockets.cxx:162
int get(void *ptr, unsigned int size)
Definition: UDSockets.cxx:175
struct hostent * server
Definition: UDSockets.hxx:96
int put(const void *ptr, unsigned int size)
Definition: UDSockets.hxx:80