2012-06-10 16:50:54 +00:00
|
|
|
/*
|
2024-09-17 19:00:25 +00:00
|
|
|
* Deskflow -- mouse and keyboard sharing utility
|
2025-01-24 00:11:17 +00:00
|
|
|
* SPDX-FileCopyrightText: (C) 2012 - 2016 Symless Ltd.
|
|
|
|
|
* SPDX-FileCopyrightText: (C) 2004 Chris Schoeneman
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
2012-06-10 16:50:54 +00:00
|
|
|
*/
|
|
|
|
|
|
2014-02-28 12:36:45 +00:00
|
|
|
#pragma once
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2014-02-28 12:36:45 +00:00
|
|
|
#include "arch/IArchNetwork.h"
|
2025-03-19 22:21:22 +00:00
|
|
|
#include "common/StdList.h"
|
2025-03-19 22:23:25 +00:00
|
|
|
#include "common/StdMap.h"
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
template <class T> class CondVar;
|
2014-11-11 13:51:47 +00:00
|
|
|
class Mutex;
|
|
|
|
|
class Thread;
|
2012-06-10 16:50:54 +00:00
|
|
|
class ISocket;
|
|
|
|
|
class ISocketMultiplexerJob;
|
|
|
|
|
|
|
|
|
|
//! Socket multiplexer
|
|
|
|
|
/*!
|
|
|
|
|
A socket multiplexer services multiple sockets simultaneously.
|
|
|
|
|
*/
|
2024-10-17 18:04:35 +00:00
|
|
|
class SocketMultiplexer
|
|
|
|
|
{
|
2012-06-10 16:50:54 +00:00
|
|
|
public:
|
2024-07-02 19:07:06 +00:00
|
|
|
SocketMultiplexer();
|
|
|
|
|
SocketMultiplexer(SocketMultiplexer const &) = delete;
|
|
|
|
|
SocketMultiplexer(SocketMultiplexer &&) = delete;
|
|
|
|
|
~SocketMultiplexer();
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
SocketMultiplexer &operator=(SocketMultiplexer const &) = delete;
|
|
|
|
|
SocketMultiplexer &operator=(SocketMultiplexer &&) = delete;
|
2020-11-03 10:52:56 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
//! @name manipulators
|
|
|
|
|
//@{
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
void addSocket(ISocket *, ISocketMultiplexerJob *);
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
void removeSocket(ISocket *);
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
//@}
|
|
|
|
|
//! @name accessors
|
|
|
|
|
//@{
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// maybe belongs on ISocketMultiplexer
|
|
|
|
|
static SocketMultiplexer *getInstance();
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
//@}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
private:
|
2024-07-02 19:07:06 +00:00
|
|
|
// list of jobs. we use a list so we can safely iterate over it
|
|
|
|
|
// while other threads modify it.
|
2024-11-27 16:51:48 +00:00
|
|
|
using SocketJobs = std::list<ISocketMultiplexerJob *>;
|
|
|
|
|
using JobCursor = SocketJobs::iterator;
|
|
|
|
|
using SocketJobMap = std::map<ISocket *, JobCursor>;
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
// service sockets. the service thread will only access m_sockets
|
|
|
|
|
// and m_update while m_pollable and m_polling are true. all other
|
|
|
|
|
// threads must only modify these when m_pollable and m_polling are
|
|
|
|
|
// false. only the service thread sets m_polling.
|
|
|
|
|
void serviceThread(void *);
|
|
|
|
|
|
|
|
|
|
// create, iterate, and destroy a cursor. a cursor is used to
|
|
|
|
|
// safely iterate through the job list while other threads modify
|
|
|
|
|
// the list. it works by inserting a dummy item in the list and
|
|
|
|
|
// moving that item through the list. the dummy item will never
|
|
|
|
|
// be removed by other edits so an iterator pointing at the item
|
|
|
|
|
// remains valid until we remove the dummy item in deleteCursor().
|
|
|
|
|
// nextCursor() finds the next non-dummy item, moves our dummy
|
|
|
|
|
// item just past it, and returns an iterator for the non-dummy
|
|
|
|
|
// item. all cursor calls lock the mutex for their duration.
|
|
|
|
|
JobCursor newCursor();
|
|
|
|
|
JobCursor nextCursor(JobCursor);
|
|
|
|
|
void deleteCursor(JobCursor);
|
|
|
|
|
|
|
|
|
|
// lock out locking the job list. this blocks if another thread
|
|
|
|
|
// has already locked out locking. once it returns, only the
|
|
|
|
|
// calling thread will be able to lock the job list after any
|
|
|
|
|
// current lock is released.
|
|
|
|
|
void lockJobListLock();
|
|
|
|
|
|
|
|
|
|
// lock the job list. this blocks if the job list is already
|
|
|
|
|
// locked. the calling thread must have called requestJobLock.
|
|
|
|
|
void lockJobList();
|
|
|
|
|
|
|
|
|
|
// unlock the job list and the lock out on locking.
|
|
|
|
|
void unlockJobList();
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
private:
|
2024-07-02 19:07:06 +00:00
|
|
|
Mutex *m_mutex;
|
|
|
|
|
Thread *m_thread;
|
|
|
|
|
bool m_update;
|
|
|
|
|
CondVar<bool> *m_jobsReady;
|
|
|
|
|
CondVar<bool> *m_jobListLock;
|
|
|
|
|
CondVar<bool> *m_jobListLockLocked;
|
|
|
|
|
Thread *m_jobListLocker;
|
|
|
|
|
Thread *m_jobListLockLocker;
|
|
|
|
|
|
|
|
|
|
SocketJobs m_socketJobs;
|
|
|
|
|
SocketJobMap m_socketJobMap;
|
|
|
|
|
ISocketMultiplexerJob *m_cursorMark;
|
2012-06-10 16:50:54 +00:00
|
|
|
};
|