VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gvcommandsocket.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 gvcommandsocket.h
19 ///
20 
21 #ifndef GV_COMMAND_SOCKET__H
22 #define GV_COMMAND_SOCKET__H
23 
24 #include "gdef.h"
25 #include "gnet.h"
26 #include "gsocket.h"
27 #include "glocalsocket.h"
28 #include "gsignalsafe.h"
29 #include <vector>
30 #include <string>
31 
32 namespace Gv
33 {
34  class CommandSocket ;
35  class CommandSocketMixin ;
36  class CommandSocketMixinImp ;
37 }
38 
39 /// \class Gv::CommandSocket
40 /// A non-blocking datagram socket that is used for sending and receiving
41 /// process control commands. The socket is either a local unix-domain socket,
42 /// or a UDP network socket, depending on the format of the socket name,
43 /// although a prefix of "udp://" can be used to override the heuristics.
44 /// Synchronous DNS lookups are used if necessary.
45 ///
47 {
48 public:
49  struct Type /// Describes a Gv::CommandSocket as a UDP address or unix-domain path.
50  {
51  bool net ;
52  std::string path ;
53  unsigned int port ;
54  } ;
55  struct NoThrow /// Overload descriminator for Gv::CommandSocket
56  {
57  } ;
58 
59  explicit CommandSocket( const std::string & bind_name ) ;
60  ///< Constructor for a receiving socket, taking a local filesystem path
61  ///< or a transport address. Does synchronous dns resolution if necessary.
62  ///< Constructs a do-nothing object if the name is empty.
63  ///< On error fd() returns -1.
64 
65  CommandSocket() ;
66  ///< Constructor for a sending socket.
67 
68  void connect( const std::string & connect_name ) ;
69  ///< Creates an association with the remote socket, taking a local filesystem
70  ///< path or a transport address. Does synchronous dns resolution if necessary.
71  ///< Throws on error.
72 
73  std::string connect( const std::string & connect_name , NoThrow ) ;
74  ///< Creates an association with the remote socket, taking a local filesystem
75  ///< path or a transport address. Does synchronous dns resolution if necessary.
76  ///< Returns a failure reason on error.
77 
78  int fd() const ;
79  ///< Returns the file descriptor.
80 
81  std::string read() ;
82  ///< Reads the socket. Returns the empty string on error.
83 
84  static Type parse( const std::string & bind_name ) ;
85  ///< Parses a filesystem path or transport address.
86 
87  void close() ;
88  ///< Closes the sending socket, if open.
89 
90 private:
91  CommandSocket( const CommandSocket & ) ;
92  void operator=( const CommandSocket & ) ;
93  static void cleanup( G::SignalSafe , const char * ) ;
94 
95 private:
96  unique_ptr<G::LocalSocket> m_local ;
97  unique_ptr<GNet::DatagramSocket> m_net ;
98  std::vector<char> m_buffer ;
99 } ;
100 
101 /// \class Gv::CommandSocketMixin
102 /// A mixin base class that contains a bound Gv::CommandSocket object
103 /// integrated with the GNet::EventLoop.
104 ///
106 {
107 public:
108  explicit CommandSocketMixin( const std::string & bind_name ) ;
109  ///< Constructor.
110 
111  virtual ~CommandSocketMixin() ;
112  ///< Destructor.
113 
114  virtual void onCommandSocketData( std::string ) = 0 ;
115  ///< Callback that delivers a datagram.
116 
117 private:
119  void operator=( const CommandSocketMixin & ) ;
120 
121 private:
122  CommandSocketMixinImp * m_imp ;
123 } ;
124 
125 #endif
Describes a Gv::CommandSocket as a UDP address or unix-domain path.
virtual void onCommandSocketData(std::string)=0
Callback that delivers a datagram.
An empty structure that is used to indicate a signal-safe, reentrant implementation.
Definition: gsignalsafe.h:36
Overload descriminator for Gv::CommandSocket.
A mixin base class that contains a bound Gv::CommandSocket object integrated with the GNet::EventLoop...
CommandSocket()
Constructor for a sending socket.
static Type parse(const std::string &bind_name)
Parses a filesystem path or transport address.
void connect(const std::string &connect_name)
Creates an association with the remote socket, taking a local filesystem path or a transport address...
int fd() const
Returns the file descriptor.
virtual ~CommandSocketMixin()
Destructor.
std::string read()
Reads the socket. Returns the empty string on error.
A non-blocking datagram socket that is used for sending and receiving process control commands...
void close()
Closes the sending socket, if open.
CommandSocketMixin(const std::string &bind_name)
Constructor.
A pimple-pattern implementation class for Gv::CommandSocketMixin.