1 // 2 // Copyright (C) 2002, 2003, 2007, 2008, 2010 Rob Caelers <robc@krandor.nl> 3 // All rights reserved. 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 // 15 // You should have received a copy of the GNU General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 // 18 19 #ifndef GNETSOCKETDRIVER_HH 20 #define GNETSOCKETDRIVER_HH 21 22 #define GNET_EXPERIMENTAL 23 #include <glib.h> 24 #include <gnet.h> 25 26 #include "SocketDriver.hh" 27 28 using namespace workrave; 29 30 //! Listen socket implementation usinf GNet 31 class GNetSocketServer 32 : public ISocketServer 33 { 34 public: 35 GNetSocketServer(); 36 virtual ~GNetSocketServer(); 37 38 // ISocketServer interface 39 virtual void listen(int port); 40 41 private: 42 // GNET callbacks 43 void async_accept(GTcpSocket *server, GTcpSocket *client); 44 static void static_async_accept(GTcpSocket* server, GTcpSocket* client, gpointer data); 45 46 private: 47 //! GNet socket 48 GTcpSocket *socket; 49 50 //! Glib IOChannel 51 GIOChannel *iochannel; 52 53 //! I/O Events we are monitoring. 54 gint watch_flags; 55 56 //! Our watch ID 57 guint watch; 58 }; 59 60 61 //! Socket implementation based on GNet 62 class GNetSocket 63 : public ISocket 64 { 65 public: 66 GNetSocket(); 67 GNetSocket(GTcpSocket *socket); 68 virtual ~GNetSocket(); 69 70 // ISocket interface 71 virtual void connect(const std::string &hostname, int port); 72 virtual void read(void *buf, int count, int &bytes_read); 73 virtual void write(void *buf, int count, int &bytes_written); 74 virtual void close(); 75 76 private: 77 // GNET callbacks 78 bool async_io(GIOChannel* iochannel, GIOCondition condition); 79 void async_connected(GTcpSocket *socket, GInetAddr *ia, GTcpSocketConnectAsyncStatus status); 80 static gboolean static_async_io(GIOChannel* iochannel, GIOCondition condition, gpointer data); 81 static void static_async_connected(GTcpSocket *socket, GTcpSocketConnectAsyncStatus status, gpointer data); 82 83 private: 84 //! GNet socket 85 GTcpSocket *socket; 86 87 //! Glib IOChannel 88 GIOChannel *iochannel; 89 90 //! I/O Events we are monitoring 91 gint watch_flags; 92 93 //! Our watch ID 94 guint watch; 95 }; 96 97 98 class GNetSocketDriver 99 : public SocketDriver 100 { 101 //! Create a new socket 102 ISocket *create_socket(); 103 104 //! Create a new listen socket 105 ISocketServer *create_server(); 106 }; 107 108 109 #endif // GNETSOCKETDRIVER_HH 110