1 // SocketDriver.hh
2 //
3 // Copyright (C) 2002, 2003, 2005, 2007, 2010, 2012 Rob Caelers <robc@krandor.nl>
4 // All rights reserved.
5 //
6 // This program is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 //
19 
20 #ifndef SOCKETDRIVER_HH
21 #define SOCKETDRIVER_HH
22 
23 class ISocket;
24 class ISocketServer;
25 
26 #include "Exception.hh"
27 
28 using namespace workrave;
29 
30 //! Asynchronous socket callbacks.
31 class ISocketListener
32 {
33 public:
ISocketListener()34   ISocketListener() {}
~ISocketListener()35   virtual ~ISocketListener() {}
36 
37   //! The specified socket is now connected.
38   virtual void socket_connected(ISocket *con, void *data) = 0;
39 
40   //! The specified socket has data ready to be read.
41   virtual void socket_io(ISocket *con, void *data) = 0;
42 
43   //! The specified socket closed its connection.
44   virtual void socket_closed(ISocket *con, void *data) = 0;
45 };
46 
47 
48 //! Asynchronous server socket callbacks.
49 class ISocketServerListener
50 {
51 public:
ISocketServerListener()52   ISocketServerListener() {}
~ISocketServerListener()53   virtual ~ISocketServerListener() {}
54 
55   //! The specified server socket has accepted a new connection
56   virtual void socket_accepted(ISocketServer *server, ISocket *con) = 0;
57 };
58 
59 
60 //! TCP Socket.
61 class ISocket
62 {
63 public:
ISocket()64   ISocket() :
65     listener(NULL)
66   {
67   }
68 
~ISocket()69   virtual ~ISocket() {};
70 
71   //! Create a connection to the specified host and port.
72   virtual void connect(const std::string &hostname, int port) = 0;
73 
74   //! Read data from the connection.
75   virtual void read(void *buf, int count, int &bytes_read) = 0;
76 
77   //! Write data to the connection
78   virtual void write(void *buf, int count, int &bytes_written) = 0;
79 
80   //! Close the connection.
81   virtual void close() = 0;
82 
83   //! Set the callback listener for asynchronous socket events.
84   void set_listener(ISocketListener *l);
85 
86   //! Set user data
87   void set_data(void *data);
88 
89 protected:
90   //! Listener that received notifications of socket events.
91   ISocketListener *listener;
92 
93   // User data for callback.
94   void *user_data;
95 };
96 
97 
98 //! TCP Listen ISocket.
99 class ISocketServer
100 {
101 public:
ISocketServer()102   ISocketServer() :
103     listener(NULL)
104   {
105   }
106 
~ISocketServer()107   virtual ~ISocketServer() {};
108 
109   //! Listen at the specified port.
110   /*! \pre set_listener called
111    */
112   virtual void listen(int port) = 0;
113 
114   //! Sets the callback listener for asynchronous events.
115   void set_listener(ISocketServerListener *l);
116 
117 protected:
118   //! Listener that receives notification of accepted connections.
119   ISocketServerListener *listener;
120 };
121 
122 
123 //! TCP Socket abstraction.
124 class SocketDriver
125 {
126 public:
127   static SocketDriver *create();
128 
~SocketDriver()129   virtual ~SocketDriver() {};
130 
131   //! Create a new socket
132   virtual ISocket *create_socket() = 0;
133 
134   //! Create a new listen socket
135   virtual ISocketServer *create_server() = 0;
136 };
137 
138 
139 //! Socket exception
140 class SocketException : public Exception
141 {
142 public:
SocketException(const std::string & detail)143   explicit SocketException(const std::string &detail) :
144     Exception(detail)
145   {
146   }
147 
~SocketException()148   virtual ~SocketException() throw()
149   {
150   }
151 };
152 
153 #include "SocketDriver.icc"
154 
155 #endif // SOCKETDRIVER_HH
156