2001-10-06 18:13:28 +04:00
|
|
|
#include "CServer.h"
|
|
|
|
#include "CInputPacketStream.h"
|
|
|
|
#include "COutputPacketStream.h"
|
|
|
|
#include "CServerProtocol.h"
|
|
|
|
#include "CProtocolUtil.h"
|
|
|
|
#include "IPrimaryScreen.h"
|
|
|
|
#include "ISocketFactory.h"
|
|
|
|
#include "ProtocolTypes.h"
|
|
|
|
#include "XSynergy.h"
|
|
|
|
#include "CNetworkAddress.h"
|
|
|
|
#include "ISocket.h"
|
|
|
|
#include "IListenSocket.h"
|
|
|
|
#include "XSocket.h"
|
|
|
|
#include "CLock.h"
|
|
|
|
#include "CThread.h"
|
|
|
|
#include "CTimerThread.h"
|
|
|
|
#include "CStopwatch.h"
|
|
|
|
#include "TMethodJob.h"
|
2001-10-14 18:37:41 +04:00
|
|
|
#include "CLog.h"
|
2001-10-06 18:13:28 +04:00
|
|
|
#include <assert.h>
|
|
|
|
#include <memory>
|
|
|
|
|
2001-11-19 03:33:36 +03:00
|
|
|
// hack to work around operator=() bug in STL in g++ prior to v3
|
|
|
|
#if defined(__GNUC__) && (__GNUC__ < 3)
|
|
|
|
#define assign(_dst, _src, _type) _dst.reset(_src)
|
|
|
|
#else
|
|
|
|
#define assign(_dst, _src, _type) _dst = std::auto_ptr<_type >(_src)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2001-10-21 04:21:02 +04:00
|
|
|
/* XXX
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
if (fork() == 0) abort();
|
|
|
|
else { wait(0); exit(1); }
|
|
|
|
*/
|
|
|
|
|
2001-10-06 18:13:28 +04:00
|
|
|
//
|
|
|
|
// CServer
|
|
|
|
//
|
|
|
|
|
2001-11-19 03:33:36 +03:00
|
|
|
CServer::CServer() : m_primary(NULL),
|
2001-10-25 03:29:29 +04:00
|
|
|
m_active(NULL),
|
|
|
|
m_primaryInfo(NULL)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
m_socketFactory = NULL;
|
|
|
|
m_securityFactory = NULL;
|
|
|
|
m_bindTimeout = 5.0 * 60.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
CServer::~CServer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServer::run()
|
|
|
|
{
|
|
|
|
try {
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_NOTE "starting server"));
|
|
|
|
|
2001-10-06 18:13:28 +04:00
|
|
|
// connect to primary screen
|
|
|
|
openPrimaryScreen();
|
|
|
|
|
|
|
|
// start listening for new clients
|
|
|
|
CThread(new TMethodJob<CServer>(this, &CServer::acceptClients));
|
|
|
|
|
|
|
|
// start listening for configuration connections
|
|
|
|
// FIXME
|
|
|
|
|
2001-11-19 03:33:36 +03:00
|
|
|
// handle events
|
|
|
|
log((CLOG_DEBUG "starting event handling"));
|
|
|
|
m_primary->run();
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
// clean up
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "stopping server"));
|
2001-10-06 18:13:28 +04:00
|
|
|
cleanupThreads();
|
2001-11-19 03:33:36 +03:00
|
|
|
closePrimaryScreen();
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
catch (XBase& e) {
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_ERR "server error: %s\n", e.what()));
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
// clean up
|
|
|
|
cleanupThreads();
|
2001-11-19 03:33:36 +03:00
|
|
|
closePrimaryScreen();
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
catch (...) {
|
2001-11-19 03:33:36 +03:00
|
|
|
log((CLOG_DEBUG "unknown server error"));
|
2001-10-14 18:37:41 +04:00
|
|
|
|
2001-10-06 18:13:28 +04:00
|
|
|
// clean up
|
|
|
|
cleanupThreads();
|
2001-11-19 03:33:36 +03:00
|
|
|
closePrimaryScreen();
|
2001-10-06 18:13:28 +04:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServer::setScreenMap(const CScreenMap& screenMap)
|
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
// FIXME -- must disconnect screens no longer listed
|
|
|
|
// (that may include warping back to server's screen)
|
|
|
|
// FIXME -- server screen must be in new map or map is rejected
|
|
|
|
m_screenMap = screenMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServer::getScreenMap(CScreenMap* screenMap) const
|
|
|
|
{
|
|
|
|
assert(screenMap != NULL);
|
|
|
|
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
*screenMap = m_screenMap;
|
|
|
|
}
|
|
|
|
|
2001-11-19 03:33:36 +03:00
|
|
|
UInt32 CServer::getActivePrimarySides() const
|
|
|
|
{
|
|
|
|
UInt32 sides = 0;
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
if (!m_screenMap.getNeighbor("primary", CScreenMap::kLeft).empty())
|
|
|
|
sides |= CScreenMap::kLeftMask;
|
|
|
|
if (!m_screenMap.getNeighbor("primary", CScreenMap::kRight).empty())
|
|
|
|
sides |= CScreenMap::kRightMask;
|
|
|
|
if (!m_screenMap.getNeighbor("primary", CScreenMap::kTop).empty())
|
|
|
|
sides |= CScreenMap::kTopMask;
|
|
|
|
if (!m_screenMap.getNeighbor("primary", CScreenMap::kBottom).empty())
|
|
|
|
sides |= CScreenMap::kBottomMask;
|
|
|
|
return sides;
|
|
|
|
}
|
|
|
|
|
2001-10-06 18:13:28 +04:00
|
|
|
void CServer::setInfo(const CString& client,
|
2001-10-14 20:58:01 +04:00
|
|
|
SInt32 w, SInt32 h, SInt32 zoneSize)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
|
|
|
|
// client must be connected
|
|
|
|
CScreenList::iterator index = m_screens.find(client);
|
|
|
|
if (index == m_screens.end()) {
|
|
|
|
throw XBadClient();
|
|
|
|
}
|
|
|
|
|
|
|
|
// update client info
|
|
|
|
CScreenInfo* info = index->second;
|
|
|
|
if (info == m_active) {
|
|
|
|
// FIXME -- ensure mouse is still on screen. warp it if necessary.
|
|
|
|
}
|
|
|
|
info->m_width = w;
|
|
|
|
info->m_height = h;
|
|
|
|
info->m_zoneSize = zoneSize;
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_NOTE "client \"%s\" size=%dx%d zone=%d", client.c_str(), w, h, zoneSize));
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CServer::onCommandKey(KeyID /*id*/,
|
|
|
|
KeyModifierMask /*mask*/, bool /*down*/)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServer::onKeyDown(KeyID id, KeyModifierMask mask)
|
|
|
|
{
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "onKeyDown id=%d mask=0x%04x", id, mask));
|
2001-10-06 18:13:28 +04:00
|
|
|
assert(m_active != NULL);
|
|
|
|
|
|
|
|
// handle command keys
|
|
|
|
if (onCommandKey(id, mask, true)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// relay
|
|
|
|
if (m_active->m_protocol != NULL) {
|
|
|
|
m_active->m_protocol->sendKeyDown(id, mask);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServer::onKeyUp(KeyID id, KeyModifierMask mask)
|
|
|
|
{
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "onKeyUp id=%d mask=0x%04x", id, mask));
|
2001-10-06 18:13:28 +04:00
|
|
|
assert(m_active != NULL);
|
|
|
|
|
|
|
|
// handle command keys
|
|
|
|
if (onCommandKey(id, mask, false)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// relay
|
|
|
|
if (m_active->m_protocol != NULL) {
|
|
|
|
m_active->m_protocol->sendKeyUp(id, mask);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServer::onKeyRepeat(KeyID id, KeyModifierMask mask)
|
|
|
|
{
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "onKeyRepeat id=%d mask=0x%04x", id, mask));
|
2001-10-06 18:13:28 +04:00
|
|
|
assert(m_active != NULL);
|
|
|
|
|
|
|
|
// handle command keys
|
|
|
|
if (onCommandKey(id, mask, false)) {
|
|
|
|
onCommandKey(id, mask, true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// relay
|
|
|
|
if (m_active->m_protocol != NULL) {
|
|
|
|
m_active->m_protocol->sendKeyRepeat(id, mask);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServer::onMouseDown(ButtonID id)
|
|
|
|
{
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "onMouseDown id=%d", id));
|
2001-10-06 18:13:28 +04:00
|
|
|
assert(m_active != NULL);
|
|
|
|
|
|
|
|
// relay
|
|
|
|
if (m_active->m_protocol != NULL) {
|
|
|
|
m_active->m_protocol->sendMouseDown(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServer::onMouseUp(ButtonID id)
|
|
|
|
{
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "onMouseUp id=%d", id));
|
2001-10-06 18:13:28 +04:00
|
|
|
assert(m_active != NULL);
|
|
|
|
|
|
|
|
// relay
|
|
|
|
if (m_active->m_protocol != NULL) {
|
|
|
|
m_active->m_protocol->sendMouseUp(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-11-19 03:33:36 +03:00
|
|
|
bool CServer::onMouseMovePrimary(SInt32 x, SInt32 y)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "onMouseMovePrimary %d,%d", x, y));
|
|
|
|
|
2001-10-06 18:13:28 +04:00
|
|
|
// mouse move on primary (server's) screen
|
|
|
|
assert(m_active != NULL);
|
|
|
|
assert(m_active->m_protocol == NULL);
|
|
|
|
|
|
|
|
// ignore if mouse is locked to screen
|
|
|
|
if (isLockedToScreen()) {
|
2001-11-19 03:33:36 +03:00
|
|
|
return false;
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// see if we should change screens
|
|
|
|
CScreenMap::EDirection dir;
|
|
|
|
if (x < m_active->m_zoneSize) {
|
|
|
|
x -= m_active->m_zoneSize;
|
|
|
|
dir = CScreenMap::kLeft;
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "switch to left"));
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
else if (x >= m_active->m_width - m_active->m_zoneSize) {
|
|
|
|
x += m_active->m_zoneSize;
|
|
|
|
dir = CScreenMap::kRight;
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "switch to right"));
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
else if (y < m_active->m_zoneSize) {
|
|
|
|
y -= m_active->m_zoneSize;
|
|
|
|
dir = CScreenMap::kTop;
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "switch to top"));
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
else if (y >= m_active->m_height - m_active->m_zoneSize) {
|
|
|
|
y += m_active->m_zoneSize;
|
|
|
|
dir = CScreenMap::kBottom;
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "switch to bottom"));
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// still on local screen
|
2001-11-19 03:33:36 +03:00
|
|
|
return false;
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// get jump destination
|
|
|
|
CScreenInfo* newScreen = getNeighbor(m_active, dir, x, y);
|
|
|
|
|
|
|
|
// if no screen in jump direction then ignore the move
|
|
|
|
if (newScreen == NULL) {
|
2001-11-19 03:33:36 +03:00
|
|
|
return false;
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// remap position to account for resolution differences
|
|
|
|
mapPosition(m_active, dir, newScreen, x, y);
|
|
|
|
|
|
|
|
// switch screen
|
|
|
|
switchScreen(newScreen, x, y);
|
2001-11-19 03:33:36 +03:00
|
|
|
return true;
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void CServer::onMouseMoveSecondary(SInt32 dx, SInt32 dy)
|
|
|
|
{
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "onMouseMoveSecondary %+d,%+d", dx, dy));
|
|
|
|
|
2001-10-06 18:13:28 +04:00
|
|
|
// mouse move on secondary (client's) screen
|
|
|
|
assert(m_active != NULL);
|
|
|
|
assert(m_active->m_protocol != NULL);
|
|
|
|
|
|
|
|
// save old position
|
|
|
|
const SInt32 xOld = m_x;
|
|
|
|
const SInt32 yOld = m_y;
|
|
|
|
|
|
|
|
// accumulate motion
|
|
|
|
m_x += dx;
|
|
|
|
m_y += dy;
|
|
|
|
|
|
|
|
// switch screens if the mouse is outside the screen and not
|
|
|
|
// locked to the screen
|
|
|
|
CScreenInfo* newScreen = NULL;
|
|
|
|
if (!isLockedToScreen()) {
|
|
|
|
// find direction of neighbor
|
|
|
|
CScreenMap::EDirection dir;
|
|
|
|
if (m_x < 0)
|
|
|
|
dir = CScreenMap::kLeft;
|
|
|
|
else if (m_x > m_active->m_width - 1)
|
|
|
|
dir = CScreenMap::kRight;
|
|
|
|
else if (m_y < 0)
|
|
|
|
dir = CScreenMap::kTop;
|
|
|
|
else if (m_y > m_active->m_height - 1)
|
|
|
|
dir = CScreenMap::kBottom;
|
|
|
|
else
|
|
|
|
newScreen = m_active;
|
|
|
|
|
|
|
|
// get neighbor if we should switch
|
|
|
|
if (newScreen == NULL) {
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "leave \"%s\" on %s", m_active->m_name.c_str(), CScreenMap::dirName(dir)));
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
SInt32 x = m_x, y = m_y;
|
|
|
|
newScreen = getNeighbor(m_active, dir, x, y);
|
|
|
|
|
|
|
|
// remap position to account for resolution differences
|
|
|
|
if (newScreen != NULL) {
|
|
|
|
mapPosition(m_active, dir, newScreen, x, y);
|
|
|
|
m_x = x;
|
|
|
|
m_y = y;
|
|
|
|
}
|
|
|
|
else {
|
2001-10-24 01:13:08 +04:00
|
|
|
log((CLOG_DEBUG "no neighbor; clamping"));
|
2001-10-06 18:13:28 +04:00
|
|
|
if (m_x < 0)
|
|
|
|
m_x = 0;
|
|
|
|
else if (m_x > m_active->m_width - 1)
|
|
|
|
m_x = m_active->m_width - 1;
|
|
|
|
if (m_y < 0)
|
|
|
|
m_y = 0;
|
|
|
|
else if (m_y > m_active->m_height - 1)
|
|
|
|
m_y = m_active->m_height - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// clamp to edge when locked
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "clamp to \"%s\"", m_active->m_name.c_str()));
|
2001-10-06 18:13:28 +04:00
|
|
|
if (m_x < 0)
|
|
|
|
m_x = 0;
|
|
|
|
else if (m_x > m_active->m_width - 1)
|
|
|
|
m_x = m_active->m_width - 1;
|
|
|
|
if (m_y < 0)
|
|
|
|
m_y = 0;
|
|
|
|
else if (m_y > m_active->m_height - 1)
|
|
|
|
m_y = m_active->m_height - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// warp cursor if on same screen
|
|
|
|
if (newScreen == NULL || newScreen == m_active) {
|
|
|
|
// do nothing if mouse didn't move
|
|
|
|
if (m_x != xOld || m_y != yOld) {
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "move on %s to %d,%d", m_active->m_name.c_str(), m_x, m_y));
|
2001-10-06 18:13:28 +04:00
|
|
|
m_active->m_protocol->sendMouseMove(m_x, m_y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise screen screens
|
|
|
|
else {
|
|
|
|
switchScreen(newScreen, m_x, m_y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServer::onMouseWheel(SInt32 delta)
|
|
|
|
{
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "onMouseWheel %+d", delta));
|
2001-10-06 18:13:28 +04:00
|
|
|
assert(m_active != NULL);
|
|
|
|
|
|
|
|
// relay
|
|
|
|
if (m_active->m_protocol != NULL) {
|
|
|
|
m_active->m_protocol->sendMouseWheel(delta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CServer::isLockedToScreen() const
|
|
|
|
{
|
|
|
|
// FIXME
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2001-11-19 03:33:36 +03:00
|
|
|
#if defined(CONFIG_PLATFORM_WIN32)
|
|
|
|
#include "CMSWindowsClipboard.h" // FIXME
|
|
|
|
#elif defined(CONFIG_PLATFORM_UNIX)
|
2001-11-14 02:34:12 +03:00
|
|
|
#include "CXWindowsClipboard.h" // FIXME
|
2001-11-19 03:33:36 +03:00
|
|
|
#endif
|
2001-10-06 18:13:28 +04:00
|
|
|
void CServer::switchScreen(CScreenInfo* dst,
|
|
|
|
SInt32 x, SInt32 y)
|
|
|
|
{
|
|
|
|
assert(dst != NULL);
|
|
|
|
assert(x >= 0 && y >= 0 && x < dst->m_width && y < dst->m_height);
|
|
|
|
assert(m_active != NULL);
|
|
|
|
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_NOTE "switch from \"%s\" to \"%s\" at %d,%d", m_active->m_name.c_str(), dst->m_name.c_str(), x, y));
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
// wrapping means leaving the active screen and entering it again.
|
|
|
|
// since that's a waste of time we skip that and just warp the
|
|
|
|
// mouse.
|
|
|
|
if (m_active != dst) {
|
|
|
|
// leave active screen
|
|
|
|
if (m_active->m_protocol == NULL) {
|
|
|
|
m_primary->leave();
|
2001-11-14 02:34:12 +03:00
|
|
|
|
|
|
|
// FIXME -- testing
|
2001-11-19 03:33:36 +03:00
|
|
|
#if defined(CONFIG_PLATFORM_WIN32)
|
|
|
|
CMSWindowsClipboard clipboard;
|
|
|
|
#elif defined(CONFIG_PLATFORM_UNIX)
|
2001-11-14 02:34:12 +03:00
|
|
|
CXWindowsClipboard clipboard;
|
2001-11-19 03:33:36 +03:00
|
|
|
#endif
|
2001-11-14 02:34:12 +03:00
|
|
|
m_primary->getClipboard(&clipboard);
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
m_active->m_protocol->sendLeave();
|
|
|
|
}
|
|
|
|
|
|
|
|
// cut over
|
|
|
|
m_active = dst;
|
|
|
|
|
|
|
|
// enter new screen
|
|
|
|
if (m_active->m_protocol == NULL) {
|
|
|
|
m_primary->enter(x, y);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
m_active->m_protocol->sendEnter(x, y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (m_active->m_protocol == NULL) {
|
|
|
|
m_primary->warpCursor(x, y);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
m_active->m_protocol->sendMouseMove(x, y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// record new position
|
|
|
|
m_x = x;
|
|
|
|
m_y = y;
|
|
|
|
}
|
|
|
|
|
|
|
|
CServer::CScreenInfo* CServer::getNeighbor(CScreenInfo* src,
|
|
|
|
CScreenMap::EDirection dir) const
|
|
|
|
{
|
|
|
|
assert(src != NULL);
|
|
|
|
|
|
|
|
CString srcName = src->m_name;
|
|
|
|
assert(!srcName.empty());
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "find neighbor on %s of \"%s\"", CScreenMap::dirName(dir), srcName.c_str()));
|
2001-10-06 18:13:28 +04:00
|
|
|
for (;;) {
|
|
|
|
// look up name of neighbor
|
|
|
|
const CString dstName(m_screenMap.getNeighbor(srcName, dir));
|
|
|
|
|
|
|
|
// if nothing in that direction then return NULL
|
2001-10-14 18:37:41 +04:00
|
|
|
if (dstName.empty()) {
|
|
|
|
log((CLOG_DEBUG "no neighbor on %s of \"%s\"", CScreenMap::dirName(dir), srcName.c_str()));
|
2001-10-06 18:13:28 +04:00
|
|
|
return NULL;
|
2001-10-14 18:37:41 +04:00
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
// look up neighbor cell. if the screen is connected then
|
|
|
|
// we can stop. otherwise we skip over an unconnected
|
|
|
|
// screen.
|
|
|
|
CScreenList::const_iterator index = m_screens.find(dstName);
|
|
|
|
if (index != m_screens.end()) {
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "\"%s\" is on %s of \"%s\"", dstName.c_str(), CScreenMap::dirName(dir), srcName.c_str()));
|
2001-10-06 18:13:28 +04:00
|
|
|
return index->second;
|
|
|
|
}
|
|
|
|
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "ignored \"%s\" on %s of \"%s\"", dstName.c_str(), CScreenMap::dirName(dir), srcName.c_str()));
|
2001-10-06 18:13:28 +04:00
|
|
|
srcName = dstName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CServer::CScreenInfo* CServer::getNeighbor(CScreenInfo* src,
|
|
|
|
CScreenMap::EDirection srcSide,
|
|
|
|
SInt32& x, SInt32& y) const
|
|
|
|
{
|
|
|
|
assert(src != NULL);
|
|
|
|
|
|
|
|
// get the first neighbor
|
2001-10-24 01:13:08 +04:00
|
|
|
CScreenInfo* lastGoodScreen = src;
|
2001-10-06 18:13:28 +04:00
|
|
|
CScreenInfo* dst = getNeighbor(src, srcSide);
|
|
|
|
|
|
|
|
// get the source screen's size (needed for kRight and kBottom)
|
|
|
|
SInt32 w = src->m_width, h = src->m_height;
|
|
|
|
|
|
|
|
// find destination screen, adjusting x or y (but not both)
|
|
|
|
switch (srcSide) {
|
|
|
|
case CScreenMap::kLeft:
|
|
|
|
while (dst != NULL) {
|
|
|
|
lastGoodScreen = dst;
|
|
|
|
w = lastGoodScreen->m_width;
|
|
|
|
h = lastGoodScreen->m_height;
|
|
|
|
x += w;
|
|
|
|
if (x >= 0) {
|
|
|
|
break;
|
|
|
|
}
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "skipping over screen %s", dst->m_name.c_str()));
|
2001-10-06 18:13:28 +04:00
|
|
|
dst = getNeighbor(lastGoodScreen, srcSide);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CScreenMap::kRight:
|
|
|
|
while (dst != NULL) {
|
|
|
|
lastGoodScreen = dst;
|
|
|
|
x -= w;
|
|
|
|
w = lastGoodScreen->m_width;
|
|
|
|
h = lastGoodScreen->m_height;
|
|
|
|
if (x < w) {
|
|
|
|
break;
|
|
|
|
}
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "skipping over screen %s", dst->m_name.c_str()));
|
2001-10-06 18:13:28 +04:00
|
|
|
dst = getNeighbor(lastGoodScreen, srcSide);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CScreenMap::kTop:
|
|
|
|
while (dst != NULL) {
|
|
|
|
lastGoodScreen = dst;
|
|
|
|
w = lastGoodScreen->m_width;
|
|
|
|
h = lastGoodScreen->m_height;
|
|
|
|
y += h;
|
|
|
|
if (y >= 0) {
|
|
|
|
break;
|
|
|
|
}
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "skipping over screen %s", dst->m_name.c_str()));
|
2001-10-06 18:13:28 +04:00
|
|
|
dst = getNeighbor(lastGoodScreen, srcSide);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CScreenMap::kBottom:
|
|
|
|
while (dst != NULL) {
|
|
|
|
lastGoodScreen = dst;
|
|
|
|
y -= h;
|
|
|
|
w = lastGoodScreen->m_width;
|
|
|
|
h = lastGoodScreen->m_height;
|
|
|
|
if (y < h) {
|
|
|
|
break;
|
|
|
|
}
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "skipping over screen %s", dst->m_name.c_str()));
|
2001-10-06 18:13:28 +04:00
|
|
|
dst = getNeighbor(lastGoodScreen, srcSide);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2001-10-24 01:13:08 +04:00
|
|
|
assert(lastGoodScreen != NULL);
|
|
|
|
|
|
|
|
// no neighbor if best neighbor is the source itself
|
|
|
|
if (lastGoodScreen == src)
|
|
|
|
return NULL;
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
// if entering primary screen then be sure to move in far enough
|
|
|
|
// to avoid the jump zone. if entering a side that doesn't have
|
|
|
|
// a neighbor (i.e. an asymmetrical side) then we don't need to
|
|
|
|
// move inwards because that side can't provoke a jump.
|
|
|
|
if (lastGoodScreen->m_protocol == NULL) {
|
|
|
|
const CString dstName(lastGoodScreen->m_name);
|
|
|
|
switch (srcSide) {
|
|
|
|
case CScreenMap::kLeft:
|
|
|
|
if (!m_screenMap.getNeighbor(dstName, CScreenMap::kRight).empty() &&
|
|
|
|
x > w - 1 - lastGoodScreen->m_zoneSize)
|
|
|
|
x = w - 1 - lastGoodScreen->m_zoneSize;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CScreenMap::kRight:
|
|
|
|
if (!m_screenMap.getNeighbor(dstName, CScreenMap::kLeft).empty() &&
|
|
|
|
x < lastGoodScreen->m_zoneSize)
|
|
|
|
x = lastGoodScreen->m_zoneSize;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CScreenMap::kTop:
|
|
|
|
if (!m_screenMap.getNeighbor(dstName, CScreenMap::kBottom).empty() &&
|
|
|
|
y > h - 1 - lastGoodScreen->m_zoneSize)
|
|
|
|
y = h - 1 - lastGoodScreen->m_zoneSize;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CScreenMap::kBottom:
|
|
|
|
if (!m_screenMap.getNeighbor(dstName, CScreenMap::kTop).empty() &&
|
|
|
|
y < lastGoodScreen->m_zoneSize)
|
|
|
|
y = lastGoodScreen->m_zoneSize;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return lastGoodScreen;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServer::mapPosition(CScreenInfo* src,
|
|
|
|
CScreenMap::EDirection srcSide,
|
|
|
|
CScreenInfo* dst,
|
|
|
|
SInt32& x, SInt32& y) const
|
|
|
|
{
|
|
|
|
assert(src != NULL);
|
|
|
|
assert(dst != NULL);
|
|
|
|
assert(srcSide >= CScreenMap::kFirstDirection &&
|
|
|
|
srcSide <= CScreenMap::kLastDirection);
|
|
|
|
|
|
|
|
switch (srcSide) {
|
|
|
|
case CScreenMap::kLeft:
|
|
|
|
case CScreenMap::kRight:
|
2001-10-25 03:29:29 +04:00
|
|
|
if (y < 0)
|
|
|
|
y = 0;
|
|
|
|
else if (y >= src->m_height)
|
2001-11-12 00:27:36 +03:00
|
|
|
y = dst->m_height - 1;
|
2001-10-25 03:29:29 +04:00
|
|
|
else
|
|
|
|
y = static_cast<SInt32>(0.5 + y *
|
2001-10-06 18:13:28 +04:00
|
|
|
static_cast<double>(dst->m_height - 1) /
|
|
|
|
(src->m_height - 1));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CScreenMap::kTop:
|
|
|
|
case CScreenMap::kBottom:
|
2001-10-25 03:29:29 +04:00
|
|
|
if (x < 0)
|
|
|
|
x = 0;
|
|
|
|
else if (x >= src->m_width)
|
2001-11-12 00:27:36 +03:00
|
|
|
x = dst->m_width - 1;
|
2001-10-25 03:29:29 +04:00
|
|
|
else
|
|
|
|
x = static_cast<SInt32>(0.5 + x *
|
2001-10-06 18:13:28 +04:00
|
|
|
static_cast<double>(dst->m_width - 1) /
|
|
|
|
(src->m_width - 1));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-10-08 23:24:46 +04:00
|
|
|
#include "CTCPListenSocket.h"
|
2001-10-06 18:13:28 +04:00
|
|
|
void CServer::acceptClients(void*)
|
|
|
|
{
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "starting to wait for clients"));
|
|
|
|
|
2001-10-06 18:13:28 +04:00
|
|
|
// add this thread to the list of threads to cancel. remove from
|
|
|
|
// list in d'tor.
|
|
|
|
CCleanupNote cleanupNote(this);
|
|
|
|
|
|
|
|
std::auto_ptr<IListenSocket> listen;
|
|
|
|
try {
|
|
|
|
// create socket listener
|
2001-11-19 03:33:36 +03:00
|
|
|
// listen = std::auto_ptr<IListenSocket>(m_socketFactory->createListen());
|
|
|
|
assign(listen, new CTCPListenSocket, IListenSocket); // FIXME
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
// bind to the desired port. keep retrying if we can't bind
|
|
|
|
// the address immediately.
|
|
|
|
CStopwatch timer;
|
|
|
|
CNetworkAddress addr(50001 /* FIXME -- m_port */);
|
|
|
|
for (;;) {
|
|
|
|
try {
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "binding listen socket"));
|
2001-10-06 18:13:28 +04:00
|
|
|
listen->bind(addr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
catch (XSocketAddressInUse&) {
|
|
|
|
// give up if we've waited too long
|
|
|
|
if (timer.getTime() >= m_bindTimeout) {
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "waited too long to bind, giving up"));
|
2001-10-06 18:13:28 +04:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait a bit before retrying
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "bind failed; waiting to retry"));
|
2001-10-06 18:13:28 +04:00
|
|
|
CThread::sleep(5.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// accept connections and begin processing them
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "waiting for client connections"));
|
2001-10-06 18:13:28 +04:00
|
|
|
for (;;) {
|
|
|
|
// accept connection
|
|
|
|
CThread::testCancel();
|
|
|
|
ISocket* socket = listen->accept();
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_NOTE "accepted client connection"));
|
2001-10-06 18:13:28 +04:00
|
|
|
CThread::testCancel();
|
|
|
|
|
|
|
|
// start handshake thread
|
|
|
|
CThread(new TMethodJob<CServer>(
|
|
|
|
this, &CServer::handshakeClient, socket));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (XBase& e) {
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_ERR "cannot listen for clients: %s", e.what()));
|
2001-10-06 18:13:28 +04:00
|
|
|
quit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServer::handshakeClient(void* vsocket)
|
|
|
|
{
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "negotiating with new client"));
|
|
|
|
|
2001-10-06 18:13:28 +04:00
|
|
|
// get the socket pointer from the argument
|
|
|
|
assert(vsocket != NULL);
|
|
|
|
std::auto_ptr<ISocket> socket(reinterpret_cast<ISocket*>(vsocket));
|
|
|
|
|
|
|
|
// add this thread to the list of threads to cancel. remove from
|
|
|
|
// list in d'tor.
|
|
|
|
CCleanupNote cleanupNote(this);
|
|
|
|
|
|
|
|
CString name("<unknown>");
|
|
|
|
try {
|
|
|
|
// get the input and output streams
|
|
|
|
IInputStream* srcInput = socket->getInputStream();
|
|
|
|
IOutputStream* srcOutput = socket->getOutputStream();
|
|
|
|
std::auto_ptr<IInputStream> input;
|
|
|
|
std::auto_ptr<IOutputStream> output;
|
|
|
|
|
|
|
|
// attach the encryption layer
|
2001-10-21 04:21:02 +04:00
|
|
|
bool own = false;
|
2001-10-06 18:13:28 +04:00
|
|
|
if (m_securityFactory != NULL) {
|
|
|
|
/* FIXME -- implement ISecurityFactory
|
2001-10-21 04:21:02 +04:00
|
|
|
input.reset(m_securityFactory->createInputFilter(srcInput, own));
|
|
|
|
output.reset(m_securityFactory->createOutputFilter(srcOutput, own));
|
2001-10-06 18:13:28 +04:00
|
|
|
srcInput = input.get();
|
|
|
|
srcOutput = output.get();
|
2001-10-21 04:21:02 +04:00
|
|
|
own = true;
|
2001-10-06 18:13:28 +04:00
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
// attach the packetizing filters
|
2001-11-19 03:33:36 +03:00
|
|
|
assign(input, new CInputPacketStream(srcInput, own), IInputStream);
|
|
|
|
assign(output, new COutputPacketStream(srcOutput, own), IOutputStream);
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
std::auto_ptr<IServerProtocol> protocol;
|
|
|
|
std::auto_ptr<CConnectionNote> connectedNote;
|
|
|
|
{
|
|
|
|
// give the client a limited time to complete the handshake
|
|
|
|
CTimerThread timer(30.0);
|
|
|
|
|
|
|
|
// limit the maximum length of the hello
|
|
|
|
static const UInt32 maxHelloLen = 1024;
|
|
|
|
|
|
|
|
// say hello
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "saying hello"));
|
2001-10-06 18:13:28 +04:00
|
|
|
CProtocolUtil::writef(output.get(), "Synergy%2i%2i",
|
|
|
|
kMajorVersion, kMinorVersion);
|
|
|
|
output->flush();
|
|
|
|
|
|
|
|
// wait for the reply
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "waiting for hello reply"));
|
2001-10-06 18:13:28 +04:00
|
|
|
UInt32 n = input->getSize();
|
|
|
|
if (n > maxHelloLen) {
|
|
|
|
throw XBadClient();
|
|
|
|
}
|
|
|
|
|
|
|
|
// get and parse the reply to hello
|
|
|
|
SInt32 major, minor;
|
|
|
|
try {
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "parsing hello reply"));
|
2001-10-06 18:13:28 +04:00
|
|
|
CProtocolUtil::readf(input.get(), "Synergy%2i%2i%s",
|
|
|
|
&major, &minor, &name);
|
|
|
|
}
|
|
|
|
catch (XIO&) {
|
|
|
|
throw XBadClient();
|
|
|
|
}
|
|
|
|
if (major < 0 || minor < 0) {
|
|
|
|
throw XBadClient();
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a protocol interpreter for the version
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "creating interpreter for client %s version %d.%d", name.c_str(), major, minor));
|
2001-11-19 03:33:36 +03:00
|
|
|
assign(protocol, CServerProtocol::create(major, minor,
|
|
|
|
this, name, input.get(), output.get()),
|
|
|
|
IServerProtocol);
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
// client is now pending
|
2001-11-19 03:33:36 +03:00
|
|
|
assign(connectedNote, new CConnectionNote(this,
|
|
|
|
name, protocol.get()), CConnectionNote);
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
// ask and wait for the client's info
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "waiting for info for client %s", name.c_str()));
|
2001-10-06 18:13:28 +04:00
|
|
|
protocol->queryInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle messages from client. returns when the client
|
|
|
|
// disconnects.
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_NOTE "client %s is connected", name.c_str()));
|
2001-10-06 18:13:28 +04:00
|
|
|
protocol->run();
|
|
|
|
}
|
|
|
|
catch (XIncompatibleClient& e) {
|
|
|
|
// client is incompatible
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_WARN "client \"%s\" has incompatible version %d.%d)", name.c_str(), e.getMajor(), e.getMinor()));
|
2001-10-06 18:13:28 +04:00
|
|
|
// FIXME -- could print network address if socket had suitable method
|
|
|
|
}
|
|
|
|
catch (XBadClient&) {
|
|
|
|
// client not behaving
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_WARN "protocol error from client \"%s\"", name.c_str()));
|
2001-10-06 18:13:28 +04:00
|
|
|
// FIXME -- could print network address if socket had suitable method
|
|
|
|
}
|
|
|
|
catch (XBase& e) {
|
|
|
|
// misc error
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_WARN "error communicating with client \"%s\": %s", name.c_str(), e.what()));
|
2001-10-06 18:13:28 +04:00
|
|
|
// FIXME -- could print network address if socket had suitable method
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-10-14 20:58:01 +04:00
|
|
|
void CServer::quit()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2001-11-19 03:33:36 +03:00
|
|
|
m_primary->stop();
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME -- use factory to create screen
|
2001-11-19 03:33:36 +03:00
|
|
|
#if defined(CONFIG_PLATFORM_WIN32)
|
|
|
|
#include "CMSWindowsPrimaryScreen.h"
|
|
|
|
#elif defined(CONFIG_PLATFORM_UNIX)
|
2001-10-06 18:13:28 +04:00
|
|
|
#include "CXWindowsPrimaryScreen.h"
|
2001-11-19 03:33:36 +03:00
|
|
|
#endif
|
2001-10-06 18:13:28 +04:00
|
|
|
void CServer::openPrimaryScreen()
|
|
|
|
{
|
|
|
|
assert(m_primary == NULL);
|
|
|
|
|
|
|
|
// open screen
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "creating primary screen"));
|
2001-11-19 03:33:36 +03:00
|
|
|
#if defined(CONFIG_PLATFORM_WIN32)
|
|
|
|
m_primary = new CMSWindowsPrimaryScreen;
|
|
|
|
#elif defined(CONFIG_PLATFORM_UNIX)
|
2001-10-06 18:13:28 +04:00
|
|
|
m_primary = new CXWindowsPrimaryScreen;
|
2001-11-19 03:33:36 +03:00
|
|
|
#endif
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "opening primary screen"));
|
2001-10-06 18:13:28 +04:00
|
|
|
m_primary->open(this);
|
|
|
|
|
|
|
|
// add connection
|
2001-10-08 23:24:46 +04:00
|
|
|
m_active = addConnection(CString("primary"/* FIXME */), NULL);
|
2001-10-25 03:29:29 +04:00
|
|
|
m_primaryInfo = m_active;
|
2001-10-08 23:24:46 +04:00
|
|
|
|
|
|
|
// update info
|
|
|
|
m_primary->getSize(&m_active->m_width, &m_active->m_height);
|
|
|
|
m_active->m_zoneSize = m_primary->getJumpZoneSize();
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_NOTE "server size=%dx%d zone=%d", m_active->m_width, m_active->m_height, m_active->m_zoneSize));
|
2001-10-08 23:24:46 +04:00
|
|
|
// FIXME -- need way for primary screen to call us back
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2001-10-14 20:58:01 +04:00
|
|
|
void CServer::closePrimaryScreen()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
assert(m_primary != NULL);
|
|
|
|
|
|
|
|
// remove connection
|
|
|
|
removeConnection(CString("primary"/* FIXME */));
|
|
|
|
|
|
|
|
// close the primary screen
|
|
|
|
try {
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "closing primary screen"));
|
2001-10-06 18:13:28 +04:00
|
|
|
m_primary->close();
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
|
|
|
|
// clean up
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "destroying primary screen"));
|
2001-10-06 18:13:28 +04:00
|
|
|
delete m_primary;
|
|
|
|
m_primary = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServer::addCleanupThread(const CThread& thread)
|
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
m_cleanupList.insert(m_cleanupList.begin(), new CThread(thread));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServer::removeCleanupThread(const CThread& thread)
|
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
for (CThreadList::iterator index = m_cleanupList.begin();
|
|
|
|
index != m_cleanupList.end(); ++index) {
|
|
|
|
if (**index == thread) {
|
2001-11-19 03:33:36 +03:00
|
|
|
CThread* thread = *index;
|
2001-10-06 18:13:28 +04:00
|
|
|
m_cleanupList.erase(index);
|
2001-11-19 03:33:36 +03:00
|
|
|
delete thread;
|
2001-10-06 18:13:28 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-10-14 20:58:01 +04:00
|
|
|
void CServer::cleanupThreads()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "cleaning up threads"));
|
2001-10-06 18:13:28 +04:00
|
|
|
m_mutex.lock();
|
|
|
|
while (m_cleanupList.begin() != m_cleanupList.end()) {
|
|
|
|
// get the next thread and cancel it
|
|
|
|
CThread* thread = m_cleanupList.front();
|
|
|
|
thread->cancel();
|
|
|
|
|
|
|
|
// wait for thread to finish with cleanup list unlocked. the
|
|
|
|
// thread will remove itself from the cleanup list.
|
|
|
|
m_mutex.unlock();
|
|
|
|
thread->wait();
|
|
|
|
m_mutex.lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME -- delete remaining threads from list
|
|
|
|
m_mutex.unlock();
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "cleaned up threads"));
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
CServer::CScreenInfo* CServer::addConnection(
|
|
|
|
const CString& name, IServerProtocol* protocol)
|
|
|
|
{
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "adding connection \"%s\"", name.c_str()));
|
2001-10-06 18:13:28 +04:00
|
|
|
CLock lock(&m_mutex);
|
|
|
|
assert(m_screens.count(name) == 0);
|
|
|
|
CScreenInfo* newScreen = new CScreenInfo(name, protocol);
|
|
|
|
m_screens.insert(std::make_pair(name, newScreen));
|
|
|
|
return newScreen;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServer::removeConnection(const CString& name)
|
|
|
|
{
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "removing connection \"%s\"", name.c_str()));
|
2001-10-06 18:13:28 +04:00
|
|
|
CLock lock(&m_mutex);
|
2001-10-25 03:29:29 +04:00
|
|
|
|
|
|
|
// find screen info
|
2001-10-06 18:13:28 +04:00
|
|
|
CScreenList::iterator index = m_screens.find(name);
|
2001-10-14 20:58:01 +04:00
|
|
|
assert(index != m_screens.end());
|
2001-10-25 03:29:29 +04:00
|
|
|
|
|
|
|
// if this is active screen then we have to jump off of it
|
2001-11-19 03:33:36 +03:00
|
|
|
if (m_active == index->second && m_active != m_primaryInfo) {
|
2001-10-25 03:29:29 +04:00
|
|
|
// record new position (center of primary screen)
|
|
|
|
m_x = m_primaryInfo->m_width >> 1;
|
|
|
|
m_y = m_primaryInfo->m_height >> 1;
|
|
|
|
|
|
|
|
// don't notify active screen since it probably already disconnected
|
|
|
|
log((CLOG_NOTE "jump from \"%s\" to \"%s\" at %d,%d", m_active->m_name.c_str(), m_primaryInfo->m_name.c_str(), m_x, m_y));
|
|
|
|
|
|
|
|
// cut over
|
|
|
|
m_active = m_primaryInfo;
|
|
|
|
|
|
|
|
// enter new screen
|
|
|
|
m_primary->enter(m_x, m_y);
|
|
|
|
}
|
|
|
|
|
|
|
|
// done with screen info
|
2001-10-06 18:13:28 +04:00
|
|
|
delete index->second;
|
|
|
|
m_screens.erase(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// CServer::CCleanupNote
|
|
|
|
//
|
|
|
|
|
|
|
|
CServer::CCleanupNote::CCleanupNote(CServer* server) : m_server(server)
|
|
|
|
{
|
|
|
|
assert(m_server != NULL);
|
|
|
|
m_server->addCleanupThread(CThread::getCurrentThread());
|
|
|
|
}
|
|
|
|
|
|
|
|
CServer::CCleanupNote::~CCleanupNote()
|
|
|
|
{
|
|
|
|
m_server->removeCleanupThread(CThread::getCurrentThread());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// CServer::CConnectionNote
|
|
|
|
//
|
|
|
|
|
|
|
|
CServer::CConnectionNote::CConnectionNote(CServer* server,
|
|
|
|
const CString& name,
|
|
|
|
IServerProtocol* protocol) :
|
|
|
|
m_server(server),
|
|
|
|
m_name(name)
|
|
|
|
{
|
|
|
|
assert(m_server != NULL);
|
|
|
|
m_server->addConnection(m_name, protocol);
|
|
|
|
}
|
|
|
|
|
|
|
|
CServer::CConnectionNote::~CConnectionNote()
|
|
|
|
{
|
|
|
|
m_server->removeConnection(m_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// CServer::CScreenInfo
|
|
|
|
//
|
|
|
|
|
|
|
|
CServer::CScreenInfo::CScreenInfo(const CString& name,
|
|
|
|
IServerProtocol* protocol) :
|
|
|
|
m_name(name),
|
|
|
|
m_protocol(protocol),
|
|
|
|
m_width(0), m_height(0),
|
|
|
|
m_zoneSize(0)
|
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
CServer::CScreenInfo::~CScreenInfo()
|
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|