| 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 CSOCKETMULTIPLEXER_H | 
|---|
| 16 | #define CSOCKETMULTIPLEXER_H | 
|---|
| 17 |  | 
|---|
| 18 | #include "IArchNetwork.h" | 
|---|
| 19 | #include "stdlist.h" | 
|---|
| 20 | #include "stdmap.h" | 
|---|
| 21 |  | 
|---|
| 22 | template <class T> | 
|---|
| 23 | class CCondVar; | 
|---|
| 24 | class CMutex; | 
|---|
| 25 | class CThread; | 
|---|
| 26 | class ISocket; | 
|---|
| 27 | class ISocketMultiplexerJob; | 
|---|
| 28 |  | 
|---|
| 29 | //! Socket multiplexer | 
|---|
| 30 | /*! | 
|---|
| 31 | A socket multiplexer services multiple sockets simultaneously. | 
|---|
| 32 | */ | 
|---|
| 33 | class CSocketMultiplexer { | 
|---|
| 34 | public: | 
|---|
| 35 | CSocketMultiplexer(); | 
|---|
| 36 | ~CSocketMultiplexer(); | 
|---|
| 37 |  | 
|---|
| 38 | //! @name manipulators | 
|---|
| 39 | //@{ | 
|---|
| 40 |  | 
|---|
| 41 | void                            addSocket(ISocket*, ISocketMultiplexerJob*); | 
|---|
| 42 |  | 
|---|
| 43 | void                            removeSocket(ISocket*); | 
|---|
| 44 |  | 
|---|
| 45 | //@} | 
|---|
| 46 | //! @name accessors | 
|---|
| 47 | //@{ | 
|---|
| 48 |  | 
|---|
| 49 | // maybe belongs on ISocketMultiplexer | 
|---|
| 50 | static CSocketMultiplexer* | 
|---|
| 51 | getInstance(); | 
|---|
| 52 |  | 
|---|
| 53 | //@} | 
|---|
| 54 |  | 
|---|
| 55 | private: | 
|---|
| 56 | // list of jobs.  we use a list so we can safely iterate over it | 
|---|
| 57 | // while other threads modify it. | 
|---|
| 58 | typedef std::list<ISocketMultiplexerJob*> CSocketJobs; | 
|---|
| 59 | typedef CSocketJobs::iterator CJobCursor; | 
|---|
| 60 | typedef std::map<ISocket*, CJobCursor> CSocketJobMap; | 
|---|
| 61 |  | 
|---|
| 62 | // service sockets.  the service thread will only access m_sockets | 
|---|
| 63 | // and m_update while m_pollable and m_polling are true.  all other | 
|---|
| 64 | // threads must only modify these when m_pollable and m_polling are | 
|---|
| 65 | // false.  only the service thread sets m_polling. | 
|---|
| 66 | void                            serviceThread(void*); | 
|---|
| 67 |  | 
|---|
| 68 | // create, iterate, and destroy a cursor.  a cursor is used to | 
|---|
| 69 | // safely iterate through the job list while other threads modify | 
|---|
| 70 | // the list.  it works by inserting a dummy item in the list and | 
|---|
| 71 | // moving that item through the list.  the dummy item will never | 
|---|
| 72 | // be removed by other edits so an iterator pointing at the item | 
|---|
| 73 | // remains valid until we remove the dummy item in deleteCursor(). | 
|---|
| 74 | // nextCursor() finds the next non-dummy item, moves our dummy | 
|---|
| 75 | // item just past it, and returns an iterator for the non-dummy | 
|---|
| 76 | // item.  all cursor calls lock the mutex for their duration. | 
|---|
| 77 | CJobCursor                      newCursor(); | 
|---|
| 78 | CJobCursor                      nextCursor(CJobCursor); | 
|---|
| 79 | void                            deleteCursor(CJobCursor); | 
|---|
| 80 |  | 
|---|
| 81 | // lock out locking the job list.  this blocks if another thread | 
|---|
| 82 | // has already locked out locking.  once it returns, only the | 
|---|
| 83 | // calling thread will be able to lock the job list after any | 
|---|
| 84 | // current lock is released. | 
|---|
| 85 | void                            lockJobListLock(); | 
|---|
| 86 |  | 
|---|
| 87 | // lock the job list.  this blocks if the job list is already | 
|---|
| 88 | // locked.  the calling thread must have called requestJobLock. | 
|---|
| 89 | void                            lockJobList(); | 
|---|
| 90 |  | 
|---|
| 91 | // unlock the job list and the lock out on locking. | 
|---|
| 92 | void                            unlockJobList(); | 
|---|
| 93 |  | 
|---|
| 94 | private: | 
|---|
| 95 | CMutex*                         m_mutex; | 
|---|
| 96 | CThread*                        m_thread; | 
|---|
| 97 | bool                            m_update; | 
|---|
| 98 | CCondVar<bool>*         m_jobsReady; | 
|---|
| 99 | CCondVar<bool>*         m_jobListLock; | 
|---|
| 100 | CCondVar<bool>*         m_jobListLockLocked; | 
|---|
| 101 | CThread*                        m_jobListLocker; | 
|---|
| 102 | CThread*                        m_jobListLockLocker; | 
|---|
| 103 |  | 
|---|
| 104 | CSocketJobs                     m_socketJobs; | 
|---|
| 105 | CSocketJobMap           m_socketJobMap; | 
|---|
| 106 | ISocketMultiplexerJob*  m_cursorMark; | 
|---|
| 107 |  | 
|---|
| 108 | static CSocketMultiplexer*      s_instance; | 
|---|
| 109 | }; | 
|---|
| 110 |  | 
|---|
| 111 | #endif | 
|---|