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 #ifdef _WIN32
typedef unsigned __int8 byte;
typedef unsigned __int16 word; typedef unsigned __int8 byte;
typedef unsigned __int32 dword; typedef unsigned __int16 word;
typedef unsigned __int64 qword; typedef unsigned __int32 dword;
typedef unsigned __int64 qword;
#else #else
typedef unsigned char byte;
typedef unsigned short word; typedef unsigned char byte;
typedef unsigned int dword; typedef unsigned short word;
typedef unsigned long int qword; typedef unsigned int dword;
typedef unsigned long int qword;
#endif #endif

View File

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

View File

@ -27,7 +27,7 @@ namespace metacall {
template <typename P1> template <typename P1>
TaskId Protocol::invoke(const Token& token, const P1& p1) { Protocol::TaskId Protocol::invoke(const Token& token, const P1& p1) {
PacketInvokeRequest packetRequest; PacketInvokeRequest packetRequest;
Serializer serializer(&packetRequest.data); Serializer serializer(&packetRequest.data);
@ -37,7 +37,7 @@ TaskId Protocol::invoke(const Token& token, const P1& p1) {
} }
template <typename P1, typename P2> 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; PacketInvokeRequest packetRequest;
Serializer serializer(&packetRequest.data); 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> 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; PacketInvokeRequest packetRequest;
Serializer serializer(&packetRequest.data); 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> 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; PacketInvokeRequest packetRequest;
Serializer serializer(&packetRequest.data); 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> 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; PacketInvokeRequest packetRequest;
Serializer serializer(&packetRequest.data); 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> 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; PacketInvokeRequest packetRequest;
Serializer serializer(&packetRequest.data); 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> 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; PacketInvokeRequest packetRequest;
Serializer serializer(&packetRequest.data); 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> 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; PacketInvokeRequest packetRequest;
Serializer serializer(&packetRequest.data); 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> 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; PacketInvokeRequest packetRequest;
Serializer serializer(&packetRequest.data); 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> 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); TaskMap::const_iterator iter = taskMap_.find(id);
if (iter == taskMap_.end()) { if (iter == taskMap_.end()) {
return TASK_STATE_UNDEFINED; 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); const TaskMap::const_iterator iter = taskMap_.find(id);
return iter == taskMap_.end() ? TASK_STATE_UNDEFINED : iter->second.state; return iter == taskMap_.end() ? TASK_STATE_UNDEFINED : iter->second.state;
} }
@ -179,12 +179,12 @@ Stream::State Protocol::advanceInvokeRequest() {
return state; return state;
} }
TaskId Protocol::invoke(const Token& token) { Protocol::TaskId Protocol::invoke(const Token& token) {
PacketInvokeRequest packetRequest; PacketInvokeRequest packetRequest;
return invokeExec(token, &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(); const TaskId taskId = registerTaskId();
packetRequest->function = token; packetRequest->function = token;
@ -198,7 +198,7 @@ TaskId Protocol::invokeExec(const Token& token, PacketInvokeRequest* packetReque
return taskId; return taskId;
} }
TaskId Protocol::registerTaskId() { Protocol::TaskId Protocol::registerTaskId() {
static int id = TASK_ID_INVALID; static int id = TASK_ID_INVALID;
while (++id == TASK_ID_INVALID); while (++id == TASK_ID_INVALID);
return static_cast<TaskId>(id); return static_cast<TaskId>(id);

View File

@ -28,35 +28,30 @@
namespace metacall { namespace metacall {
//
// Types
//
enum TaskId {
TASK_ID_INVALID
};
enum TaskState {
TASK_STATE_UNDEFINED,
TASK_STATE_PENDING,
TASK_STATE_READY,
TASK_STATE_ERROR
};
typedef void (*HandlerProc)(
class Protocol* protocol,
TaskId id,
TaskState state,
void* userPtr
);
// //
// Protocol // Protocol
// //
class Protocol { class Protocol {
public: public:
enum TaskId {
TASK_ID_INVALID
};
enum TaskState {
TASK_STATE_UNDEFINED,
TASK_STATE_PENDING,
TASK_STATE_READY,
TASK_STATE_ERROR
};
typedef void (*HandlerProc)(
class Protocol* protocol,
TaskId id,
TaskState state,
void* userPtr
);
Protocol(Stream* stream, Binding* binding); Protocol(Stream* stream, Binding* binding);
void advance (); void advance ();

View File

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

View File

@ -28,33 +28,28 @@
namespace metacall { namespace metacall {
//
// Types
//
enum ClientId {
CLIENT_ID_INVALID
};
struct ClientData {
ClientData() :
id(CLIENT_ID_INVALID),
protocol(NULL)
{
}
ClientId id;
std::string name;
Protocol* protocol;
};
// //
// Server // Server
// //
class Server { class Server {
public: public:
enum ClientId {
CLIENT_ID_INVALID
};
struct ClientData {
ClientData() :
id(CLIENT_ID_INVALID),
protocol(NULL)
{
}
ClientId id;
std::string name;
Protocol* protocol;
};
Server(); Server();
~Server(); ~Server();

View File

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

View File

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

View File

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

View File

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