Code cleanup
This commit is contained in:
parent
75ab0b2c99
commit
c9a663c03c
@ -32,8 +32,8 @@ namespace metacall {
|
||||
|
||||
template <typename R>
|
||||
bool Binding::bind(const Token& token, R(*function)()) {
|
||||
if (functors_.find(token) == functors_.end()) {
|
||||
functors_[token] = new Functor0p<R>(function);
|
||||
if (m_functors.find(token) == m_functors.end()) {
|
||||
m_functors[token] = new Functor0p<R>(function);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -42,8 +42,8 @@ bool Binding::bind(const Token& token, R(*function)()) {
|
||||
|
||||
template <typename R, typename P1>
|
||||
bool Binding::bind(const Token& token, R(*function)(P1)) {
|
||||
if (functors_.find(token) == functors_.end()) {
|
||||
functors_[token] = new Functor1p<R, P1>(function);
|
||||
if (m_functors.find(token) == m_functors.end()) {
|
||||
m_functors[token] = new Functor1p<R, P1>(function);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -52,8 +52,8 @@ bool Binding::bind(const Token& token, R(*function)(P1)) {
|
||||
|
||||
template <typename R, typename P1, typename P2>
|
||||
bool Binding::bind(const Token& token, R(*function)(P1, P2)) {
|
||||
if (functors_.find(token) == functors_.end()) {
|
||||
functors_[token] = new Functor2p<R, P1, P2>(function);
|
||||
if (m_functors.find(token) == m_functors.end()) {
|
||||
m_functors[token] = new Functor2p<R, P1, P2>(function);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -62,8 +62,8 @@ bool Binding::bind(const Token& token, R(*function)(P1, P2)) {
|
||||
|
||||
template <typename R, typename P1, typename P2, typename P3>
|
||||
bool Binding::bind(const Token& token, R(*function)(P1, P2, P3)) {
|
||||
if (functors_.find(token) == functors_.end()) {
|
||||
functors_[token] = new Functor3p<R, P1, P2, P3>(function);
|
||||
if (m_functors.find(token) == m_functors.end()) {
|
||||
m_functors[token] = new Functor3p<R, P1, P2, P3>(function);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -72,8 +72,8 @@ bool Binding::bind(const Token& token, R(*function)(P1, P2, P3)) {
|
||||
|
||||
template <typename R, typename P1, typename P2, typename P3, typename P4>
|
||||
bool Binding::bind(const Token& token, R(*function)(P1, P2, P3, P4)) {
|
||||
if (functors_.find(token) == functors_.end()) {
|
||||
functors_[token] = new Functor4p<R, P1, P2, P3, P4>(function);
|
||||
if (m_functors.find(token) == m_functors.end()) {
|
||||
m_functors[token] = new Functor4p<R, P1, P2, P3, P4>(function);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -82,8 +82,8 @@ bool Binding::bind(const Token& token, R(*function)(P1, P2, P3, P4)) {
|
||||
|
||||
template <typename R, typename P1, typename P2, typename P3, typename P4, typename P5>
|
||||
bool Binding::bind(const Token& token, R(*function)(P1, P2, P3, P4, P5)) {
|
||||
if (functors_.find(token) == functors_.end()) {
|
||||
functors_[token] = new Functor5p<R, P1, P2, P3, P4, P5>(function);
|
||||
if (m_functors.find(token) == m_functors.end()) {
|
||||
m_functors[token] = new Functor5p<R, P1, P2, P3, P4, P5>(function);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -92,8 +92,8 @@ bool Binding::bind(const Token& token, R(*function)(P1, P2, P3, P4, P5)) {
|
||||
|
||||
template <typename R, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6>
|
||||
bool Binding::bind(const Token& token, R(*function)(P1, P2, P3, P4, P5, P6)) {
|
||||
if (functors_.find(token) == functors_.end()) {
|
||||
functors_[token] = new Functor6p<R, P1, P2, P3, P4, P5, P6>(function);
|
||||
if (m_functors.find(token) == m_functors.end()) {
|
||||
m_functors[token] = new Functor6p<R, P1, P2, P3, P4, P5, P6>(function);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -102,8 +102,8 @@ bool Binding::bind(const Token& token, R(*function)(P1, P2, P3, P4, P5, P6)) {
|
||||
|
||||
template <typename R, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7>
|
||||
bool Binding::bind(const Token& token, R(*function)(P1, P2, P3, P4, P5, P6, P7)) {
|
||||
if (functors_.find(token) == functors_.end()) {
|
||||
functors_[token] = new Functor7p<R, P1, P2, P3, P4, P5, P6, P7>(function);
|
||||
if (m_functors.find(token) == m_functors.end()) {
|
||||
m_functors[token] = new Functor7p<R, P1, P2, P3, P4, P5, P6, P7>(function);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -112,8 +112,8 @@ bool Binding::bind(const Token& token, R(*function)(P1, P2, P3, P4, P5, P6, P7))
|
||||
|
||||
template <typename R, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8>
|
||||
bool Binding::bind(const Token& token, R(*function)(P1, P2, P3, P4, P5, P6, P7, P8)) {
|
||||
if (functors_.find(token) == functors_.end()) {
|
||||
functors_[token] = new Functor8p<R, P1, P2, P3, P4, P5, P6, P7, P8>(function);
|
||||
if (m_functors.find(token) == m_functors.end()) {
|
||||
m_functors[token] = new Functor8p<R, P1, P2, P3, P4, P5, P6, P7, P8>(function);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -122,8 +122,8 @@ bool Binding::bind(const Token& token, R(*function)(P1, P2, P3, P4, P5, P6, P7,
|
||||
|
||||
template <typename R, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9>
|
||||
bool Binding::bind(const Token& token, R(*function)(P1, P2, P3, P4, P5, P6, P7, P8, P9)) {
|
||||
if (functors_.find(token) == functors_.end()) {
|
||||
functors_[token] = new Functor9p<R, P1, P2, P3, P4, P5, P6, P7, P8, P9>(function);
|
||||
if (m_functors.find(token) == m_functors.end()) {
|
||||
m_functors[token] = new Functor9p<R, P1, P2, P3, P4, P5, P6, P7, P8, P9>(function);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -37,9 +37,9 @@ Binding::~Binding() {
|
||||
}
|
||||
|
||||
Binding::CallResult Binding::call(const Token& token, Deserializer* args, Serializer* ret) const {
|
||||
const FunctorMap::const_iterator iter = functors_.find(token);
|
||||
const FunctorMap::const_iterator iter = m_functors.find(token);
|
||||
|
||||
if (iter == functors_.end()) {
|
||||
if (iter == m_functors.end()) {
|
||||
return CALL_RESULT_UNBOUND_FUNC;
|
||||
}
|
||||
if (!iter->second->call(args, ret)) {
|
||||
@ -50,19 +50,19 @@ Binding::CallResult Binding::call(const Token& token, Deserializer* args, Serial
|
||||
}
|
||||
|
||||
void Binding::unbind(const Token& token) {
|
||||
FunctorMap::iterator iter = functors_.find(token);
|
||||
if (iter != functors_.end()) {
|
||||
FunctorMap::iterator iter = m_functors.find(token);
|
||||
if (iter != m_functors.end()) {
|
||||
delete iter->second;
|
||||
functors_.erase(iter);
|
||||
m_functors.erase(iter);
|
||||
}
|
||||
}
|
||||
|
||||
void Binding::unbindAll() {
|
||||
for (FunctorMap::iterator iter = functors_.begin(); iter != functors_.end(); ++iter) {
|
||||
for (FunctorMap::iterator iter = m_functors.begin(); iter != m_functors.end(); ++iter) {
|
||||
delete iter->second;
|
||||
}
|
||||
|
||||
functors_.clear();
|
||||
m_functors.clear();
|
||||
}
|
||||
|
||||
|
||||
|
@ -43,33 +43,33 @@ public:
|
||||
~Binding();
|
||||
|
||||
template <typename R>
|
||||
bool bind (const Token& token, R(*function)());
|
||||
bool bind(const Token& token, R(*function)());
|
||||
template <typename R, typename P1>
|
||||
bool bind (const Token& token, R(*function)(P1));
|
||||
bool bind(const Token& token, R(*function)(P1));
|
||||
template <typename R, typename P1, typename P2>
|
||||
bool bind (const Token& token, R(*function)(P1, P2));
|
||||
bool bind(const Token& token, R(*function)(P1, P2));
|
||||
template <typename R, typename P1, typename P2, typename P3>
|
||||
bool bind (const Token& token, R(*function)(P1, P2, P3));
|
||||
bool bind(const Token& token, R(*function)(P1, P2, P3));
|
||||
template <typename R, typename P1, typename P2, typename P3, typename P4>
|
||||
bool bind (const Token& token, R(*function)(P1, P2, P3, P4));
|
||||
bool bind(const Token& token, R(*function)(P1, P2, P3, P4));
|
||||
template <typename R, typename P1, typename P2, typename P3, typename P4, typename P5>
|
||||
bool bind (const Token& token, R(*function)(P1, P2, P3, P4, P5));
|
||||
bool bind(const Token& token, R(*function)(P1, P2, P3, P4, P5));
|
||||
template <typename R, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6>
|
||||
bool bind (const Token& token, R(*function)(P1, P2, P3, P4, P5, P6));
|
||||
bool bind(const Token& token, R(*function)(P1, P2, P3, P4, P5, P6));
|
||||
template <typename R, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7>
|
||||
bool bind (const Token& token, R(*function)(P1, P2, P3, P4, P5, P6, P7));
|
||||
bool bind(const Token& token, R(*function)(P1, P2, P3, P4, P5, P6, P7));
|
||||
template <typename R, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8>
|
||||
bool bind (const Token& token, R(*function)(P1, P2, P3, P4, P5, P6, P7, P8));
|
||||
bool bind(const Token& token, R(*function)(P1, P2, P3, P4, P5, P6, P7, P8));
|
||||
template <typename R, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9>
|
||||
bool bind (const Token& token, R(*function)(P1, P2, P3, P4, P5, P6, P7, P8, P9));
|
||||
bool bind(const Token& token, R(*function)(P1, P2, P3, P4, P5, P6, P7, P8, P9));
|
||||
|
||||
CallResult call (const Token& token, Deserializer* args, Serializer* ret) const;
|
||||
void unbind (const Token& token);
|
||||
void unbindAll ();
|
||||
CallResult call(const Token& token, Deserializer* args, Serializer* ret) const;
|
||||
void unbind(const Token& token);
|
||||
void unbindAll();
|
||||
|
||||
private:
|
||||
typedef std::map<unsigned, IFunctor*> FunctorMap;
|
||||
FunctorMap functors_;
|
||||
FunctorMap m_functors;
|
||||
};
|
||||
|
||||
|
||||
|
@ -42,60 +42,62 @@ Buffer::Buffer() {
|
||||
}
|
||||
|
||||
void Buffer::addToBack(const void* data, int bytes) {
|
||||
data_.insert(
|
||||
data_.end(),
|
||||
m_data.insert(
|
||||
m_data.end(),
|
||||
static_cast<const byte*>(data),
|
||||
static_cast<const byte*>(data) + bytes
|
||||
);
|
||||
}
|
||||
|
||||
void Buffer::addToFront(const void* data, int bytes) {
|
||||
data_.insert(
|
||||
data_.begin(),
|
||||
m_data.insert(
|
||||
m_data.begin(),
|
||||
static_cast<const byte*>(data),
|
||||
static_cast<const byte*>(data) + bytes
|
||||
);
|
||||
}
|
||||
|
||||
int Buffer::removeFromFront(void* data, int bytes) {
|
||||
bytes = std::min(static_cast<size_t>(bytes), data_.size());
|
||||
bytes = std::min(static_cast<size_t>(bytes), m_data.size());
|
||||
if (data != NULL) {
|
||||
memcpy(data, &data_[0], bytes);
|
||||
memcpy(data, &m_data[0], bytes);
|
||||
}
|
||||
data_.erase(data_.begin(), data_.begin() + bytes);
|
||||
|
||||
m_data.erase(m_data.begin(), m_data.begin() + bytes);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
int Buffer::removeFromBack(void* data, int bytes) {
|
||||
bytes = std::min(static_cast<size_t>(bytes), data_.size());
|
||||
bytes = std::min(static_cast<size_t>(bytes), m_data.size());
|
||||
if (data != NULL) {
|
||||
memcpy(data, &data_[data_.size() - bytes], bytes);
|
||||
memcpy(data, &m_data[m_data.size() - bytes], bytes);
|
||||
}
|
||||
data_.erase(data_.end() - bytes, data_.end());
|
||||
|
||||
m_data.erase(m_data.end() - bytes, m_data.end());
|
||||
return bytes;
|
||||
}
|
||||
|
||||
void Buffer::clear() {
|
||||
data_.clear();
|
||||
m_data.clear();
|
||||
}
|
||||
|
||||
void Buffer::setData(const void* data, int bytes) {
|
||||
data_.assign(
|
||||
m_data.assign(
|
||||
static_cast<const byte*>(data),
|
||||
static_cast<const byte*>(data) + bytes
|
||||
);
|
||||
}
|
||||
|
||||
const void* Buffer::data() const {
|
||||
return bytes() == 0 ? NULL : &data_[0];
|
||||
return bytes() == 0 ? NULL : &m_data[0];
|
||||
}
|
||||
|
||||
void* Buffer::data() {
|
||||
return bytes() == 0 ? NULL : &data_[0];
|
||||
return bytes() == 0 ? NULL : &m_data[0];
|
||||
}
|
||||
|
||||
int Buffer::bytes() const {
|
||||
return data_.size();
|
||||
return m_data.size();
|
||||
}
|
||||
|
||||
bool Buffer::serialize(Serializer* serializer) const {
|
||||
|
@ -37,22 +37,22 @@ public:
|
||||
Buffer(const void* data, int bytes);
|
||||
Buffer();
|
||||
|
||||
void addToBack (const void* data, int bytes);
|
||||
void addToFront (const void* data, int bytes);
|
||||
int removeFromFront (void* data, int bytes);
|
||||
int removeFromBack (void* data, int bytes);
|
||||
void clear ();
|
||||
void addToBack(const void* data, int bytes);
|
||||
void addToFront(const void* data, int bytes);
|
||||
int removeFromFront(void* data, int bytes);
|
||||
int removeFromBack(void* data, int bytes);
|
||||
void clear();
|
||||
|
||||
void setData (const void* data, int bytes);
|
||||
const void* data () const;
|
||||
void* data ();
|
||||
int bytes () const;
|
||||
void setData(const void* data, int bytes);
|
||||
const void* data() const;
|
||||
void* data();
|
||||
int bytes() const;
|
||||
|
||||
bool serialize (Serializer* serializer) const;
|
||||
bool deserialize (Deserializer* deserializer);
|
||||
bool serialize(Serializer* serializer) const;
|
||||
bool deserialize(Deserializer* deserializer);
|
||||
|
||||
private:
|
||||
std::vector<byte> data_;
|
||||
std::vector<byte> m_data;
|
||||
};
|
||||
|
||||
|
||||
|
@ -34,18 +34,18 @@ namespace metacall {
|
||||
//
|
||||
|
||||
Client::Client() :
|
||||
stream_(&socket_),
|
||||
protocol_(&stream_, &binding_)
|
||||
m_stream(&m_socket),
|
||||
m_protocol(&m_stream, &m_binding)
|
||||
{
|
||||
}
|
||||
|
||||
void Client::advance() {
|
||||
if (!socket_.opened()) {
|
||||
if (!m_socket.opened()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (socket_.connected()) {
|
||||
protocol_.advance();
|
||||
if (m_socket.connected()) {
|
||||
m_protocol.advance();
|
||||
}
|
||||
else {
|
||||
disconnect();
|
||||
@ -55,42 +55,42 @@ void Client::advance() {
|
||||
bool Client::connect(const char name[], int port) {
|
||||
disconnect();
|
||||
|
||||
socket_.open();
|
||||
socket_.setNagle(false);
|
||||
m_socket.open();
|
||||
m_socket.setNagle(false);
|
||||
|
||||
if (!socket_.connect(name, port)) {
|
||||
if (!m_socket.connect(name, port)) {
|
||||
disconnect();
|
||||
return false;
|
||||
}
|
||||
|
||||
socket_.setBlocking(false);
|
||||
m_socket.setBlocking(false);
|
||||
return true;
|
||||
}
|
||||
|
||||
void Client::disconnect() {
|
||||
protocol_.clearHandlers();
|
||||
stream_.reset();
|
||||
socket_.close();
|
||||
m_protocol.clearHandlers();
|
||||
m_stream.reset();
|
||||
m_socket.close();
|
||||
}
|
||||
|
||||
bool Client::connected() const {
|
||||
return socket_.connected();
|
||||
return m_socket.connected();
|
||||
}
|
||||
|
||||
const Protocol* Client::protocol() const {
|
||||
return &protocol_;
|
||||
return &m_protocol;
|
||||
}
|
||||
|
||||
Protocol* Client::protocol() {
|
||||
return &protocol_;
|
||||
return &m_protocol;
|
||||
}
|
||||
|
||||
const Binding* Client::binding() const {
|
||||
return &binding_;
|
||||
return &m_binding;
|
||||
}
|
||||
|
||||
Binding* Client::binding() {
|
||||
return &binding_;
|
||||
return &m_binding;
|
||||
}
|
||||
|
||||
|
||||
|
@ -36,22 +36,21 @@ class Client {
|
||||
public:
|
||||
Client();
|
||||
|
||||
void advance ();
|
||||
void advance();
|
||||
bool connect(const char name[], int port);
|
||||
void disconnect();
|
||||
bool connected() const;
|
||||
|
||||
bool connect (const char name[], int port);
|
||||
void disconnect ();
|
||||
bool connected () const;
|
||||
|
||||
const Protocol* protocol () const;
|
||||
Protocol* protocol ();
|
||||
const Binding* binding () const;
|
||||
Binding* binding ();
|
||||
const Protocol* protocol() const;
|
||||
Protocol* protocol();
|
||||
const Binding* binding() const;
|
||||
Binding* binding();
|
||||
|
||||
private:
|
||||
Socket socket_;
|
||||
Stream stream_;
|
||||
Binding binding_;
|
||||
Protocol protocol_;
|
||||
Socket m_socket;
|
||||
Stream m_stream;
|
||||
Binding m_binding;
|
||||
Protocol m_protocol;
|
||||
};
|
||||
|
||||
|
||||
|
120
mc_functor.hpp
120
mc_functor.hpp
@ -49,17 +49,17 @@ public:
|
||||
typedef R(*F)();
|
||||
|
||||
Functor0p(F function) :
|
||||
function_(function)
|
||||
m_function(function)
|
||||
{
|
||||
}
|
||||
|
||||
bool call(Deserializer*, Serializer* ret) const {
|
||||
ret->write(function_());
|
||||
ret->write(m_function());
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
F function_;
|
||||
F m_function;
|
||||
};
|
||||
|
||||
template <>
|
||||
@ -68,17 +68,17 @@ public:
|
||||
typedef void(*F)();
|
||||
|
||||
Functor0p(F function) :
|
||||
function_(function)
|
||||
m_function(function)
|
||||
{
|
||||
}
|
||||
|
||||
bool call(Deserializer*, Serializer*) const {
|
||||
function_();
|
||||
m_function();
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
F function_;
|
||||
F m_function;
|
||||
};
|
||||
|
||||
|
||||
@ -92,7 +92,7 @@ public:
|
||||
typedef R (*F)(P1);
|
||||
|
||||
Functor1p(F function) :
|
||||
function_(function)
|
||||
m_function(function)
|
||||
{
|
||||
}
|
||||
|
||||
@ -101,14 +101,14 @@ public:
|
||||
|
||||
const bool success = args->read(&p1);
|
||||
if (success) {
|
||||
ret->write(function_(p1));
|
||||
ret->write(m_function(p1));
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
private:
|
||||
F function_;
|
||||
F m_function;
|
||||
};
|
||||
|
||||
template <typename P1>
|
||||
@ -117,7 +117,7 @@ public:
|
||||
typedef void (*F)(P1);
|
||||
|
||||
Functor1p(F function) :
|
||||
function_(function)
|
||||
m_function(function)
|
||||
{
|
||||
}
|
||||
|
||||
@ -126,14 +126,14 @@ public:
|
||||
|
||||
const bool success = args->read(&p1);
|
||||
if (success) {
|
||||
function_(p1);
|
||||
m_function(p1);
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
private:
|
||||
F function_;
|
||||
F m_function;
|
||||
};
|
||||
|
||||
|
||||
@ -147,7 +147,7 @@ public:
|
||||
typedef R (*F)(P1, P2);
|
||||
|
||||
Functor2p(F function) :
|
||||
function_(function)
|
||||
m_function(function)
|
||||
{
|
||||
}
|
||||
|
||||
@ -160,14 +160,14 @@ public:
|
||||
args->read(&p2);
|
||||
|
||||
if (success) {
|
||||
ret->write(function_(p1, p2));
|
||||
ret->write(m_function(p1, p2));
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
private:
|
||||
F function_;
|
||||
F m_function;
|
||||
};
|
||||
|
||||
template <typename P1, typename P2>
|
||||
@ -176,7 +176,7 @@ public:
|
||||
typedef void (*F)(P1, P2);
|
||||
|
||||
Functor2p(F function) :
|
||||
function_(function)
|
||||
m_function(function)
|
||||
{
|
||||
}
|
||||
|
||||
@ -189,14 +189,14 @@ public:
|
||||
args->read(&p2);
|
||||
|
||||
if (success) {
|
||||
function_(p1, p2);
|
||||
m_function(p1, p2);
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
private:
|
||||
F function_;
|
||||
F m_function;
|
||||
};
|
||||
|
||||
|
||||
@ -210,7 +210,7 @@ public:
|
||||
typedef R (*F)(P1, P2, P3);
|
||||
|
||||
Functor3p(F function) :
|
||||
function_(function)
|
||||
m_function(function)
|
||||
{
|
||||
}
|
||||
|
||||
@ -225,14 +225,14 @@ public:
|
||||
args->read(&p3);
|
||||
|
||||
if (success) {
|
||||
ret->write(function_(p1, p2, p3));
|
||||
ret->write(m_function(p1, p2, p3));
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
private:
|
||||
F function_;
|
||||
F m_function;
|
||||
};
|
||||
|
||||
template <typename P1, typename P2, typename P3>
|
||||
@ -241,7 +241,7 @@ public:
|
||||
typedef void (*F)(P1, P2, P3);
|
||||
|
||||
Functor3p(F function) :
|
||||
function_(function)
|
||||
m_function(function)
|
||||
{
|
||||
}
|
||||
|
||||
@ -256,14 +256,14 @@ public:
|
||||
args->read(&p3);
|
||||
|
||||
if (success) {
|
||||
function_(p1, p2, p3);
|
||||
m_function(p1, p2, p3);
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
private:
|
||||
F function_;
|
||||
F m_function;
|
||||
};
|
||||
|
||||
|
||||
@ -277,7 +277,7 @@ public:
|
||||
typedef R (*F)(P1, P2, P3, P4);
|
||||
|
||||
Functor4p(F function) :
|
||||
function_(function)
|
||||
m_function(function)
|
||||
{
|
||||
}
|
||||
|
||||
@ -294,14 +294,14 @@ public:
|
||||
args->read(&p4);
|
||||
|
||||
if (success) {
|
||||
ret->write(function_(p1, p2, p3, p4));
|
||||
ret->write(m_function(p1, p2, p3, p4));
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
private:
|
||||
F function_;
|
||||
F m_function;
|
||||
};
|
||||
|
||||
template <typename P1, typename P2, typename P3, typename P4>
|
||||
@ -310,7 +310,7 @@ public:
|
||||
typedef void (*F)(P1, P2, P3, P4);
|
||||
|
||||
Functor4p(F function) :
|
||||
function_(function)
|
||||
m_function(function)
|
||||
{
|
||||
}
|
||||
|
||||
@ -327,14 +327,14 @@ public:
|
||||
args->read(&p4);
|
||||
|
||||
if (success) {
|
||||
function_(p1, p2, p3, p4);
|
||||
m_function(p1, p2, p3, p4);
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
private:
|
||||
F function_;
|
||||
F m_function;
|
||||
};
|
||||
|
||||
|
||||
@ -348,7 +348,7 @@ public:
|
||||
typedef R (*F)(P1, P2, P3, P4, P5);
|
||||
|
||||
Functor5p(F function) :
|
||||
function_(function)
|
||||
m_function(function)
|
||||
{
|
||||
}
|
||||
|
||||
@ -367,14 +367,14 @@ public:
|
||||
args->read(&p5);
|
||||
|
||||
if (success) {
|
||||
ret->write(function_(p1, p2, p3, p4, p5));
|
||||
ret->write(m_function(p1, p2, p3, p4, p5));
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
private:
|
||||
F function_;
|
||||
F m_function;
|
||||
};
|
||||
|
||||
template <typename P1, typename P2, typename P3, typename P4, typename P5>
|
||||
@ -383,7 +383,7 @@ public:
|
||||
typedef void (*F)(P1, P2, P3, P4, P5);
|
||||
|
||||
Functor5p(F function) :
|
||||
function_(function)
|
||||
m_function(function)
|
||||
{
|
||||
}
|
||||
|
||||
@ -402,14 +402,14 @@ public:
|
||||
args->read(&p5);
|
||||
|
||||
if (success) {
|
||||
function_(p1, p2, p3, p4, p5);
|
||||
m_function(p1, p2, p3, p4, p5);
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
private:
|
||||
F function_;
|
||||
F m_function;
|
||||
};
|
||||
|
||||
|
||||
@ -423,7 +423,7 @@ public:
|
||||
typedef R (*F)(P1, P2, P3, P4, P5, P6);
|
||||
|
||||
Functor6p(F function) :
|
||||
function_(function)
|
||||
m_function(function)
|
||||
{
|
||||
}
|
||||
|
||||
@ -444,14 +444,14 @@ public:
|
||||
args->read(&p6);
|
||||
|
||||
if (success) {
|
||||
ret->write(function_(p1, p2, p3, p4, p5, p6));
|
||||
ret->write(m_function(p1, p2, p3, p4, p5, p6));
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
private:
|
||||
F function_;
|
||||
F m_function;
|
||||
};
|
||||
|
||||
template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6>
|
||||
@ -460,7 +460,7 @@ public:
|
||||
typedef void (*F)(P1, P2, P3, P4, P5, P6);
|
||||
|
||||
Functor6p(F function) :
|
||||
function_(function)
|
||||
m_function(function)
|
||||
{
|
||||
}
|
||||
|
||||
@ -481,14 +481,14 @@ public:
|
||||
args->read(&p6);
|
||||
|
||||
if (success) {
|
||||
function_(p1, p2, p3, p4, p5, p6);
|
||||
m_function(p1, p2, p3, p4, p5, p6);
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
private:
|
||||
F function_;
|
||||
F m_function;
|
||||
};
|
||||
|
||||
|
||||
@ -502,7 +502,7 @@ public:
|
||||
typedef R (*F)(P1, P2, P3, P4, P5, P6, P7);
|
||||
|
||||
Functor7p(F function) :
|
||||
function_(function)
|
||||
m_function(function)
|
||||
{
|
||||
}
|
||||
|
||||
@ -525,14 +525,14 @@ public:
|
||||
args->read(&p7);
|
||||
|
||||
if (success) {
|
||||
ret->write(function_(p1, p2, p3, p4, p5, p6, p7));
|
||||
ret->write(m_function(p1, p2, p3, p4, p5, p6, p7));
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
private:
|
||||
F function_;
|
||||
F m_function;
|
||||
};
|
||||
|
||||
template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7>
|
||||
@ -541,7 +541,7 @@ public:
|
||||
typedef void (*F)(P1, P2, P3, P4, P5, P6, P7);
|
||||
|
||||
Functor7p(F function) :
|
||||
function_(function)
|
||||
m_function(function)
|
||||
{
|
||||
}
|
||||
|
||||
@ -564,14 +564,14 @@ public:
|
||||
args->read(&p7);
|
||||
|
||||
if (success) {
|
||||
function_(p1, p2, p3, p4, p5, p6, p7);
|
||||
m_function(p1, p2, p3, p4, p5, p6, p7);
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
private:
|
||||
F function_;
|
||||
F m_function;
|
||||
};
|
||||
|
||||
|
||||
@ -585,7 +585,7 @@ public:
|
||||
typedef R (*F)(P1, P2, P3, P4, P5, P6, P7, P8);
|
||||
|
||||
Functor8p(F function) :
|
||||
function_(function)
|
||||
m_function(function)
|
||||
{
|
||||
}
|
||||
|
||||
@ -610,14 +610,14 @@ public:
|
||||
args->read(&p8);
|
||||
|
||||
if (success) {
|
||||
ret->write(function_(p1, p2, p3, p4, p5, p6, p7, p8));
|
||||
ret->write(m_function(p1, p2, p3, p4, p5, p6, p7, p8));
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
private:
|
||||
F function_;
|
||||
F m_function;
|
||||
};
|
||||
|
||||
template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8>
|
||||
@ -626,7 +626,7 @@ public:
|
||||
typedef void (*F)(P1, P2, P3, P4, P5, P6, P7, P8);
|
||||
|
||||
Functor8p(F function) :
|
||||
function_(function)
|
||||
m_function(function)
|
||||
{
|
||||
}
|
||||
|
||||
@ -651,14 +651,14 @@ public:
|
||||
args->read(&p8);
|
||||
|
||||
if (success) {
|
||||
function_(p1, p2, p3, p4, p5, p6, p7, p8);
|
||||
m_function(p1, p2, p3, p4, p5, p6, p7, p8);
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
private:
|
||||
F function_;
|
||||
F m_function;
|
||||
};
|
||||
|
||||
|
||||
@ -672,7 +672,7 @@ public:
|
||||
typedef R (*F)(P1, P2, P3, P4, P5, P6, P7, P8, P9);
|
||||
|
||||
Functor9p(F function) :
|
||||
function_(function)
|
||||
m_function(function)
|
||||
{
|
||||
}
|
||||
|
||||
@ -699,14 +699,14 @@ public:
|
||||
args->read(&p9);
|
||||
|
||||
if (success) {
|
||||
ret->write(function_(p1, p2, p3, p4, p5, p6, p7, p8, p9));
|
||||
ret->write(m_function(p1, p2, p3, p4, p5, p6, p7, p8, p9));
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
private:
|
||||
F function_;
|
||||
F m_function;
|
||||
};
|
||||
|
||||
template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9>
|
||||
@ -715,7 +715,7 @@ public:
|
||||
typedef void (*F)(P1, P2, P3, P4, P5, P6, P7, P8, P9);
|
||||
|
||||
Functor9p(F function) :
|
||||
function_(function)
|
||||
m_function(function)
|
||||
{
|
||||
}
|
||||
|
||||
@ -742,14 +742,14 @@ public:
|
||||
args->read(&p9);
|
||||
|
||||
if (success) {
|
||||
function_(p1, p2, p3, p4, p5, p6, p7, p8, p9);
|
||||
m_function(p1, p2, p3, p4, p5, p6, p7, p8, p9);
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
private:
|
||||
F function_;
|
||||
F m_function;
|
||||
};
|
||||
|
||||
|
||||
|
@ -154,8 +154,8 @@ Protocol::TaskId Protocol::invoke(const Token& token, const P1& p1, const P2& p2
|
||||
|
||||
template <typename R>
|
||||
Protocol::TaskState Protocol::queryResult(TaskId id, R* result) {
|
||||
TaskMap::const_iterator iter = taskMap_.find(id);
|
||||
if (iter == taskMap_.end()) {
|
||||
TaskMap::const_iterator iter = m_taskMap.find(id);
|
||||
if (iter == m_taskMap.end()) {
|
||||
return TASK_STATE_UNDEFINED;
|
||||
}
|
||||
|
||||
|
@ -34,25 +34,25 @@ namespace metacall {
|
||||
//
|
||||
|
||||
Protocol::Protocol(Stream* stream, Binding* binding) :
|
||||
stream_(stream),
|
||||
binding_(binding),
|
||||
rate_(64)
|
||||
m_stream(stream),
|
||||
m_binding(binding),
|
||||
m_rate(64)
|
||||
{
|
||||
}
|
||||
|
||||
void Protocol::advance() {
|
||||
if (!stream_->socket()->connected()) {
|
||||
if (!m_stream->socket()->connected()) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < rate_; ++i) {
|
||||
for (int i = 0; i < m_rate; ++i) {
|
||||
if (advanceStream() != Stream::STATE_READY) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<TaskMap::iterator> processed;
|
||||
for (TaskMap::iterator iter = taskMap_.begin(); iter != taskMap_.end(); ++iter) {
|
||||
for (TaskMap::iterator iter = m_taskMap.begin(); iter != m_taskMap.end(); ++iter) {
|
||||
TaskEntry& entry = iter->second;
|
||||
switch (entry.state) {
|
||||
case TASK_STATE_READY:
|
||||
@ -69,21 +69,21 @@ void Protocol::advance() {
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < processed.size(); ++i) {
|
||||
taskMap_.erase(processed[i]);
|
||||
m_taskMap.erase(processed[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void Protocol::setRate(int rate) {
|
||||
rate_ = rate;
|
||||
m_rate = rate;
|
||||
}
|
||||
|
||||
bool Protocol::pendingTasks() const {
|
||||
return taskMap_.size() > 0;
|
||||
return m_taskMap.size() > 0;
|
||||
}
|
||||
|
||||
bool Protocol::setHandler(TaskId id, HandlerProc handler, void* userPtr) {
|
||||
const TaskMap::iterator iter = taskMap_.find(id);
|
||||
if (iter == taskMap_.end() || iter->second.state != TASK_STATE_PENDING) {
|
||||
const TaskMap::iterator iter = m_taskMap.find(id);
|
||||
if (iter == m_taskMap.end() || iter->second.state != TASK_STATE_PENDING) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -92,25 +92,25 @@ bool Protocol::setHandler(TaskId id, HandlerProc handler, void* userPtr) {
|
||||
}
|
||||
|
||||
void Protocol::clearHandler(TaskId id) {
|
||||
const TaskMap::iterator iter = taskMap_.find(id);
|
||||
if (iter != taskMap_.end()) {
|
||||
taskMap_.erase(iter);
|
||||
const TaskMap::iterator iter = m_taskMap.find(id);
|
||||
if (iter != m_taskMap.end()) {
|
||||
m_taskMap.erase(iter);
|
||||
}
|
||||
}
|
||||
|
||||
void Protocol::clearHandlers() {
|
||||
for (TaskMap::iterator iter = taskMap_.begin(); iter != taskMap_.end(); ++iter) {
|
||||
for (TaskMap::iterator iter = m_taskMap.begin(); iter != m_taskMap.end(); ++iter) {
|
||||
iter->second.handlers.clear();
|
||||
}
|
||||
}
|
||||
|
||||
Stream::State Protocol::advanceStream() {
|
||||
stream_->advance();
|
||||
m_stream->advance();
|
||||
|
||||
PacketHeader header;
|
||||
int headerSize = 0;
|
||||
|
||||
Stream::State state = stream_->peek(&header, &headerSize);
|
||||
Stream::State state = m_stream->peek(&header, &headerSize);
|
||||
if (state == Stream::STATE_READY) {
|
||||
switch (header.id) {
|
||||
case PACKET_ID_FUNCTION_CALL_REQUEST:
|
||||
@ -132,10 +132,10 @@ Stream::State Protocol::advanceInvokeReply() {
|
||||
PacketInvokeReply packetReply;
|
||||
Deserializer deserializer(&packetReply.data);
|
||||
|
||||
Stream::State state = stream_->receive(&packetReply);
|
||||
Stream::State state = m_stream->receive(&packetReply);
|
||||
if (state == Stream::STATE_READY) {
|
||||
const TaskMap::iterator iter = taskMap_.find(static_cast<TaskId>(packetReply.taskId));
|
||||
if (iter == taskMap_.end()) {
|
||||
const TaskMap::iterator iter = m_taskMap.find(static_cast<TaskId>(packetReply.taskId));
|
||||
if (iter == m_taskMap.end()) {
|
||||
state = Stream::STATE_ERROR_PROTOCOL;
|
||||
}
|
||||
else {
|
||||
@ -155,13 +155,13 @@ Stream::State Protocol::advanceInvokeRequest() {
|
||||
PacketInvokeRequest packetRequest;
|
||||
Deserializer deserializer(&packetRequest.data);
|
||||
|
||||
const Stream::State state = stream_->receive(&packetRequest);
|
||||
const Stream::State state = m_stream->receive(&packetRequest);
|
||||
if (state == Stream::STATE_READY) {
|
||||
PacketInvokeReply packetReply;
|
||||
packetReply.taskId = packetRequest.taskId;
|
||||
|
||||
Serializer serializer(&packetReply.data);
|
||||
switch (binding_->call(packetRequest.function, &deserializer, &serializer)) {
|
||||
switch (m_binding->call(packetRequest.function, &deserializer, &serializer)) {
|
||||
case Binding::CALL_RESULT_INVALID_ARGS:
|
||||
packetReply.flags |= PacketInvokeReply::FLAG_INVALID_ARGS;
|
||||
break;
|
||||
@ -172,7 +172,7 @@ Stream::State Protocol::advanceInvokeRequest() {
|
||||
break;
|
||||
}
|
||||
|
||||
stream_->send(packetReply);
|
||||
m_stream->send(packetReply);
|
||||
}
|
||||
|
||||
return state;
|
||||
@ -189,18 +189,18 @@ Protocol::TaskId Protocol::invokeExec(const Token& token, PacketInvokeRequest* p
|
||||
packetRequest->function = token;
|
||||
packetRequest->taskId = taskId;
|
||||
|
||||
if (stream_->send(*packetRequest) != Stream::STATE_READY) {
|
||||
if (m_stream->send(*packetRequest) != Stream::STATE_READY) {
|
||||
return TASK_ID_INVALID;
|
||||
}
|
||||
|
||||
taskMap_[taskId] = TaskEntry();
|
||||
m_taskMap[taskId] = TaskEntry();
|
||||
return taskId;
|
||||
}
|
||||
|
||||
Protocol::TaskId Protocol::registerTaskId() {
|
||||
static int id = TASK_ID_INVALID;
|
||||
while (++id == TASK_ID_INVALID);
|
||||
return static_cast<TaskId>(id);
|
||||
static int s_id = TASK_ID_INVALID;
|
||||
while (++s_id == TASK_ID_INVALID);
|
||||
return static_cast<TaskId>(s_id);
|
||||
}
|
||||
|
||||
|
||||
|
@ -54,36 +54,36 @@ public:
|
||||
|
||||
Protocol(Stream* stream, Binding* binding);
|
||||
|
||||
void advance ();
|
||||
void setRate (int rate);
|
||||
void advance();
|
||||
void setRate(int rate);
|
||||
|
||||
template <typename R>
|
||||
TaskState queryResult (TaskId id, R* result);
|
||||
bool pendingTasks () const;
|
||||
TaskState queryResult(TaskId id, R* result);
|
||||
bool pendingTasks() const;
|
||||
|
||||
bool setHandler (TaskId id, HandlerProc handler, void* userPtr = NULL);
|
||||
void clearHandler (TaskId id);
|
||||
void clearHandlers ();
|
||||
bool setHandler(TaskId id, HandlerProc handler, void* userPtr = NULL);
|
||||
void clearHandler(TaskId id);
|
||||
void clearHandlers();
|
||||
|
||||
TaskId invoke (const Token& token);
|
||||
TaskId invoke(const Token& token);
|
||||
template <typename P1>
|
||||
TaskId invoke (const Token& token, const P1& p0);
|
||||
TaskId invoke(const Token& token, const P1& p0);
|
||||
template <typename P1, typename P2>
|
||||
TaskId invoke (const Token& token, const P1& p0, const P2& p1);
|
||||
TaskId invoke(const Token& token, const P1& p0, const P2& p1);
|
||||
template <typename P1, typename P2, typename P3>
|
||||
TaskId invoke (const Token& token, const P1& p0, const P2& p1, const P3& p2);
|
||||
TaskId invoke(const Token& token, const P1& p0, const P2& p1, const P3& p2);
|
||||
template <typename P1, typename P2, typename P3, typename P4>
|
||||
TaskId invoke (const Token& token, const P1& p0, const P2& p1, const P3& p2, const P4& p3);
|
||||
TaskId invoke(const Token& token, const P1& p0, const P2& p1, const P3& p2, const P4& p3);
|
||||
template <typename P1, typename P2, typename P3, typename P4, typename P5>
|
||||
TaskId invoke (const Token& token, const P1& p0, const P2& p1, const P3& p2, const P4& p3, const P5& p4);
|
||||
TaskId invoke(const Token& token, const P1& p0, const P2& p1, const P3& p2, const P4& p3, const P5& p4);
|
||||
template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6>
|
||||
TaskId invoke (const Token& token, const P1& p0, const P2& p1, const P3& p2, const P4& p3, const P5& p4, const P6& p5);
|
||||
TaskId invoke(const Token& token, const P1& p0, const P2& p1, const P3& p2, const P4& p3, const P5& p4, const P6& p5);
|
||||
template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7>
|
||||
TaskId invoke (const Token& token, const P1& p0, const P2& p1, const P3& p2, const P4& p3, const P5& p4, const P6& p5, const P7& p6);
|
||||
TaskId invoke(const Token& token, const P1& p0, const P2& p1, const P3& p2, const P4& p3, const P5& p4, const P6& p5, const P7& p6);
|
||||
template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8>
|
||||
TaskId invoke (const Token& token, const P1& p0, const P2& p1, const P3& p2, const P4& p3, const P5& p4, const P6& p5, const P7& p6, const P8& p7);
|
||||
TaskId invoke(const Token& token, const P1& p0, const P2& p1, const P3& p2, const P4& p3, const P5& p4, const P6& p5, const P7& p6, const P8& p7);
|
||||
template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9>
|
||||
TaskId invoke (const Token& token, const P1& p0, const P2& p1, const P3& p2, const P4& p3, const P5& p4, const P6& p5, const P7& p6, const P8& p7, const P9& p8);
|
||||
TaskId invoke(const Token& token, const P1& p0, const P2& p1, const P3& p2, const P4& p3, const P5& p4, const P6& p5, const P7& p6, const P8& p7, const P9& p8);
|
||||
|
||||
private:
|
||||
struct HandlerEntry {
|
||||
@ -109,18 +109,18 @@ private:
|
||||
Buffer data;
|
||||
};
|
||||
|
||||
Stream::State advanceStream ();
|
||||
Stream::State advanceStream();
|
||||
Stream::State advanceInvokeRequest();
|
||||
Stream::State advanceInvokeReply ();
|
||||
Stream::State advanceInvokeReply();
|
||||
|
||||
TaskId invokeExec (const Token& token, PacketInvokeRequest* packetRequest);
|
||||
static TaskId registerTaskId ();
|
||||
TaskId invokeExec(const Token& token, PacketInvokeRequest* packetRequest);
|
||||
static TaskId registerTaskId();
|
||||
|
||||
typedef std::map<TaskId, TaskEntry> TaskMap;
|
||||
TaskMap taskMap_;
|
||||
Stream* stream_;
|
||||
Binding* binding_;
|
||||
int rate_;
|
||||
TaskMap m_taskMap;
|
||||
Stream* m_stream;
|
||||
Binding* m_binding;
|
||||
int m_rate;
|
||||
};
|
||||
|
||||
|
||||
|
@ -34,31 +34,31 @@ namespace metacall {
|
||||
//
|
||||
|
||||
Deserializer::Deserializer(const Buffer* data) :
|
||||
data_(data),
|
||||
offset_(0)
|
||||
m_data(data),
|
||||
m_offset(0)
|
||||
{
|
||||
}
|
||||
|
||||
const byte* Deserializer::readRaw(int size) {
|
||||
if (data_ == NULL || size > data_->bytes() - offset_) {
|
||||
if (m_data == NULL || size > m_data->bytes() - m_offset) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const byte* data = static_cast<const byte*>(data_->data()) + offset_;
|
||||
offset_ += size;
|
||||
const byte* data = static_cast<const byte*>(m_data->data()) + m_offset;
|
||||
m_offset += size;
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
int Deserializer::offset() const {
|
||||
return offset_;
|
||||
return m_offset;
|
||||
}
|
||||
|
||||
bool Deserializer::setOffset(int offset, bool relative) {
|
||||
const int offsetNew = relative ? offset_ + offset : offset;
|
||||
const int offsetNew = relative ? m_offset + offset : offset;
|
||||
|
||||
if (offsetNew >= 0 && offsetNew <= data_->bytes()) {
|
||||
offset_ = offsetNew;
|
||||
if (offsetNew >= 0 && offsetNew <= m_data->bytes()) {
|
||||
m_offset = offsetNew;
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -71,25 +71,25 @@ bool Deserializer::setOffset(int offset, bool relative) {
|
||||
//
|
||||
|
||||
Serializer::Serializer(Buffer* data) :
|
||||
data_(data),
|
||||
offset_(0)
|
||||
m_data(data),
|
||||
m_offset(0)
|
||||
{
|
||||
}
|
||||
|
||||
bool Serializer::writeRaw(const void* data, int size) {
|
||||
if (data_ != NULL) {
|
||||
data_->addToBack(static_cast<const byte*>(data), size);
|
||||
offset_ += size;
|
||||
if (m_data != NULL) {
|
||||
m_data->addToBack(static_cast<const byte*>(data), size);
|
||||
m_offset += size;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Serializer::setOffset(int offset, bool relative) {
|
||||
const int offsetNew = relative ? offset_ + offset : offset;
|
||||
const int offsetNew = relative ? m_offset + offset : offset;
|
||||
|
||||
if (offsetNew >= 0 && offsetNew <= data_->bytes()) {
|
||||
offset_ = offsetNew;
|
||||
if (offsetNew >= 0 && offsetNew <= m_data->bytes()) {
|
||||
m_offset = offsetNew;
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -97,7 +97,7 @@ bool Serializer::setOffset(int offset, bool relative) {
|
||||
}
|
||||
|
||||
int Serializer::offset() const {
|
||||
return offset_;
|
||||
return m_offset;
|
||||
}
|
||||
|
||||
|
||||
|
@ -37,14 +37,14 @@ public:
|
||||
Deserializer(const Buffer* data);
|
||||
|
||||
template <typename T>
|
||||
bool read (T* data);
|
||||
const byte* readRaw (int size);
|
||||
bool setOffset (int offset, bool relative);
|
||||
int offset () const;
|
||||
bool read(T* data);
|
||||
const byte* readRaw(int size);
|
||||
bool setOffset(int offset, bool relative);
|
||||
int offset() const;
|
||||
|
||||
private:
|
||||
const Buffer* data_;
|
||||
int offset_;
|
||||
const Buffer* m_data;
|
||||
int m_offset;
|
||||
};
|
||||
|
||||
|
||||
@ -57,14 +57,14 @@ public:
|
||||
Serializer(Buffer* data);
|
||||
|
||||
template <typename T>
|
||||
bool write (const T& data);
|
||||
bool writeRaw (const void* data, int size);
|
||||
bool setOffset (int offset, bool relative);
|
||||
int offset () const;
|
||||
bool write(const T& data);
|
||||
bool writeRaw(const void* data, int size);
|
||||
bool setOffset(int offset, bool relative);
|
||||
int offset() const;
|
||||
|
||||
private:
|
||||
Buffer* data_;
|
||||
int offset_;
|
||||
Buffer* m_data;
|
||||
int m_offset;
|
||||
};
|
||||
|
||||
|
||||
|
@ -34,8 +34,8 @@ namespace metacall {
|
||||
//
|
||||
|
||||
Server::Server() :
|
||||
clientMax_(1),
|
||||
clientActive_(CLIENT_ID_INVALID)
|
||||
m_clientMax(1),
|
||||
m_clientActive(CLIENT_ID_INVALID)
|
||||
{
|
||||
}
|
||||
|
||||
@ -50,20 +50,20 @@ void Server::advance() {
|
||||
}
|
||||
|
||||
const Binding* Server::binding() const {
|
||||
return &binding_;
|
||||
return &m_binding;
|
||||
}
|
||||
|
||||
Binding* Server::binding() {
|
||||
return &binding_;
|
||||
return &m_binding;
|
||||
}
|
||||
|
||||
bool Server::start(int serverPort, int clientsMax) {
|
||||
stop();
|
||||
|
||||
if (server_.open() && server_.bind(serverPort) && server_.listen()) {
|
||||
server_.setBlocking(false);
|
||||
server_.setNagle(false);
|
||||
clientMax_ = clientsMax;
|
||||
if (m_server.open() && m_server.bind(serverPort) && m_server.listen()) {
|
||||
m_server.setBlocking(false);
|
||||
m_server.setNagle(false);
|
||||
m_clientMax = clientsMax;
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -73,18 +73,18 @@ bool Server::start(int serverPort, int clientsMax) {
|
||||
|
||||
void Server::stop() {
|
||||
disconnectAll();
|
||||
clientMax_ = 0;
|
||||
m_clientMax = 0;
|
||||
}
|
||||
|
||||
void Server::getClients(std::vector<ClientId>* clients) const {
|
||||
for (ClientMap::const_iterator iter = clients_.begin(); iter != clients_.end(); ++iter) {
|
||||
void Server::clients(std::vector<ClientId>* clients) const {
|
||||
for (ClientMap::const_iterator iter = m_clients.begin(); iter != m_clients.end(); ++iter) {
|
||||
clients->push_back(iter->first);
|
||||
}
|
||||
}
|
||||
|
||||
bool Server::getClient(ClientId id, ClientData* data) const {
|
||||
const ClientMap::const_iterator iter = clients_.find(id);
|
||||
if (iter == clients_.end()) {
|
||||
bool Server::clients(ClientId id, ClientData* data) const {
|
||||
const ClientMap::const_iterator iter = m_clients.find(id);
|
||||
if (iter == m_clients.end()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -96,45 +96,45 @@ bool Server::getClient(ClientId id, ClientData* data) const {
|
||||
}
|
||||
|
||||
void Server::disconnect(ClientId id) {
|
||||
const ClientMap::iterator iter = clients_.find(id);
|
||||
if (iter != clients_.end()) {
|
||||
const ClientMap::iterator iter = m_clients.find(id);
|
||||
if (iter != m_clients.end()) {
|
||||
delete iter->second;
|
||||
clients_.erase(iter);
|
||||
m_clients.erase(iter);
|
||||
}
|
||||
}
|
||||
|
||||
void Server::disconnectAll() {
|
||||
for (ClientMap::iterator iter = clients_.begin(); iter != clients_.end(); ++iter) {
|
||||
for (ClientMap::iterator iter = m_clients.begin(); iter != m_clients.end(); ++iter) {
|
||||
delete iter->second;
|
||||
}
|
||||
|
||||
clients_.clear();
|
||||
m_clients.clear();
|
||||
}
|
||||
|
||||
int Server::clientCount() const {
|
||||
return clients_.size();
|
||||
return m_clients.size();
|
||||
}
|
||||
|
||||
Server::ClientId Server::clientActive() const {
|
||||
return clientActive_;
|
||||
return m_clientActive;
|
||||
}
|
||||
|
||||
void Server::advanceConnecting() {
|
||||
if (clientMax_ == 0 || !server_.opened()) {
|
||||
if (m_clientMax == 0 || !m_server.opened()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Socket client;
|
||||
if (!server_.accept(&client)) {
|
||||
if (!m_server.accept(&client)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const std::pair<ClientId, ClientEntry*> entry(
|
||||
registerClientId(),
|
||||
new ClientEntry(&binding_)
|
||||
new ClientEntry(&m_binding)
|
||||
);
|
||||
|
||||
const ClientMap::iterator iter = clients_.insert(entry).first;
|
||||
const ClientMap::iterator iter = m_clients.insert(entry).first;
|
||||
iter->second->socket.set(client.release());
|
||||
iter->second->socket.setNagle(false);
|
||||
iter->second->socket.setBlocking(false);
|
||||
@ -142,7 +142,7 @@ void Server::advanceConnecting() {
|
||||
|
||||
void Server::advanceDisconnecting() {
|
||||
std::vector<ClientMap::iterator> clients;
|
||||
for (ClientMap::iterator iter = clients_.begin(); iter != clients_.end(); ++iter) {
|
||||
for (ClientMap::iterator iter = m_clients.begin(); iter != m_clients.end(); ++iter) {
|
||||
if (!iter->second->socket.connected()) {
|
||||
clients.push_back(iter);
|
||||
}
|
||||
@ -154,20 +154,20 @@ void Server::advanceDisconnecting() {
|
||||
}
|
||||
|
||||
void Server::advanceConnected() {
|
||||
ASSERT(clientActive_ == CLIENT_ID_INVALID);
|
||||
ASSERT(m_clientActive == CLIENT_ID_INVALID);
|
||||
|
||||
for (ClientMap::iterator iter = clients_.begin(); iter != clients_.end(); ++iter) {
|
||||
clientActive_ = iter->first;
|
||||
for (ClientMap::iterator iter = m_clients.begin(); iter != m_clients.end(); ++iter) {
|
||||
m_clientActive = iter->first;
|
||||
iter->second->protocol.advance();
|
||||
}
|
||||
|
||||
clientActive_ = CLIENT_ID_INVALID;
|
||||
m_clientActive = CLIENT_ID_INVALID;
|
||||
}
|
||||
|
||||
Server::ClientId Server::registerClientId() {
|
||||
static int id = CLIENT_ID_INVALID;
|
||||
while (++id == CLIENT_ID_INVALID);
|
||||
return static_cast<ClientId>(id);
|
||||
static int s_id = CLIENT_ID_INVALID;
|
||||
while (++s_id == CLIENT_ID_INVALID);
|
||||
return static_cast<ClientId>(s_id);
|
||||
}
|
||||
|
||||
|
||||
|
@ -53,18 +53,18 @@ public:
|
||||
Server();
|
||||
~Server();
|
||||
|
||||
void advance ();
|
||||
bool start (int serverPort, int clientsMax = 1);
|
||||
void stop ();
|
||||
void disconnect (ClientId id);
|
||||
void disconnectAll ();
|
||||
void advance();
|
||||
bool start(int serverPort, int clientsMax = 1);
|
||||
void stop();
|
||||
void disconnect(ClientId id);
|
||||
void disconnectAll();
|
||||
|
||||
void getClients (std::vector<ClientId>* clients) const;
|
||||
bool getClient (ClientId id, ClientData* data) const;
|
||||
ClientId clientActive () const;
|
||||
int clientCount () const;
|
||||
const Binding* binding () const;
|
||||
Binding* binding ();
|
||||
void clients(std::vector<ClientId>* clients) const;
|
||||
bool clients(ClientId id, ClientData* data) const;
|
||||
ClientId clientActive() const;
|
||||
int clientCount() const;
|
||||
const Binding* binding() const;
|
||||
Binding* binding();
|
||||
|
||||
private:
|
||||
struct ClientEntry : public ClientData {
|
||||
@ -79,17 +79,17 @@ private:
|
||||
Protocol protocol;
|
||||
};
|
||||
|
||||
void advanceConnecting ();
|
||||
void advanceConnected ();
|
||||
void advanceDisconnecting ();
|
||||
static ClientId registerClientId ();
|
||||
void advanceConnecting();
|
||||
void advanceConnected();
|
||||
void advanceDisconnecting();
|
||||
static ClientId registerClientId();
|
||||
|
||||
typedef std::map<ClientId, ClientEntry*> ClientMap;
|
||||
ClientMap clients_;
|
||||
int clientMax_;
|
||||
ClientId clientActive_;
|
||||
Socket server_;
|
||||
Binding binding_;
|
||||
ClientMap m_clients;
|
||||
int m_clientMax;
|
||||
ClientId m_clientActive;
|
||||
Socket m_server;
|
||||
Binding m_binding;
|
||||
};
|
||||
|
||||
|
||||
|
@ -46,7 +46,7 @@ static struct Winsock {
|
||||
~Winsock() {
|
||||
WSACleanup();
|
||||
}
|
||||
} winsock;
|
||||
} s_winsock;
|
||||
|
||||
#endif
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user