| 1 | /* | 
|---|
| 2 | * synergy -- mouse and keyboard sharing utility | 
|---|
| 3 | * Copyright (C) 2004 Chris Schoeneman | 
|---|
| 4 | * | 
|---|
| 5 | * This package is free software; you can redistribute it and/or | 
|---|
| 6 | * modify it under the terms of the GNU General Public License | 
|---|
| 7 | * found in the file COPYING that should have accompanied this file. | 
|---|
| 8 | * | 
|---|
| 9 | * This package 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 |  | 
|---|
| 15 | #ifndef TSOCKERMULTIPLEXERMETHODJOB_H | 
|---|
| 16 | #define TSOCKERMULTIPLEXERMETHODJOB_H | 
|---|
| 17 |  | 
|---|
| 18 | #include "ISocketMultiplexerJob.h" | 
|---|
| 19 | #include "CArch.h" | 
|---|
| 20 |  | 
|---|
| 21 | //! Use a method as a socket multiplexer job | 
|---|
| 22 | /*! | 
|---|
| 23 | A socket multiplexer job class that invokes a member function. | 
|---|
| 24 | */ | 
|---|
| 25 | template <class T> | 
|---|
| 26 | class TSocketMultiplexerMethodJob : public ISocketMultiplexerJob { | 
|---|
| 27 | public: | 
|---|
| 28 | typedef ISocketMultiplexerJob* | 
|---|
| 29 | (T::*Method)(ISocketMultiplexerJob*, bool, bool, bool); | 
|---|
| 30 |  | 
|---|
| 31 | //! run() invokes \c object->method(arg) | 
|---|
| 32 | TSocketMultiplexerMethodJob(T* object, Method method, | 
|---|
| 33 | CArchSocket socket, bool readable, bool writeable); | 
|---|
| 34 | virtual ~TSocketMultiplexerMethodJob(); | 
|---|
| 35 |  | 
|---|
| 36 | // IJob overrides | 
|---|
| 37 | virtual ISocketMultiplexerJob* | 
|---|
| 38 | run(bool readable, bool writable, bool error); | 
|---|
| 39 | virtual CArchSocket     getSocket() const; | 
|---|
| 40 | virtual bool            isReadable() const; | 
|---|
| 41 | virtual bool            isWritable() const; | 
|---|
| 42 |  | 
|---|
| 43 | private: | 
|---|
| 44 | T*                                      m_object; | 
|---|
| 45 | Method                          m_method; | 
|---|
| 46 | CArchSocket                     m_socket; | 
|---|
| 47 | bool                            m_readable; | 
|---|
| 48 | bool                            m_writable; | 
|---|
| 49 | void*                           m_arg; | 
|---|
| 50 | }; | 
|---|
| 51 |  | 
|---|
| 52 | template <class T> | 
|---|
| 53 | inline | 
|---|
| 54 | TSocketMultiplexerMethodJob<T>::TSocketMultiplexerMethodJob(T* object, | 
|---|
| 55 | Method method, CArchSocket socket, | 
|---|
| 56 | bool readable, bool writable) : | 
|---|
| 57 | m_object(object), | 
|---|
| 58 | m_method(method), | 
|---|
| 59 | m_socket(ARCH->copySocket(socket)), | 
|---|
| 60 | m_readable(readable), | 
|---|
| 61 | m_writable(writable) | 
|---|
| 62 | { | 
|---|
| 63 | // do nothing | 
|---|
| 64 | } | 
|---|
| 65 |  | 
|---|
| 66 | template <class T> | 
|---|
| 67 | inline | 
|---|
| 68 | TSocketMultiplexerMethodJob<T>::~TSocketMultiplexerMethodJob() | 
|---|
| 69 | { | 
|---|
| 70 | ARCH->closeSocket(m_socket); | 
|---|
| 71 | } | 
|---|
| 72 |  | 
|---|
| 73 | template <class T> | 
|---|
| 74 | inline | 
|---|
| 75 | ISocketMultiplexerJob* | 
|---|
| 76 | TSocketMultiplexerMethodJob<T>::run(bool read, bool write, bool error) | 
|---|
| 77 | { | 
|---|
| 78 | if (m_object != NULL) { | 
|---|
| 79 | return (m_object->*m_method)(this, read, write, error); | 
|---|
| 80 | } | 
|---|
| 81 | return NULL; | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | template <class T> | 
|---|
| 85 | inline | 
|---|
| 86 | CArchSocket | 
|---|
| 87 | TSocketMultiplexerMethodJob<T>::getSocket() const | 
|---|
| 88 | { | 
|---|
| 89 | return m_socket; | 
|---|
| 90 | } | 
|---|
| 91 |  | 
|---|
| 92 | template <class T> | 
|---|
| 93 | inline | 
|---|
| 94 | bool | 
|---|
| 95 | TSocketMultiplexerMethodJob<T>::isReadable() const | 
|---|
| 96 | { | 
|---|
| 97 | return m_readable; | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | template <class T> | 
|---|
| 101 | inline | 
|---|
| 102 | bool | 
|---|
| 103 | TSocketMultiplexerMethodJob<T>::isWritable() const | 
|---|
| 104 | { | 
|---|
| 105 | return m_writable; | 
|---|
| 106 | } | 
|---|
| 107 |  | 
|---|
| 108 | #endif | 
|---|