Commit 6c20de48 authored by 吴贤德's avatar 吴贤德

1.0

parents
//
// sio_client.h
//
// Created by Melo Yao on 3/25/15.
//
#ifndef SIO_CLIENT_H
#define SIO_CLIENT_H
#include <string>
#include <functional>
#include "sio_message.h"
#include "sio_socket.h"
namespace sio
{
class client_impl;
class client {
public:
enum close_reason
{
close_reason_normal,
close_reason_drop
};
typedef std::function<void(void)> con_listener;
typedef std::function<void(close_reason const& reason)> close_listener;
typedef std::function<void(unsigned, unsigned)> reconnect_listener;
typedef std::function<void(std::string const& nsp)> socket_listener;
client();
~client();
//set listeners and event bindings.
void set_open_listener(con_listener const& l);
void set_fail_listener(con_listener const& l);
void set_reconnecting_listener(con_listener const& l);
void set_reconnect_listener(reconnect_listener const& l);
void set_close_listener(close_listener const& l);
void set_socket_open_listener(socket_listener const& l);
void set_socket_close_listener(socket_listener const& l);
void clear_con_listeners();
void clear_socket_listeners();
// Client Functions - such as send, etc.
void connect(const std::string& uri);
void connect(const std::string& uri, const std::map<std::string,std::string>& query);
void set_reconnect_attempts(int attempts);
void set_reconnect_delay(unsigned millis);
void set_reconnect_delay_max(unsigned millis);
sio::socket::ptr const& socket(const std::string& nsp = "");
// Closes the connection
void close();
void sync_close();
bool opened() const;
std::string const& get_sessionid() const;
private:
//disable copy constructor and assign operator.
client(client const& cl){}
void operator=(client const& cl){}
client_impl* m_impl;
};
}
#endif // __SIO_CLIENT__H__
//
// sio_message.h
//
// Created by Melo Yao on 3/25/15.
//
#ifndef __SIO_MESSAGE_H__
#define __SIO_MESSAGE_H__
#include <string>
#include <memory>
#include <vector>
#include <map>
#include <cassert>
#include <type_traits>
namespace sio
{
using namespace std;
class message
{
public:
enum flag
{
flag_integer,
flag_double,
flag_string,
flag_binary,
flag_array,
flag_object,
flag_boolean,
flag_null
};
virtual ~message(){};
class list;
flag get_flag() const
{
return _flag;
}
typedef shared_ptr<message> ptr;
virtual bool get_bool() const
{
assert(false);
return false;
}
virtual int64_t get_int() const
{
assert(false);
return 0;
}
virtual double get_double() const
{
assert(false);
return 0;
}
virtual string const& get_string() const
{
assert(false);
static string s_empty_string;
s_empty_string.clear();
return s_empty_string;
}
virtual shared_ptr<const string> const& get_binary() const
{
assert(false);
static shared_ptr<const string> s_empty_binary;
s_empty_binary = nullptr;
return s_empty_binary;
}
virtual const vector<ptr>& get_vector() const
{
assert(false);
static vector<ptr> s_empty_vector;
s_empty_vector.clear();
return s_empty_vector;
}
virtual vector<ptr>& get_vector()
{
assert(false);
static vector<ptr> s_empty_vector;
s_empty_vector.clear();
return s_empty_vector;
}
virtual const map<string,message::ptr>& get_map() const
{
assert(false);
static map<string,message::ptr> s_empty_map;
s_empty_map.clear();
return s_empty_map;
}
virtual map<string,message::ptr>& get_map()
{
assert(false);
static map<string,message::ptr> s_empty_map;
s_empty_map.clear();
return s_empty_map;
}
private:
flag _flag;
protected:
message(flag f):_flag(f){}
};
class null_message : public message
{
protected:
null_message()
:message(flag_null)
{
}
public:
static message::ptr create()
{
return ptr(new null_message());
}
};
class bool_message : public message
{
bool _v;
protected:
bool_message(bool v)
:message(flag_boolean),_v(v)
{
}
public:
static message::ptr create(bool v)
{
return ptr(new bool_message(v));
}
bool get_bool() const
{
return _v;
}
};
class int_message : public message
{
int64_t _v;
protected:
int_message(int64_t v)
:message(flag_integer),_v(v)
{
}
public:
static message::ptr create(int64_t v)
{
return ptr(new int_message(v));
}
int64_t get_int() const
{
return _v;
}
double get_double() const//add double accessor for integer.
{
return static_cast<double>(_v);
}
};
class double_message : public message
{
double _v;
double_message(double v)
:message(flag_double),_v(v)
{
}
public:
static message::ptr create(double v)
{
return ptr(new double_message(v));
}
double get_double() const
{
return _v;
}
};
class string_message : public message
{
string _v;
string_message(string const& v)
:message(flag_string),_v(v)
{
}
string_message(string&& v)
:message(flag_string),_v(move(v))
{
}
public:
static message::ptr create(string const& v)
{
return ptr(new string_message(v));
}
static message::ptr create(string&& v)
{
return ptr(new string_message(move(v)));
}
string const& get_string() const
{
return _v;
}
};
class binary_message : public message
{
shared_ptr<const string> _v;
binary_message(shared_ptr<const string> const& v)
:message(flag_binary),_v(v)
{
}
public:
static message::ptr create(shared_ptr<const string> const& v)
{
return ptr(new binary_message(v));
}
shared_ptr<const string> const& get_binary() const
{
return _v;
}
};
class array_message : public message
{
vector<message::ptr> _v;
array_message():message(flag_array)
{
}
public:
static message::ptr create()
{
return ptr(new array_message());
}
vector<ptr>& get_vector()
{
return _v;
}
const vector<ptr>& get_vector() const
{
return _v;
}
};
class object_message : public message
{
map<string,message::ptr> _v;
object_message() : message(flag_object)
{
}
public:
static message::ptr create()
{
return ptr(new object_message());
}
map<string,message::ptr>& get_map()
{
return _v;
}
const map<string,message::ptr>& get_map() const
{
return _v;
}
};
class message::list
{
public:
list()
{
}
list(nullptr_t)
{
}
list(message::list&& rhs):
m_vector(std::move(rhs.m_vector))
{
}
list & operator= (const message::list && rhs)
{
m_vector = std::move(rhs.m_vector);
return *this;
}
template <typename T>
list(T&& content,
typename enable_if<is_same<vector<message::ptr>,typename remove_reference<T>::type>::value>::type* = 0):
m_vector(std::forward<T>(content))
{
}
list(message::list const& rhs):
m_vector(rhs.m_vector)
{
}
list(message::ptr const& message)
{
if(message)
m_vector.push_back(message);
}
list(const string& text)
{
m_vector.push_back(string_message::create(text));
}
list(string&& text)
{
m_vector.push_back(string_message::create(move(text)));
}
list(shared_ptr<string> const& binary)
{
if(binary)
m_vector.push_back(binary_message::create(binary));
}
list(shared_ptr<const string> const& binary)
{
if(binary)
m_vector.push_back(binary_message::create(binary));
}
void push(message::ptr const& message)
{
if(message)
m_vector.push_back(message);
}
void insert(size_t pos,message::ptr const& message)
{
m_vector.insert(m_vector.begin()+pos, message);
}
size_t size() const
{
return m_vector.size();
}
const message::ptr& at(size_t i) const
{
return m_vector[i];
}
const message::ptr& operator[] (size_t i) const
{
return m_vector[i];
}
message::ptr to_array_message(string const& event_name) const
{
message::ptr arr = array_message::create();
arr->get_vector().push_back(string_message::create(event_name));
arr->get_vector().insert(arr->get_vector().end(),m_vector.begin(),m_vector.end());
return arr;
}
message::ptr to_array_message() const
{
message::ptr arr = array_message::create();
arr->get_vector().insert(arr->get_vector().end(),m_vector.begin(),m_vector.end());
return arr;
}
private:
vector<message::ptr> m_vector;
};
}
#endif
#ifndef SIO_SOCKET_H
#define SIO_SOCKET_H
#include "sio_message.h"
#include <functional>
namespace sio
{
class event_adapter;
class event
{
public:
const std::string& get_nsp() const;
const std::string& get_name() const;
const message::ptr& get_message() const;
const message::list& get_messages() const;
bool need_ack() const;
void put_ack_message(message::list const& ack_message);
message::list const& get_ack_message() const;
protected:
event(std::string const& nsp,std::string const& name,message::list const& messages,bool need_ack);
event(std::string const& nsp,std::string const& name,message::list&& messages,bool need_ack);
message::list& get_ack_message_impl();
private:
const std::string m_nsp;
const std::string m_name;
const message::list m_messages;
const bool m_need_ack;
message::list m_ack_message;
friend class event_adapter;
};
class client_impl;
class packet;
//The name 'socket' is taken from concept of official socket.io.
class socket
{
public:
typedef std::function<void(const std::string& name,message::ptr const& message,bool need_ack, message::list& ack_message)> event_listener_aux;
typedef std::function<void(event& event)> event_listener;
typedef std::function<void(message::ptr const& message)> error_listener;
typedef std::shared_ptr<socket> ptr;
~socket();
void on(std::string const& event_name,event_listener const& func);
void on(std::string const& event_name,event_listener_aux const& func);
void off(std::string const& event_name);
void off_all();
void close();
void on_error(error_listener const& l);
void off_error();
void emit(std::string const& name, message::list const& msglist = nullptr, std::function<void (message::list const&)> const& ack = nullptr);
std::string const& get_namespace() const;
protected:
socket(client_impl*,std::string const&);
void on_connected();
void on_close();
void on_open();
void on_disconnect();
void on_message_packet(packet const& p);
friend class client_impl;
private:
//disable copy constructor and assign operator.
socket(socket const& sock){}
void operator=(socket const& sock){}
class impl;
impl *m_impl;
};
}
#endif // SIO_SOCKET_H
Pod::Spec.new do |s|
s.name = "sioclient"
s.version = "1.0.0"
s.summary = "sioclient ios librarys. support : i386,x86_64,armv7,arm64"
s.homepage = "http://code.dev.soooner.com:18000/wuxiande/sioclient"
s.license = "MIT"
s.requires_arc = true
s.author = { "wuxiande" => "xd.wu@msn.com" }
s.source = { :git => "http://code.dev.soooner.com:18000/wuxiande/sioclient.git", :tag => '1.0.0'}
s.ios.deployment_target = "8.0"
s.header_mappings_dir = 'include'
s.source_files = 'include/*/*.h'
s.public_header_files = 'include/*/*.h'
s.vendored_libraries = 'lib/*.a'
s.libraries = 'sioclient', 'z'
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment