Skip to content

Commit

Permalink
removed dependency on boost threads, mutex, and unordered containers,…
Browse files Browse the repository at this point in the history
… now boost is optional and only used for preprocessor generators
  • Loading branch information
Niall Ryan authored and Niall Ryan committed Sep 11, 2010
1 parent affc14c commit e75b55b
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 17 deletions.
4 changes: 2 additions & 2 deletions Cpp/Source/ProtoBufRemote/RpcClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define PROTOBUFREMOTE_RPCCLIENT_H 1

#include <string>
#include <boost/unordered_map.hpp>
#include <unordered_map>

namespace ProtoBufRemote {

Expand All @@ -29,7 +29,7 @@ class RpcClient
RpcController* m_controller;
int m_nextId;

typedef boost::unordered_map<int, PendingCall*> PendingCallMap;
typedef std::unordered_map<int, PendingCall*> PendingCallMap;
PendingCallMap m_pendingCalls;
};

Expand Down
4 changes: 2 additions & 2 deletions Cpp/Source/ProtoBufRemote/RpcServer.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef PROTOBUFREMOTE_RPCSERVER_H
#define PROTOBUFREMOTE_RPCSERVER_H 1

#include <boost/unordered_map.hpp>
#include <unordered_map>
#include "ProtoBufRemote/RpcMessage.pb.h"

namespace ProtoBufRemote {
Expand All @@ -26,7 +26,7 @@ class RpcServer
RpcController* m_controller;
RpcMessage m_resultMessage;

typedef boost::unordered_map<std::string, RpcService*> ServiceMap;
typedef std::unordered_map<std::string, RpcService*> ServiceMap;
ServiceMap m_services;
};

Expand Down
27 changes: 22 additions & 5 deletions Cpp/Source/ProtoBufRemote/SocketRpcChannel.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

#include "ProtoBufRemote/SocketRpcChannel.h"

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <process.h>

#include "ProtoBufRemote/RpcController.h"
#include "ProtoBufRemote/RpcMessage.pb.h"

Expand All @@ -12,24 +16,27 @@ SocketRpcChannel::SocketRpcChannel(RpcController* controller, SOCKET socket)
{
m_sendEvent = WSACreateEvent();
m_terminateEvent = WSACreateEvent();
InitializeCriticalSection(&m_sendMutex);
}

SocketRpcChannel::~SocketRpcChannel()
{
CloseAndJoin();
WSACloseEvent(m_sendEvent);
WSACloseEvent(m_terminateEvent);
DeleteCriticalSection(&m_sendMutex);
}

void SocketRpcChannel::Start()
{
m_thread = boost::thread(&SocketRpcChannel::Run, this);
m_thread = reinterpret_cast<HANDLE>(_beginthreadex(NULL, 0, &SocketRpcChannel::ThreadRun, this, 0, NULL));
}

void SocketRpcChannel::CloseAndJoin()
{
WSASetEvent(m_terminateEvent);
m_thread.join();
WaitForSingleObject(m_thread, INFINITE);
CloseHandle(m_thread);
}

unsigned int SocketRpcChannel::GetAndClearBytesRead()
Expand All @@ -52,9 +59,17 @@ void SocketRpcChannel::Send(const RpcMessage& message)
bool isOk = message.SerializeToArray(data.m_data+sizeof(int), messageSize);
assert(isOk);

boost::lock_guard<boost::mutex> lock(m_sendMutex);
EnterCriticalSection(&m_sendMutex);
m_sendMessages.push(data);
WSASetEvent(m_sendEvent);
LeaveCriticalSection(&m_sendMutex);
}

unsigned int _stdcall SocketRpcChannel::ThreadRun(void* arg)
{
SocketRpcChannel* socketRpcChannel = static_cast<SocketRpcChannel*>(arg);
socketRpcChannel->Run();
return 0;
}

void SocketRpcChannel::Run()
Expand Down Expand Up @@ -86,7 +101,7 @@ void SocketRpcChannel::Run()
if (waitResult == 2)
{
assert(!isSending);
boost::lock_guard<boost::mutex> lock(m_sendMutex);
EnterCriticalSection(&m_sendMutex);
if (!m_sendMessages.empty())
{
currentSendMessage = m_sendMessages.front();
Expand All @@ -95,6 +110,7 @@ void SocketRpcChannel::Run()
isSendReady = true; //send immediately
sendPos = 0;
}
LeaveCriticalSection(&m_sendMutex);
}
else
{
Expand Down Expand Up @@ -124,7 +140,7 @@ void SocketRpcChannel::Run()
{
delete[] currentSendMessage.m_data;

boost::lock_guard<boost::mutex> lock(m_sendMutex);
EnterCriticalSection(&m_sendMutex);
if (m_sendMessages.empty())
{
WSAResetEvent(m_sendEvent);
Expand All @@ -136,6 +152,7 @@ void SocketRpcChannel::Run()
m_sendMessages.pop();
sendPos = 0;
}
LeaveCriticalSection(&m_sendMutex);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions Cpp/Source/ProtoBufRemote/SocketRpcChannel.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include <winsock2.h>
#include <queue>
#include <boost/thread.hpp>
#include "ProtoBufRemote/RpcChannel.h"

namespace ProtoBufRemote {
Expand All @@ -25,14 +24,15 @@ class SocketRpcChannel : RpcChannel
unsigned int GetAndClearBytesWritten();

private:
static unsigned int _stdcall ThreadRun(void* arg);
void Run();

SOCKET m_socket;
boost::thread m_thread;
HANDLE m_thread;
WSAEVENT m_sendEvent;
WSAEVENT m_terminateEvent;

boost::mutex m_sendMutex;
CRITICAL_SECTION m_sendMutex;

struct QueuedMessageData
{
Expand Down
4 changes: 2 additions & 2 deletions Cpp/Source/ProtoBufRemoteTest/ProtoBufRemoteTest.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<IntDir>obj\$(Configuration)\</IntDir>
<LibraryPath>$(GmockDebugLibPath);$(ProtoBufDebugLibPath);$(BoostLibPath);$(LibraryPath)</LibraryPath>
<LibraryPath>$(GmockDebugLibPath);$(ProtoBufDebugLibPath);$(LibraryPath)</LibraryPath>
<IncludePath>$(GtestIncludePath);$(GmockIncludePath);$(ProtoBufIncludePath);$(BoostIncludePath);C:\Program Files\Microsoft DirectX SDK %28February 2010%29\Include;$(IncludePath)</IncludePath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<OutDir>$(ProjectDir)obj\$(Configuration)\</OutDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<IntDir>obj\$(Configuration)\</IntDir>
<LibraryPath>$(GmockReleaseLibPath);$(ProtoBufReleaseLibPath);$(BoostLibPath);$(LibraryPath)</LibraryPath>
<LibraryPath>$(GmockReleaseLibPath);$(ProtoBufReleaseLibPath);$(LibraryPath)</LibraryPath>
<IncludePath>$(GtestIncludePath);$(GmockIncludePath);$(ProtoBufIncludePath);$(BoostIncludePath);C:\Program Files\Microsoft DirectX SDK %28February 2010%29\Include;$(IncludePath)</IncludePath>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
Expand Down
10 changes: 7 additions & 3 deletions Cpp/Source/ProtoBufRemoteTest/SocketRpcChannelTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <gtest/gtest.h>

#include <ws2tcpip.h>
#include <process.h>
#include "ProtoBufRemote/RpcController.h"
#include "ProtoBufRemote/RpcMessage.pb.h"
#include "ProtoBufRemote/SocketRpcChannel.h"
Expand All @@ -15,7 +16,7 @@ namespace {
SocketRpcChannel* g_serverChannel;
}

void ServerThreadRun()
unsigned int _stdcall ServerThreadRun(void * arg)
{
ADDRINFO hints;
ADDRINFO* addrResult;
Expand All @@ -39,6 +40,8 @@ void ServerThreadRun()

freeaddrinfo(addrResult);
closesocket(listenSocket);

return 0;
}

class DummyRpcController : public RpcController
Expand Down Expand Up @@ -79,7 +82,7 @@ TEST(SocketRpcChannelTest, SendReceive)
WSADATA wsaData;
int result = WSAStartup(MAKEWORD(2,2), &wsaData);

boost::thread serverThread(ServerThreadRun);
HANDLE serverThread = reinterpret_cast<HANDLE>(_beginthreadex(NULL, 0, &ServerThreadRun, NULL, 0, NULL));

ADDRINFO hints;
ADDRINFO* addrResult;
Expand All @@ -92,7 +95,8 @@ TEST(SocketRpcChannelTest, SendReceive)
SOCKET connectSocket = socket(addrResult->ai_family, addrResult->ai_socktype, addrResult->ai_protocol);
result = connect(connectSocket, addrResult->ai_addr, (int)addrResult->ai_addrlen);

serverThread.join();
WaitForSingleObject(serverThread, INFINITE);
CloseHandle(serverThread);

DummyRpcController dummyController;
SocketRpcChannel clientChannel(&dummyController, connectSocket);
Expand Down

0 comments on commit e75b55b

Please sign in to comment.