code reorg

This commit is contained in:
Alex Yatskov 2011-09-04 20:05:33 -07:00
parent c4cc7b9f83
commit af2f8894e3
11 changed files with 129 additions and 110 deletions

View File

@ -41,15 +41,19 @@ namespace metacall {
//
#ifdef _WIN32
typedef unsigned __int8 byte;
typedef unsigned __int16 word;
typedef unsigned __int32 dword;
typedef unsigned __int64 qword;
#else
typedef unsigned char byte;
typedef unsigned short word;
typedef unsigned int dword;
typedef unsigned long int qword;
#endif

View File

@ -33,8 +33,7 @@ namespace metacall {
//
enum PacketConst {
PACKET_CONST_MAGIC = 0x55534544,
PACKET_CONST_VERSION = 1
PACKET_CONST_MAGIC = 0x55534544
};
enum PacketId {
@ -49,10 +48,10 @@ enum PacketId {
//
struct PacketHeader {
PacketHeader() :
magic(0),
id(PACKET_ID_INVALID),
size(0)
PacketHeader(dword magic = PACKET_CONST_MAGIC, byte id = PACKET_ID_INVALID, dword size = 0) :
magic(magic),
id(id),
size(size)
{
}
@ -81,9 +80,9 @@ struct PacketHeader {
//
struct PacketInvokeRequest {
PacketInvokeRequest() :
function(0),
taskId(0)
PacketInvokeRequest(dword function = 0, dword taskId = 0) :
function(function),
taskId(taskId)
{
}
@ -116,9 +115,9 @@ struct PacketInvokeRequest {
//
struct PacketInvokeReply {
PacketInvokeReply() :
flags(0),
taskId(0)
PacketInvokeReply(dword flags = 0, dword taskId = 0) :
flags(flags),
taskId(taskId)
{
}

View File

@ -27,7 +27,7 @@ namespace metacall {
template <typename P1>
TaskId Protocol::invoke(const Token& token, const P1& p1) {
Protocol::TaskId Protocol::invoke(const Token& token, const P1& p1) {
PacketInvokeRequest packetRequest;
Serializer serializer(&packetRequest.data);
@ -37,7 +37,7 @@ TaskId Protocol::invoke(const Token& token, const P1& p1) {
}
template <typename P1, typename P2>
TaskId Protocol::invoke(const Token& token, const P1& p1, const P2& p2) {
Protocol::TaskId Protocol::invoke(const Token& token, const P1& p1, const P2& p2) {
PacketInvokeRequest packetRequest;
Serializer serializer(&packetRequest.data);
@ -48,7 +48,7 @@ TaskId Protocol::invoke(const Token& token, const P1& p1, const P2& p2) {
}
template <typename P1, typename P2, typename P3>
TaskId Protocol::invoke(const Token& token, const P1& p1, const P2& p2, const P3& p3) {
Protocol::TaskId Protocol::invoke(const Token& token, const P1& p1, const P2& p2, const P3& p3) {
PacketInvokeRequest packetRequest;
Serializer serializer(&packetRequest.data);
@ -60,7 +60,7 @@ TaskId Protocol::invoke(const Token& token, const P1& p1, const P2& p2, const P3
}
template <typename P1, typename P2, typename P3, typename P4>
TaskId Protocol::invoke(const Token& token, const P1& p1, const P2& p2, const P3 & p3, const P4& p4) {
Protocol::TaskId Protocol::invoke(const Token& token, const P1& p1, const P2& p2, const P3 & p3, const P4& p4) {
PacketInvokeRequest packetRequest;
Serializer serializer(&packetRequest.data);
@ -73,7 +73,7 @@ TaskId Protocol::invoke(const Token& token, const P1& p1, const P2& p2, const P3
}
template <typename P1, typename P2, typename P3, typename P4, typename P5>
TaskId Protocol::invoke(const Token& token, const P1& p1, const P2& p2, const P3& p3, const P4& p4, const P5& p5) {
Protocol::TaskId Protocol::invoke(const Token& token, const P1& p1, const P2& p2, const P3& p3, const P4& p4, const P5& p5) {
PacketInvokeRequest packetRequest;
Serializer serializer(&packetRequest.data);
@ -87,7 +87,7 @@ TaskId Protocol::invoke(const Token& token, const P1& p1, const P2& p2, const P3
}
template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6>
TaskId Protocol::invoke(const Token& token, const P1& p1, const P2& p2, const P3& p3, const P4& p4, const P5& p5, const P6& p6) {
Protocol::TaskId Protocol::invoke(const Token& token, const P1& p1, const P2& p2, const P3& p3, const P4& p4, const P5& p5, const P6& p6) {
PacketInvokeRequest packetRequest;
Serializer serializer(&packetRequest.data);
@ -102,7 +102,7 @@ TaskId Protocol::invoke(const Token& token, const P1& p1, const P2& p2, const P3
}
template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7>
TaskId Protocol::invoke(const Token& token, const P1& p1, const P2& p2, const P3& p3, const P4& p4, const P5& p5, const P6& p6, const P7& p7) {
Protocol::TaskId Protocol::invoke(const Token& token, const P1& p1, const P2& p2, const P3& p3, const P4& p4, const P5& p5, const P6& p6, const P7& p7) {
PacketInvokeRequest packetRequest;
Serializer serializer(&packetRequest.data);
@ -118,7 +118,7 @@ TaskId Protocol::invoke(const Token& token, const P1& p1, const P2& p2, const P3
}
template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8>
TaskId Protocol::invoke(const Token& token, const P1& p1, const P2& p2, const P3& p3, const P4& p4, const P5& p5, const P6& p6, const P7& p7, const P8& p8) {
Protocol::TaskId Protocol::invoke(const Token& token, const P1& p1, const P2& p2, const P3& p3, const P4& p4, const P5& p5, const P6& p6, const P7& p7, const P8& p8) {
PacketInvokeRequest packetRequest;
Serializer serializer(&packetRequest.data);
@ -135,7 +135,7 @@ TaskId Protocol::invoke(const Token& token, const P1& p1, const P2& p2, const P3
}
template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9>
TaskId Protocol::invoke(const Token& token, const P1& p1, const P2& p2, const P3& p3, const P4& p4, const P5& p5, const P6& p6, const P7& p7, const P8& p8, const P9& p9) {
Protocol::TaskId Protocol::invoke(const Token& token, const P1& p1, const P2& p2, const P3& p3, const P4& p4, const P5& p5, const P6& p6, const P7& p7, const P8& p8, const P9& p9) {
PacketInvokeRequest packetRequest;
Serializer serializer(&packetRequest.data);
@ -153,7 +153,7 @@ TaskId Protocol::invoke(const Token& token, const P1& p1, const P2& p2, const P3
}
template <typename R>
TaskState Protocol::queryResult(TaskId id, R* result) {
Protocol::TaskState Protocol::queryResult(TaskId id, R* result) {
TaskMap::const_iterator iter = taskMap_.find(id);
if (iter == taskMap_.end()) {
return TASK_STATE_UNDEFINED;

View File

@ -100,7 +100,7 @@ void Protocol::clearHandlers() {
}
}
TaskState Protocol::queryState(TaskId id) const {
Protocol::TaskState Protocol::queryState(TaskId id) const {
const TaskMap::const_iterator iter = taskMap_.find(id);
return iter == taskMap_.end() ? TASK_STATE_UNDEFINED : iter->second.state;
}
@ -179,12 +179,12 @@ Stream::State Protocol::advanceInvokeRequest() {
return state;
}
TaskId Protocol::invoke(const Token& token) {
Protocol::TaskId Protocol::invoke(const Token& token) {
PacketInvokeRequest packetRequest;
return invokeExec(token, &packetRequest);
}
TaskId Protocol::invokeExec(const Token& token, PacketInvokeRequest* packetRequest) {
Protocol::TaskId Protocol::invokeExec(const Token& token, PacketInvokeRequest* packetRequest) {
const TaskId taskId = registerTaskId();
packetRequest->function = token;
@ -198,7 +198,7 @@ TaskId Protocol::invokeExec(const Token& token, PacketInvokeRequest* packetReque
return taskId;
}
TaskId Protocol::registerTaskId() {
Protocol::TaskId Protocol::registerTaskId() {
static int id = TASK_ID_INVALID;
while (++id == TASK_ID_INVALID);
return static_cast<TaskId>(id);

View File

@ -29,9 +29,11 @@ namespace metacall {
//
// Types
// Protocol
//
class Protocol {
public:
enum TaskId {
TASK_ID_INVALID
};
@ -50,13 +52,6 @@ typedef void (*HandlerProc)(
void* userPtr
);
//
// Protocol
//
class Protocol {
public:
Protocol(Stream* stream, Binding* binding);
void advance ();

View File

@ -84,14 +84,15 @@ void Server::getClients(std::vector<ClientId>* clients) const {
bool Server::getClient(ClientId id, ClientData* data) const {
const ClientMap::const_iterator iter = clients_.find(id);
if (iter != clients_.end()) {
if (iter == clients_.end()) {
return false;
}
data->id = iter->first;
data->name = iter->second->socket.hostname();
data->protocol = &iter->second->protocol;
return true;
}
return false;
return true;
}
void Server::disconnect(ClientId id) {
@ -114,7 +115,7 @@ int Server::clientCount() const {
return clients_.size();
}
ClientId Server::clientActive() const {
Server::ClientId Server::clientActive() const {
return clientActive_;
}
@ -128,7 +129,11 @@ void Server::advanceConnecting() {
return;
}
const std::pair<ClientId, ClientEntry*> entry(registerClientId(), new ClientEntry(&binding_));
const std::pair<ClientId, ClientEntry*> entry(
registerClientId(),
new ClientEntry(&binding_)
);
const ClientMap::iterator iter = clients_.insert(entry).first;
iter->second->socket.set(client.release());
iter->second->socket.setNagle(false);
@ -159,10 +164,10 @@ void Server::advanceConnected() {
clientActive_ = CLIENT_ID_INVALID;
}
ClientId Server::registerClientId() {
static int s_id = CLIENT_ID_INVALID;
while (++s_id == CLIENT_ID_INVALID);
return static_cast<ClientId>(s_id);
Server::ClientId Server::registerClientId() {
static int id = CLIENT_ID_INVALID;
while (++id == CLIENT_ID_INVALID);
return static_cast<ClientId>(id);
}

View File

@ -29,9 +29,11 @@ namespace metacall {
//
// Types
// Server
//
class Server {
public:
enum ClientId {
CLIENT_ID_INVALID
};
@ -48,13 +50,6 @@ struct ClientData {
Protocol* protocol;
};
//
// Server
//
class Server {
public:
Server();
~Server();

View File

@ -32,31 +32,24 @@ namespace metacall {
namespace {
//
// Constants
//
const int SOCKET_INVALID = -1;
#ifdef _WIN32
//
// Winsock
//
#ifdef _WIN32
#pragma comment(lib, "ws2_32.lib")
struct WsaScope {
WsaScope() {
struct Winsock {
Winsock() {
WSAData data;
WSAStartup(MAKEWORD(2, 2), &data);
}
~WsaScope() {
~Winsock() {
WSACleanup();
}
} wsa;
} winsock;
#endif
@ -64,6 +57,15 @@ struct WsaScope {
}
//
// Constants
//
enum {
SOCKET_INVALID = -1
};
//
// Socket
//

View File

@ -36,20 +36,25 @@ Stream::State Stream::send(const T& packet) {
Serializer serializerTemp(&buffTemp);
serializerTemp.write(packet);
PacketHeader header;
header.magic = PACKET_CONST_MAGIC;
header.size = serializerTemp.offset();
header.id = T::Id;
const PacketHeader header(
PACKET_CONST_MAGIC,
T::Id,
serializerTemp.offset()
);
Serializer serializer_send(&buffSend_);
serializer_send.write(header);
serializer_send.writeRaw(buffTemp.data(), buffTemp.bytes());
Serializer serializerSend(&buffSend_);
serializerSend.write(header);
serializerSend.write(buffTemp);
return STATE_READY;
}
template <typename T>
Stream::State Stream::receive(T* packet) {
if (!socket_->connected()) {
return STATE_ERROR_CONNECTION;
}
PacketHeader header;
int headerSize = 0;

View File

@ -29,6 +29,15 @@
namespace metacall {
//
// Constants
//
enum {
SOCKET_BUFFER_SIZE = 1024
};
//
// Stream
//
@ -57,7 +66,7 @@ Stream::State Stream::advance() {
}
if (socket_->wait(Socket::MASK_READ, 0)) {
byte buffRecv[1024];
byte buffRecv[SOCKET_BUFFER_SIZE];
const int bytesRecv = socket_->receive(
buffRecv,
sizeof(buffRecv)
@ -79,6 +88,10 @@ void Stream::reset() {
}
Stream::State Stream::peek(PacketHeader* header, int* headerSize) {
if (!socket_->connected()) {
return STATE_ERROR_CONNECTION;
}
Deserializer deserializer(&buffRecv_);
if (!deserializer.read(header)) {
return STATE_PENDING_PACKET_HEADER;

View File

@ -48,6 +48,7 @@ Token::operator unsigned() const {
}
unsigned Token::hash(const char str[]) {
// djb2 string hash, see http://www.cse.yorku.ca/~oz/hash.html
unsigned hash = 5381;
for (const char* ptr = str; *ptr != 0; ++ptr) {
hash = (hash << 5) + hash + *ptr;