mirror of
https://github.com/debauchee/barrier.git
synced 2026-11-25 21:33:20 +08:00
finished ipc send to server.
This commit is contained in:
@@ -33,3 +33,24 @@ CIpcClient::connect()
|
||||
{
|
||||
m_socket.connect(m_serverAddress);
|
||||
}
|
||||
|
||||
void
|
||||
CIpcClient::send(const CIpcMessage& message)
|
||||
{
|
||||
UInt8 code[1];
|
||||
code[0] = message.m_type;
|
||||
m_socket.write(code, 1);
|
||||
|
||||
switch (message.m_type) {
|
||||
case kIpcCommand: {
|
||||
CString* s = (CString*)message.m_data;
|
||||
|
||||
UInt8 len[1];
|
||||
len[0] = s->size();
|
||||
m_socket.write(len, 1);
|
||||
|
||||
m_socket.write(s->c_str(), s->size());
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
|
||||
#include "CNetworkAddress.h"
|
||||
#include "CTCPSocket.h"
|
||||
#include "CIpcMessage.h"
|
||||
|
||||
//! IPC client for communication between daemon and GUI.
|
||||
/*!
|
||||
@@ -30,7 +31,10 @@ public:
|
||||
virtual ~CIpcClient();
|
||||
|
||||
//! Connects to the IPC server at localhost.
|
||||
void connect();
|
||||
void connect();
|
||||
|
||||
//! Sends a message to the server.
|
||||
void send(const CIpcMessage& message);
|
||||
|
||||
private:
|
||||
CNetworkAddress m_serverAddress;
|
||||
|
||||
@@ -17,12 +17,79 @@
|
||||
|
||||
#include "CIpcClientProxy.h"
|
||||
#include "IStream.h"
|
||||
#include "TMethodEventJob.h"
|
||||
#include "Ipc.h"
|
||||
#include "CLog.h"
|
||||
#include "CIpcMessage.h"
|
||||
|
||||
CEvent::Type CIpcClientProxy::s_messageReceivedEvent = CEvent::kUnknown;
|
||||
|
||||
CIpcClientProxy::CIpcClientProxy(IStream& stream) :
|
||||
m_stream(stream)
|
||||
{
|
||||
EVENTQUEUE->adoptHandler(m_stream.getInputReadyEvent(),
|
||||
stream.getEventTarget(),
|
||||
new TMethodEventJob<CIpcClientProxy>(
|
||||
this, &CIpcClientProxy::handleData, nullptr));
|
||||
}
|
||||
|
||||
CIpcClientProxy::~CIpcClientProxy()
|
||||
{
|
||||
EVENTQUEUE->removeHandler(m_stream.getInputReadyEvent(),
|
||||
m_stream.getEventTarget());
|
||||
}
|
||||
|
||||
void
|
||||
CIpcClientProxy::handleData(const CEvent&, void*)
|
||||
{
|
||||
UInt8 code[1];
|
||||
UInt32 n = m_stream.read(code, 1);
|
||||
while (n != 0) {
|
||||
|
||||
CIpcMessage* m = new CIpcMessage();
|
||||
m->m_type = code[1];
|
||||
|
||||
LOG((CLOG_DEBUG "ipc read message: %d", code[0]));
|
||||
switch (code[0]) {
|
||||
case kIpcCommand:
|
||||
m->m_data = parseCommand();
|
||||
break;
|
||||
|
||||
default:
|
||||
delete m;
|
||||
disconnect();
|
||||
return;
|
||||
}
|
||||
|
||||
// event deletes data.
|
||||
EVENTQUEUE->addEvent(CEvent(getMessageReceivedEvent(), this, m));
|
||||
|
||||
n = m_stream.read(code, 1);
|
||||
}
|
||||
}
|
||||
|
||||
void*
|
||||
CIpcClientProxy::parseCommand()
|
||||
{
|
||||
UInt8 len[1];
|
||||
m_stream.read(len, 1);
|
||||
|
||||
UInt8* buffer = new UInt8[len[0]];
|
||||
m_stream.read(buffer, len[0]);
|
||||
|
||||
return new CString((const char*)buffer, len[0]);
|
||||
}
|
||||
|
||||
void
|
||||
CIpcClientProxy::disconnect()
|
||||
{
|
||||
LOG((CLOG_NOTE "disconnect, closing stream"));
|
||||
m_stream.close();
|
||||
}
|
||||
|
||||
CEvent::Type
|
||||
CIpcClientProxy::getMessageReceivedEvent()
|
||||
{
|
||||
return EVENTQUEUE->registerTypeOnce(
|
||||
s_messageReceivedEvent, "CIpcClientProxy::messageReceived");
|
||||
}
|
||||
|
||||
@@ -17,6 +17,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "CEvent.h"
|
||||
|
||||
class IStream;
|
||||
|
||||
class CIpcClientProxy {
|
||||
@@ -24,6 +26,16 @@ public:
|
||||
CIpcClientProxy(IStream& stream);
|
||||
virtual ~CIpcClientProxy();
|
||||
|
||||
//! Raised when the server receives a message from a client.
|
||||
static CEvent::Type getMessageReceivedEvent();
|
||||
|
||||
private:
|
||||
void handleData(const CEvent&, void*);
|
||||
void* parseCommand();
|
||||
void disconnect();
|
||||
|
||||
private:
|
||||
IStream& m_stream;
|
||||
|
||||
static CEvent::Type s_messageReceivedEvent;
|
||||
};
|
||||
|
||||
27
src/lib/ipc/CIpcMessage.cpp
Normal file
27
src/lib/ipc/CIpcMessage.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* synergy -- mouse and keyboard sharing utility
|
||||
* Copyright (C) 2012 Nick Bolton
|
||||
*
|
||||
* This package is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* found in the file COPYING that should have accompanied this file.
|
||||
*
|
||||
* This package is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "CIpcMessage.h"
|
||||
|
||||
CIpcMessage::CIpcMessage()
|
||||
{
|
||||
}
|
||||
|
||||
CIpcMessage::~CIpcMessage()
|
||||
{
|
||||
delete m_data;
|
||||
}
|
||||
29
src/lib/ipc/CIpcMessage.h
Normal file
29
src/lib/ipc/CIpcMessage.h
Normal file
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* synergy -- mouse and keyboard sharing utility
|
||||
* Copyright (C) 2012 Nick Bolton
|
||||
*
|
||||
* This package is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* found in the file COPYING that should have accompanied this file.
|
||||
*
|
||||
* This package is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "BasicTypes.h"
|
||||
|
||||
class CIpcMessage {
|
||||
public:
|
||||
CIpcMessage();
|
||||
virtual ~CIpcMessage();
|
||||
|
||||
UInt8 m_type;
|
||||
void* m_data;
|
||||
};
|
||||
@@ -35,7 +35,7 @@ m_address(CNetworkAddress(IPC_HOST, IPC_PORT))
|
||||
EVENTQUEUE->adoptHandler(
|
||||
IListenSocket::getConnectingEvent(), &m_socket,
|
||||
new TMethodEventJob<CIpcServer>(
|
||||
this, &CIpcServer::handleClientConnecting));
|
||||
this, &CIpcServer::handleClientConnecting));
|
||||
}
|
||||
|
||||
CIpcServer::~CIpcServer()
|
||||
|
||||
@@ -46,7 +46,7 @@ public:
|
||||
//! @name accessors
|
||||
//@{
|
||||
|
||||
//! This event is raised when we have created the client proxy.
|
||||
//! Raised when we have created the client proxy.
|
||||
static CEvent::Type getClientConnectedEvent();
|
||||
|
||||
//@}
|
||||
|
||||
@@ -19,6 +19,7 @@ set(inc
|
||||
CIpcClient.h
|
||||
CIpcServerProxy.h
|
||||
CIpcClientProxy.h
|
||||
CIpcMessage.h
|
||||
)
|
||||
|
||||
set(src
|
||||
@@ -26,6 +27,7 @@ set(src
|
||||
CIpcClient.cpp
|
||||
CIpcServerProxy.cpp
|
||||
CIpcClientProxy.cpp
|
||||
CIpcMessage.cpp
|
||||
)
|
||||
|
||||
if (WIN32)
|
||||
|
||||
@@ -19,3 +19,8 @@
|
||||
|
||||
#define IPC_HOST "127.0.0.1"
|
||||
#define IPC_PORT 24801
|
||||
|
||||
enum EIpcMessage {
|
||||
kIpcLog,
|
||||
kIpcCommand
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user