From c72fb76316f436eb5987687f7c5b3f68f24cc817 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Thu, 15 Mar 2012 22:05:33 +0100 Subject: [PATCH] MSVC fixage for the new socket API --- library/RemoteClient.cpp | 40 +++++++++++++++++++--------------- library/RemoteServer.cpp | 22 +++++++++++-------- library/dfhack-run.cpp | 1 + library/include/RemoteClient.h | 10 +++++---- library/include/RemoteServer.h | 6 +++-- library/xml | 2 +- plugins/Plugins.cmake | 7 +++++- 7 files changed, 53 insertions(+), 35 deletions(-) diff --git a/library/RemoteClient.cpp b/library/RemoteClient.cpp index f8d3e3e7e..bec935efd 100644 --- a/library/RemoteClient.cpp +++ b/library/RemoteClient.cpp @@ -43,8 +43,10 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include #include "RemoteClient.h" +#include #include "MiscUtils.h" #include @@ -88,24 +90,26 @@ void color_ostream_proxy::decode(CoreTextNotification *data) RemoteClient::RemoteClient() { active = false; + socket = new CActiveSocket(); } RemoteClient::~RemoteClient() { disconnect(); + delete socket; } -bool DFHack::readFullBuffer(CSimpleSocket &socket, void *buf, int size) +bool DFHack::readFullBuffer(CSimpleSocket *socket, void *buf, int size) { - if (!socket.IsSocketValid()) + if (!socket->IsSocketValid()) return false; char *ptr = (char*)buf; while (size > 0) { - int cnt = socket.Receive(size); + int cnt = socket->Receive(size); if (cnt <= 0) return false; - memcpy(ptr, socket.GetData(), cnt); + memcpy(ptr, socket->GetData(), cnt); ptr += cnt; size -= cnt; } @@ -132,13 +136,13 @@ bool RemoteClient::connect(int port) if (port <= 0) port = GetDefaultPort(); - if (!socket.Initialize()) + if (!socket->Initialize()) { std::cerr << "Socket init failed." << endl; return false; } - if (!socket.Open((const uint8 *)"localhost", port)) + if (!socket->Open((const uint8 *)"localhost", port)) { std::cerr << "Could not connect to localhost:" << port << endl; return false; @@ -150,17 +154,17 @@ bool RemoteClient::connect(int port) memcpy(header.magic, RPCHandshakeHeader::REQUEST_MAGIC, sizeof(header.magic)); header.version = 1; - if (socket.Send((uint8*)&header, sizeof(header)) != sizeof(header)) + if (socket->Send((uint8*)&header, sizeof(header)) != sizeof(header)) { std::cerr << "Could not send header." << endl; - socket.Close(); + socket->Close(); return active = false; } if (!readFullBuffer(socket, &header, sizeof(header))) { std::cerr << "Could not read header." << endl; - socket.Close(); + socket->Close(); return active = false; } @@ -168,7 +172,7 @@ bool RemoteClient::connect(int port) header.version != 1) { std::cerr << "Invalid handshake response." << endl; - socket.Close(); + socket->Close(); return active = false; } @@ -185,22 +189,22 @@ bool RemoteClient::connect(int port) void RemoteClient::disconnect() { - if (active && socket.IsSocketValid()) + if (active && socket->IsSocketValid()) { RPCMessageHeader header; header.id = RPC_REQUEST_QUIT; header.size = 0; - if (socket.Send((uint8_t*)&header, sizeof(header)) != sizeof(header)) + if (socket->Send((uint8_t*)&header, sizeof(header)) != sizeof(header)) std::cerr << "Could not send the disconnect message." << endl; } - socket.Close(); + socket->Close(); } bool RemoteClient::bind(color_ostream &out, RemoteFunctionBase *function, const std::string &name, const std::string &proto) { - if (!active || !socket.IsSocketValid()) + if (!active || !socket->IsSocketValid()) return false; bind_call.reset(); @@ -227,7 +231,7 @@ bool RemoteClient::bind(color_ostream &out, RemoteFunctionBase *function, command_result RemoteClient::run_command(color_ostream &out, const std::string &cmd, const std::vector &args) { - if (!active || !socket.IsSocketValid()) + if (!active || !socket->IsSocketValid()) { out.printerr("In RunCommand: client connection not valid.\n"); return CR_FAILURE; @@ -278,7 +282,7 @@ bool RemoteFunctionBase::bind(color_ostream &out, RemoteClient *client, return client->bind(out, this, name, proto); } -bool DFHack::sendRemoteMessage(CSimpleSocket &socket, int16_t id, const MessageLite *msg, int *psz) +bool DFHack::sendRemoteMessage(CSimpleSocket *socket, int16_t id, const MessageLite *msg, int *psz) { int size = msg->ByteSize(); int fullsz = size + sizeof(RPCMessageHeader); @@ -295,7 +299,7 @@ bool DFHack::sendRemoteMessage(CSimpleSocket &socket, int16_t id, const MessageL if (!msg->SerializeToArray(data.get() + sizeof(RPCMessageHeader), size)) return false; - return (socket.Send(data.get(), fullsz) == fullsz); + return (socket->Send(data.get(), fullsz) == fullsz); } command_result RemoteFunctionBase::execute(color_ostream &out, @@ -307,7 +311,7 @@ command_result RemoteFunctionBase::execute(color_ostream &out, return CR_NOT_IMPLEMENTED; } - if (!p_client->socket.IsSocketValid()) + if (!p_client->socket->IsSocketValid()) { out.printerr("In call to %s::%s: invalid socket.\n", this->proto.c_str(), this->name.c_str()); diff --git a/library/RemoteServer.cpp b/library/RemoteServer.cpp index 9d3c1101f..e7ab0cbad 100644 --- a/library/RemoteServer.cpp +++ b/library/RemoteServer.cpp @@ -43,8 +43,10 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include #include "RemoteServer.h" +#include "PassiveSocket.h" #include "PluginManager.h" #include "MiscUtils.h" @@ -250,7 +252,7 @@ void ServerConnection::connection_ostream::flush_proxy() buffer.clear(); - if (!sendRemoteMessage(*owner->socket, RPC_REPLY_TEXT, &msg)) + if (!sendRemoteMessage(owner->socket, RPC_REPLY_TEXT, &msg)) { owner->in_error = true; Core::printerr("Error writing text into client socket.\n"); @@ -267,7 +269,7 @@ void ServerConnection::threadFn(void *arg) { RPCHandshakeHeader header; - if (!readFullBuffer(*me->socket, &header, sizeof(header))) + if (!readFullBuffer(me->socket, &header, sizeof(header))) { out << "In RPC server: could not read handshake header." << endl; delete me; @@ -301,7 +303,7 @@ void ServerConnection::threadFn(void *arg) // Read the message RPCMessageHeader header; - if (!readFullBuffer(*me->socket, &header, sizeof(header))) + if (!readFullBuffer(me->socket, &header, sizeof(header))) { out.printerr("In RPC server: I/O error in receive header.\n"); break; @@ -318,7 +320,7 @@ void ServerConnection::threadFn(void *arg) std::auto_ptr buf(new uint8_t[header.size]); - if (!readFullBuffer(*me->socket, buf.get(), header.size)) + if (!readFullBuffer(me->socket, buf.get(), header.size)) { out.printerr("In RPC server: I/O error in receive %d bytes of data.\n", header.size); break; @@ -363,7 +365,7 @@ void ServerConnection::threadFn(void *arg) if (res == CR_OK && reply) { - if (!sendRemoteMessage(*me->socket, RPC_REPLY_RESULT, reply, &out_size)) + if (!sendRemoteMessage(me->socket, RPC_REPLY_RESULT, reply, &out_size)) { out.printerr("In RPC server: I/O error in send result.\n"); break; @@ -398,12 +400,14 @@ void ServerConnection::threadFn(void *arg) ServerMain::ServerMain() { + socket = new CPassiveSocket(); thread = NULL; } ServerMain::~ServerMain() { - socket.Close(); + socket->Close(); + delete socket; } bool ServerMain::listen(int port) @@ -411,9 +415,9 @@ bool ServerMain::listen(int port) if (thread) return true; - socket.Initialize(); + socket->Initialize(); - if (!socket.Listen((const uint8 *)"127.0.0.1", port)) + if (!socket->Listen((const uint8 *)"127.0.0.1", port)) return false; thread = new tthread::thread(threadFn, this); @@ -425,7 +429,7 @@ void ServerMain::threadFn(void *arg) ServerMain *me = (ServerMain*)arg; CActiveSocket *client; - while ((client = me->socket.Accept()) != NULL) + while ((client = me->socket->Accept()) != NULL) { new ServerConnection(client); } diff --git a/library/dfhack-run.cpp b/library/dfhack-run.cpp index eab039013..16c6087ec 100644 --- a/library/dfhack-run.cpp +++ b/library/dfhack-run.cpp @@ -43,6 +43,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include #include "RemoteClient.h" diff --git a/library/include/RemoteClient.h b/library/include/RemoteClient.h index af03b32a1..c14ee56fc 100644 --- a/library/include/RemoteClient.h +++ b/library/include/RemoteClient.h @@ -27,7 +27,9 @@ distribution. #include "Export.h" #include "ColorText.h" -#include "ActiveSocket.h" +class CPassiveSocket; +class CActiveSocket; +class CSimpleSocket; #include "CoreProtocol.pb.h" @@ -197,8 +199,8 @@ namespace DFHack } }; - bool readFullBuffer(CSimpleSocket &socket, void *buf, int size); - bool sendRemoteMessage(CSimpleSocket &socket, int16_t id, + bool readFullBuffer(CSimpleSocket *socket, void *buf, int size); + bool sendRemoteMessage(CSimpleSocket *socket, int16_t id, const ::google::protobuf::MessageLite *msg, int *psz = NULL); class DFHACK_EXPORT RemoteClient @@ -222,7 +224,7 @@ namespace DFHack private: bool active; - CActiveSocket socket; + CActiveSocket * socket; RemoteFunction bind_call; RemoteFunction runcmd_call; diff --git a/library/include/RemoteServer.h b/library/include/RemoteServer.h index c9cbc8252..274d2bcda 100644 --- a/library/include/RemoteServer.h +++ b/library/include/RemoteServer.h @@ -28,7 +28,9 @@ distribution. #include "RemoteClient.h" #include "Core.h" -#include "PassiveSocket.h" +class CPassiveSocket; +class CActiveSocket; +class CSimpleSocket; namespace DFHack { @@ -236,7 +238,7 @@ namespace DFHack }; class DFHACK_EXPORT ServerMain { - CPassiveSocket socket; + CPassiveSocket *socket; tthread::thread *thread; static void threadFn(void *); diff --git a/library/xml b/library/xml index 136181f06..39ab58ccc 160000 --- a/library/xml +++ b/library/xml @@ -1 +1 @@ -Subproject commit 136181f067a0a5ed19a19c9f98eece41003fe372 +Subproject commit 39ab58ccc2d898cb2aef13e46eccc9f645c7dc0f diff --git a/plugins/Plugins.cmake b/plugins/Plugins.cmake index 17c7320aa..78892967d 100644 --- a/plugins/Plugins.cmake +++ b/plugins/Plugins.cmake @@ -72,7 +72,12 @@ MACRO(DFHACK_PLUGIN) ADD_LIBRARY(${PLUGIN_NAME} MODULE ${PLUGIN_SOURCES}) IDE_FOLDER(${PLUGIN_NAME} "Plugins") - TARGET_LINK_LIBRARIES(${PLUGIN_NAME} dfhack ${PLUGIN_LINK_LIBRARIES}) + LIST(LENGTH PLUGIN_PROTOBUFS NUM_PROTO) + IF(NUM_PROTO) + TARGET_LINK_LIBRARIES(${PLUGIN_NAME} dfhack protobuf-lite ${PLUGIN_LINK_LIBRARIES}) + ELSE() + TARGET_LINK_LIBRARIES(${PLUGIN_NAME} dfhack ${PLUGIN_LINK_LIBRARIES}) + ENDIF() IF(UNIX) SET_TARGET_PROPERTIES(${PLUGIN_NAME} PROPERTIES SUFFIX .plug.so PREFIX "") SET_TARGET_PROPERTIES(${PLUGIN_NAME} PROPERTIES COMPILE_FLAGS "-include Export.h")