VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
glocalsocket.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 ///
18 /// \file glocalsocket.h
19 ///
20 
21 #ifndef G_LOCAL_SOCKET__H
22 #define G_LOCAL_SOCKET__H
23 
24 #include "gdef.h"
25 #include "gexception.h"
26 #include "gsignalsafe.h"
27 #include <string>
28 #include <cstring>
29 #include <sys/socket.h>
30 #include <sys/un.h>
31 
32 namespace G
33 {
34  class LocalSocketAddress ;
35  class LocalSocket ;
36 }
37 
38 /// \class G::LocalSocketAddress
39 /// An address class for G::LocalSocket that works with file system paths.
40 ///
42 {
43 public:
44  explicit LocalSocketAddress( const std::string & path ) ;
45  ///< Constructor.
46 
47  LocalSocketAddress( SignalSafe , const char * path ) ;
48  ///< Constructor. Signal-safe overload.
49 
50  struct sockaddr * p() ;
51  ///< Returns the sockaddr pointer.
52 
53  const struct sockaddr * p() const ;
54  ///< Const overload.
55 
56  size_t size() const ;
57  ///< Returns the sockaddr size.
58 
59 private:
60  union Union
61  {
62  struct sockaddr_un specific ;
63  struct sockaddr general ;
64  } ;
65  Union m_u ;
66 } ;
67 
68 /// \class G::LocalSocket
69 /// and accept()ing should be performed directly on the file descriptor.
70 /// \see G::Msg
71 ///
73 {
74 public:
75  typedef LocalSocketAddress Address ;
76  G_EXCEPTION( Error , "local socket error" ) ;
77 
78  explicit LocalSocket( bool datagram ) ;
79  ///< Constructor for a datagram socket or a stream socket.
80 
81  ~LocalSocket() ;
82  ///< Destructor.
83 
84  bool connect( const std::string & address_path , bool no_throw = false ) ;
85  ///< Connects to the given address. For datagram sockets this just becomes
86  ///< the default destination address. By default throws Error on error.
87 
88  void bind( const std::string & address_path ) ;
89  ///< Binds the given address to the socket. Throws Error on error.
90 
91  void nonblock() ;
92  ///< Makes the socket non-blocking.
93 
94  int fd() const ;
95  ///< Returns the socket's file descriptor.
96 
97  bool connected() const ;
98  ///< Returns true if the socket has been successfully connect()ed.
99 
100 private:
101  LocalSocket( const LocalSocket & ) ;
102  void operator=( const LocalSocket & ) ;
103 
104 private:
105  int m_fd ;
106  bool m_connected ;
107 } ;
108 
109 #endif
LocalSocketAddress(const std::string &path)
Constructor.
An empty structure that is used to indicate a signal-safe, reentrant implementation.
Definition: gsignalsafe.h:36
int fd() const
Returns the socket's file descriptor.
void nonblock()
Makes the socket non-blocking.
LocalSocket(bool datagram)
Constructor for a datagram socket or a stream socket.
bool connect(const std::string &address_path, bool no_throw=false)
Connects to the given address.
~LocalSocket()
Destructor.
struct sockaddr * p()
Returns the sockaddr pointer.
bool connected() const
Returns true if the socket has been successfully connect()ed.
and accept()ing should be performed directly on the file descriptor.
Definition: glocalsocket.h:72
An address class for G::LocalSocket that works with file system paths.
Definition: glocalsocket.h:41
size_t size() const
Returns the sockaddr size.
void bind(const std::string &address_path)
Binds the given address to the socket. Throws Error on error.