-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Niall Ryan
authored and
Niall Ryan
committed
Jun 4, 2010
1 parent
25b877a
commit d359a04
Showing
11 changed files
with
351 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
|
||
#include "ProtoBufRemote/SocketRpcChannel.h" | ||
|
||
#include "ProtoBufRemote/RpcController.h" | ||
#include "ProtoBufRemote/RpcMessage.pb.h" | ||
|
||
|
||
namespace ProtoBufRemote { | ||
|
||
SocketRpcChannel::SocketRpcChannel(RpcController* controller, SOCKET socket) | ||
: RpcChannel(controller), m_socket(socket) | ||
{ | ||
m_sendEvent = WSACreateEvent(); | ||
m_terminateEvent = WSACreateEvent(); | ||
} | ||
|
||
SocketRpcChannel::~SocketRpcChannel() | ||
{ | ||
CloseAndJoin(); | ||
WSACloseEvent(m_sendEvent); | ||
WSACloseEvent(m_terminateEvent); | ||
} | ||
|
||
void SocketRpcChannel::Start() | ||
{ | ||
m_thread = boost::thread(&SocketRpcChannel::Run, this); | ||
} | ||
|
||
void SocketRpcChannel::CloseAndJoin() | ||
{ | ||
WSASetEvent(m_terminateEvent); | ||
m_thread.join(); | ||
} | ||
|
||
void SocketRpcChannel::Send(const RpcMessage& message) | ||
{ | ||
QueuedMessageData data; | ||
unsigned int messageSize = message.ByteSize(); | ||
data.m_size = messageSize + sizeof(int); | ||
data.m_data = new char[data.m_size]; | ||
*reinterpret_cast<unsigned int*>(data.m_data) = messageSize; | ||
message.SerializeToArray(data.m_data+sizeof(int), messageSize); | ||
|
||
boost::lock_guard<boost::mutex> lock(m_sendMutex); | ||
m_sendMessages.push(data); | ||
WSASetEvent(m_sendEvent); | ||
} | ||
|
||
void SocketRpcChannel::Run() | ||
{ | ||
WSAEVENT selectEvent = WSACreateEvent(); | ||
WSAEventSelect(m_socket, selectEvent, FD_READ|FD_WRITE); | ||
|
||
bool isSending = false; | ||
unsigned int sendPos = 0; | ||
QueuedMessageData currentSendMessage; | ||
|
||
bool isReceiving = false; | ||
unsigned int receivePos = 0; | ||
unsigned int receiveSize = sizeof(int); | ||
std::vector<char> receiveBuffer; | ||
receiveBuffer.resize(1024); | ||
const unsigned int maxAllowedMessageSize = 1024*1024; | ||
|
||
WSAEVENT events[3] = { m_terminateEvent, selectEvent, m_sendEvent }; | ||
for ( ;; ) | ||
{ | ||
int waitResult = WSAWaitForMultipleEvents(isSending ? 2 : 3, events, FALSE, WSA_INFINITE, FALSE); | ||
if (waitResult == 0) | ||
break; | ||
|
||
bool isSendReady = false; | ||
bool isReceiveReady = false; | ||
if (waitResult == 2) | ||
{ | ||
assert(!isSending); | ||
boost::lock_guard<boost::mutex> lock(m_sendMutex); | ||
if (!m_sendMessages.empty()) | ||
{ | ||
currentSendMessage = m_sendMessages.front(); | ||
m_sendMessages.pop(); | ||
isSending = true; | ||
isSendReady = true; //send immediately | ||
sendPos = 0; | ||
} | ||
} | ||
else | ||
{ | ||
WSANETWORKEVENTS networkEvents; | ||
int result = WSAEnumNetworkEvents(m_socket, selectEvent, &networkEvents); | ||
isSendReady = (networkEvents.lNetworkEvents & FD_WRITE) ? true : false; | ||
isReceiveReady = (networkEvents.lNetworkEvents & FD_READ) ? true : false; | ||
} | ||
|
||
if (isSending && isSendReady) | ||
{ | ||
int bytesSent = send(m_socket, ¤tSendMessage.m_data[sendPos], currentSendMessage.m_size-sendPos, 0); | ||
if (bytesSent != SOCKET_ERROR) | ||
{ | ||
sendPos += bytesSent; | ||
|
||
if (sendPos >= currentSendMessage.m_size) | ||
{ | ||
delete[] currentSendMessage.m_data; | ||
|
||
boost::lock_guard<boost::mutex> lock(m_sendMutex); | ||
if (m_sendMessages.empty()) | ||
{ | ||
WSAResetEvent(m_sendEvent); | ||
isSending = false; | ||
} | ||
else | ||
{ | ||
currentSendMessage = m_sendMessages.front(); | ||
m_sendMessages.pop(); | ||
sendPos = 0; | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (isReceiveReady) | ||
{ | ||
int bytesReceived = recv(m_socket, &receiveBuffer[receivePos], receiveSize-receivePos, 0); | ||
if (bytesReceived == 0) | ||
break; | ||
if (bytesReceived != SOCKET_ERROR) | ||
{ | ||
receivePos += bytesReceived; | ||
|
||
if (receivePos >= receiveSize) | ||
{ | ||
if (isReceiving) | ||
{ | ||
RpcMessage message; | ||
message.ParseFromArray(&receiveBuffer[0], receiveSize); | ||
m_controller->Receive(message); | ||
|
||
isReceiving = false; | ||
receiveSize = sizeof(int); | ||
receivePos = 0; | ||
} | ||
else | ||
{ | ||
isReceiving = true; | ||
receiveSize = *reinterpret_cast<unsigned int*>(&receiveBuffer[0]); | ||
receivePos = 0; | ||
|
||
if (receiveSize > maxAllowedMessageSize) | ||
{ | ||
assert(false); | ||
break; //better to abort rather than allocate based on bad data | ||
} | ||
if (receiveBuffer.size() < receiveSize) | ||
receiveBuffer.resize(receiveSize); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
closesocket(m_socket); | ||
m_socket = INVALID_SOCKET; | ||
WSACloseEvent(selectEvent); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#ifndef PROTOBUFREMOTE_SOCKETRPCCHANNEL_H | ||
#define PROTOBUFREMOTE_SOCKETRPCCHANNEL_H 1 | ||
|
||
#include <winsock2.h> | ||
#include <queue> | ||
#include <boost/thread.hpp> | ||
#include "ProtoBufRemote/RpcChannel.h" | ||
|
||
namespace ProtoBufRemote { | ||
|
||
class SocketRpcChannel : RpcChannel | ||
{ | ||
public: | ||
SocketRpcChannel(RpcController* controller, SOCKET socket); | ||
virtual ~SocketRpcChannel(); | ||
|
||
void Start(); | ||
|
||
void CloseAndJoin(); | ||
|
||
virtual void Send(const RpcMessage& message); | ||
|
||
private: | ||
void Run(); | ||
|
||
SOCKET m_socket; | ||
boost::thread m_thread; | ||
WSAEVENT m_sendEvent; | ||
WSAEVENT m_terminateEvent; | ||
|
||
boost::mutex m_sendMutex; | ||
|
||
struct QueuedMessageData | ||
{ | ||
unsigned int m_size; | ||
char* m_data; | ||
}; | ||
std::queue<QueuedMessageData> m_sendMessages; | ||
}; | ||
|
||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.