提交 b02e1720 编写于 作者: Y youngowlf

Support optional timers (deadline_timer, steady_timer and system_timer).

Split ext/st_asio_wrapper_net.h into ext/st_asio_wrapper_client.h, ext/st_asio_wrapper_server.h and ext/st_asio_wrapper_udp.h.
Add virtual function st_server_base::on_accept_error, it controls whether to continue the acception or not when error occurred.
上级 2920a73a
......@@ -39,7 +39,7 @@ public:
static void debug_out(const char* fmt, ...) {all_out_helper2("debug");}
};
#include "../include/ext/st_asio_wrapper_net.h"
#include "../include/ext/st_asio_wrapper_client.h"
using namespace st_asio_wrapper::ext;
#define QUIT_COMMAND "quit"
......
......@@ -3,11 +3,14 @@
//configuration
#define ST_ASIO_SERVER_PORT 9527
#define ST_ASIO_ASYNC_ACCEPT_NUM 5
#define ST_ASIO_REUSE_OBJECT //use objects pool
#define ST_ASIO_FREE_OBJECT_INTERVAL 60
//#define ST_ASIO_FORCE_TO_USE_MSG_RECV_BUFFER //force to use the msg recv buffer
#define ST_ASIO_ENHANCED_STABILITY
//#define ST_ASIO_FULL_STATISTIC //full statistic will slightly impact efficiency.
//#define ST_ASIO_USE_STEADY_TIMER
//#define ST_ASIO_USE_SYSTEM_TIMER
//use the following macro to control the type of packer and unpacker
#define PACKER_UNPACKER_TYPE 0
......@@ -27,7 +30,7 @@
#endif
//configuration
#include "../include/ext/st_asio_wrapper_net.h"
#include "../include/ext/st_asio_wrapper_server.h"
using namespace st_asio_wrapper;
using namespace st_asio_wrapper::ext;
......@@ -220,6 +223,7 @@ int main(int argc, const char* argv[])
//restore configuration
#undef ST_ASIO_SERVER_PORT
#undef ST_ASIO_ASYNC_ACCEPT_NUM
#undef ST_ASIO_REUSE_OBJECT
#undef ST_ASIO_FREE_OBJECT_INTERVAL
#undef ST_ASIO_FORCE_TO_USE_MSG_RECV_BUFFER
......@@ -227,4 +231,6 @@ int main(int argc, const char* argv[])
#undef ST_ASIO_FULL_STATISTIC
#undef ST_ASIO_DEFAULT_PACKER
#undef ST_ASIO_DEFAULT_UNPACKER
#undef ST_ASIO_USE_STEADY_TIMER
#undef ST_ASIO_USE_SYSTEM_TIMER
//restore configuration
......@@ -39,7 +39,7 @@ public:
static void debug_out(const char* fmt, ...) {all_out_helper2("debug");}
};
#include "../include/ext/st_asio_wrapper_net.h"
#include "../include/ext/st_asio_wrapper_client.h"
using namespace st_asio_wrapper::ext;
#define QUIT_COMMAND "quit"
......
......@@ -3,11 +3,14 @@
//configuration
#define ST_ASIO_SERVER_PORT 9528
#define ST_ASIO_ASYNC_ACCEPT_NUM 5
#define ST_ASIO_REUSE_OBJECT //use objects pool
#define ST_ASIO_FREE_OBJECT_INTERVAL 60
//#define ST_ASIO_FORCE_TO_USE_MSG_RECV_BUFFER //force to use the msg recv buffer
#define ST_ASIO_ENHANCED_STABILITY
#define ST_ASIO_FULL_STATISTIC //full statistic will slightly impact efficiency.
//#define ST_ASIO_USE_STEADY_TIMER
//#define ST_ASIO_USE_SYSTEM_TIMER
//use the following macro to control the type of packer and unpacker
#define PACKER_UNPACKER_TYPE 0
......@@ -27,7 +30,7 @@
#endif
//configuration
#include "../include/ext/st_asio_wrapper_net.h"
#include "../include/ext/st_asio_wrapper_server.h"
using namespace st_asio_wrapper;
using namespace st_asio_wrapper::ext;
......@@ -224,6 +227,7 @@ int main(int argc, const char* argv[])
//restore configuration
#undef ST_ASIO_SERVER_PORT
#undef ST_ASIO_ASYNC_ACCEPT_NUM
#undef ST_ASIO_REUSE_OBJECT
#undef ST_ASIO_FREE_OBJECT_INTERVAL
#undef ST_ASIO_FORCE_TO_USE_MSG_RECV_BUFFER
......@@ -231,4 +235,6 @@ int main(int argc, const char* argv[])
#undef ST_ASIO_FULL_STATISTIC
#undef ST_ASIO_DEFAULT_PACKER
#undef ST_ASIO_DEFAULT_UNPACKER
#undef ST_ASIO_USE_STEADY_TIMER
#undef ST_ASIO_USE_SYSTEM_TIMER
//restore configuration
......@@ -26,7 +26,7 @@ release debug : ${target}
${target} : ${objects}
${CXX} -o $@ $^ ${lflag}
${objects} : ${dir}/%.o : %.cpp
${CXX} ${cflag} -E -MMD -MT '$@' -MF ${subst .cpp,.d,${dir}/$<} $< 1>/dev/null
${CXX} ${cflag} -E -MMD -w -MT '$@' -MF ${subst .cpp,.d,${dir}/$<} $< 1>/dev/null
${CXX} ${cflag} -c $< -o $@
.PHONY : clean
......
......@@ -7,7 +7,7 @@
#include <boost/lambda/lambda.hpp>
#include "../file_server/packer_unpacker.h"
#include "../include/ext/st_asio_wrapper_net.h"
#include "../include/ext/st_asio_wrapper_client.h"
using namespace st_asio_wrapper;
using namespace st_asio_wrapper::ext;
......
......@@ -3,6 +3,7 @@
//configuration
#define ST_ASIO_SERVER_PORT 5051
#define ST_ASIO_ASYNC_ACCEPT_NUM 5
#define ST_ASIO_CLEAR_OBJECT_INTERVAL 60
#define ST_ASIO_ENHANCED_STABILITY
#define ST_ASIO_WANT_MSG_SEND_NOTIFY
......@@ -56,6 +57,7 @@ int main(int argc, const char* argv[])
//restore configuration
#undef ST_ASIO_SERVER_PORT
#undef ST_ASIO_ASYNC_ACCEPT_NUM
#undef ST_ASIO_CLEAR_OBJECT_INTERVAL
#undef ST_ASIO_ENHANCED_STABILITY
#undef ST_ASIO_WANT_MSG_SEND_NOTIFY
......
......@@ -3,7 +3,7 @@
#define FILE_SOCKET_H_
#include "packer_unpacker.h"
#include "../include/ext/st_asio_wrapper_net.h"
#include "../include/ext/st_asio_wrapper_server.h"
using namespace st_asio_wrapper::ext;
class file_socket : public base_socket, public st_server_socket
......
/*
* st_asio_wrapper_net.h
* st_asio_wrapper_client.h
*
* Created on: 2016-7-30
* Author: youngwolf
......@@ -7,19 +7,17 @@
* QQ: 676218192
* Community on QQ: 198941541
*
* extensional, replaceable and indispensable components.
* client related conveniences.
*/
#ifndef ST_ASIO_WRAPPER_NET_H_
#define ST_ASIO_WRAPPER_NET_H_
#ifndef ST_ASIO_WRAPPER_EXT_CLIENT_H_
#define ST_ASIO_WRAPPER_EXT_CLIENT_H_
#include "st_asio_wrapper_packer.h"
#include "st_asio_wrapper_unpacker.h"
#include "../st_asio_wrapper_client.h"
#include "../st_asio_wrapper_tcp_client.h"
#include "../st_asio_wrapper_udp_client.h"
#include "../st_asio_wrapper_server.h"
#ifndef ST_ASIO_DEFAULT_PACKER
#define ST_ASIO_DEFAULT_PACKER packer
......@@ -29,23 +27,12 @@
#define ST_ASIO_DEFAULT_UNPACKER unpacker
#endif
#ifndef ST_ASIO_DEFAULT_UDP_UNPACKER
#define ST_ASIO_DEFAULT_UDP_UNPACKER udp_unpacker
#endif
namespace st_asio_wrapper { namespace ext {
typedef st_server_socket_base<ST_ASIO_DEFAULT_PACKER, ST_ASIO_DEFAULT_UNPACKER> st_server_socket;
typedef st_server_base<st_server_socket> st_server;
typedef st_connector_base<ST_ASIO_DEFAULT_PACKER, ST_ASIO_DEFAULT_UNPACKER> st_connector;
typedef st_sclient<st_connector> st_tcp_sclient;
typedef st_tcp_client_base<st_connector> st_tcp_client;
typedef st_udp_socket_base<ST_ASIO_DEFAULT_PACKER, ST_ASIO_DEFAULT_UDP_UNPACKER> st_udp_socket;
typedef st_sclient<st_udp_socket> st_udp_sclient;
typedef st_udp_client_base<st_udp_socket> st_udp_client;
}} //namespace
#endif /* ST_ASIO_WRAPPER_NET_H_ */
#endif /* ST_ASIO_WRAPPER_EXT_CLIENT_H_ */
......@@ -7,7 +7,7 @@
* QQ: 676218192
* Community on QQ: 198941541
*
* extensional, replaceable and indispensable components.
* extensional common.
*/
#ifndef ST_ASIO_WRAPPER_EXT_H_
......
......@@ -7,11 +7,11 @@
* QQ: 676218192
* Community on QQ: 198941541
*
* packer base class
* packers
*/
#ifndef ST_ASIO_WRAPPER_PACKER_H_
#define ST_ASIO_WRAPPER_PACKER_H_
#ifndef ST_ASIO_WRAPPER_EXT_PACKER_H_
#define ST_ASIO_WRAPPER_EXT_PACKER_H_
#include "st_asio_wrapper_ext.h"
......@@ -165,4 +165,4 @@ private:
}} //namespace
#endif /* ST_ASIO_WRAPPER_PACKER_H_ */
#endif /* ST_ASIO_WRAPPER_EXT_PACKER_H_ */
/*
* st_asio_wrapper_server.h
*
* Created on: 2016-9-7
* Author: youngwolf
* email: mail2tao@163.com
* QQ: 676218192
* Community on QQ: 198941541
*
* server related conveniences.
*/
#ifndef ST_ASIO_WRAPPER_EXT_SERVER_H_
#define ST_ASIO_WRAPPER_EXT_SERVER_H_
#include "st_asio_wrapper_packer.h"
#include "st_asio_wrapper_unpacker.h"
#include "../st_asio_wrapper_server.h"
#ifndef ST_ASIO_DEFAULT_PACKER
#define ST_ASIO_DEFAULT_PACKER packer
#endif
#ifndef ST_ASIO_DEFAULT_UNPACKER
#define ST_ASIO_DEFAULT_UNPACKER unpacker
#endif
namespace st_asio_wrapper { namespace ext {
typedef st_server_socket_base<ST_ASIO_DEFAULT_PACKER, ST_ASIO_DEFAULT_UNPACKER> st_server_socket;
typedef st_server_base<st_server_socket> st_server;
}} //namespace
#endif /* ST_ASIO_WRAPPER_EXT_SERVER_H_ */
......@@ -7,7 +7,7 @@
* QQ: 676218192
* Community on QQ: 198941541
*
* extensional, replaceable and indispensable components.
* ssl related conveniences.
*/
#ifndef ST_ASIO_WRAPPER_EXT_SSL_H_
......
/*
* st_asio_wrapper_udp.h
*
* Created on: 2016-9-7
* Author: youngwolf
* email: mail2tao@163.com
* QQ: 676218192
* Community on QQ: 198941541
*
* udp related conveniences.
*/
#ifndef ST_ASIO_WRAPPER_EXT_UDP_H_
#define ST_ASIO_WRAPPER_EXT_UDP_H_
#include "st_asio_wrapper_packer.h"
#include "st_asio_wrapper_unpacker.h"
#include "../st_asio_wrapper_client.h"
#include "../st_asio_wrapper_udp_client.h"
#ifndef ST_ASIO_DEFAULT_PACKER
#define ST_ASIO_DEFAULT_PACKER packer
#endif
#ifndef ST_ASIO_DEFAULT_UDP_UNPACKER
#define ST_ASIO_DEFAULT_UDP_UNPACKER udp_unpacker
#endif
namespace st_asio_wrapper { namespace ext {
typedef st_udp_socket_base<ST_ASIO_DEFAULT_PACKER, ST_ASIO_DEFAULT_UDP_UNPACKER> st_udp_socket;
typedef st_sclient<st_udp_socket> st_udp_sclient;
typedef st_udp_client_base<st_udp_socket> st_udp_client;
}} //namespace
#endif /* ST_ASIO_WRAPPER_EXT_UDP_H_ */
......@@ -7,11 +7,11 @@
* QQ: 676218192
* Community on QQ: 198941541
*
* unpacker base class
* unpackers
*/
#ifndef ST_ASIO_WRAPPER_UNPACKER_H_
#define ST_ASIO_WRAPPER_UNPACKER_H_
#ifndef ST_ASIO_WRAPPER_EXT_UNPACKER_H_
#define ST_ASIO_WRAPPER_EXT_UNPACKER_H_
#include <boost/array.hpp>
......@@ -444,4 +444,4 @@ protected:
}} //namespace
#endif /* ST_ASIO_WRAPPER_UNPACKER_H_ */
#endif /* ST_ASIO_WRAPPER_EXT_UNPACKER_H_ */
......@@ -119,6 +119,21 @@ protected:
virtual void uninit() {ST_THIS stop(); stop_listen(); close_all_client();}
virtual bool on_accept(typename Pool::object_ctype& client_ptr) {return true;}
//if you want to ignore this error and continue to accept new connections immediately, return true in this virtual function;
//if you want to ignore this error and continue to accept new connections after a specific delay, start a timer immediately and return false (don't call stop_listen()),
// when the timer ends up, call start_next_accept() in the callback function.
//otherwise, don't rewrite this virtual function or call st_server_base::on_accept_error() directly after your code.
virtual bool on_accept_error(const boost::system::error_code& ec, typename Pool::object_ctype& client_ptr)
{
if (boost::asio::error::operation_aborted != ec)
{
unified_out::error_out("failed to accept new connection because of %s, will stop listening.", ec.message().data());
stop_listen();
}
return false;
}
virtual void start_next_accept()
{
typename Pool::object_type client_ptr = ST_THIS create_object(boost::ref(*this));
......@@ -148,8 +163,8 @@ protected:
start_next_accept();
}
else
stop_listen();
else if (on_accept_error(ec, client_ptr))
start_next_accept();
}
protected:
......
......@@ -186,8 +186,8 @@ protected:
#ifdef ST_ASIO_ENHANCED_STABILITY
virtual bool on_exception(const std::exception& e)
{
unified_out::info_out("service pump exception: %s.", e.what());
return true; //continue this boost::asio::io_service::run, if needed, rewrite this to decide whether to continue or not
unified_out::error_out("service pump exception: %s.", e.what());
return true; //continue this io_service::run, if needed, rewrite this to decide whether to continue or not
}
size_t run(boost::system::error_code& ec)
......
......@@ -23,10 +23,6 @@
#error graceful close duration must be bigger than zero.
#endif
#ifndef ST_ASIO_DEFAULT_UNPACKER
#define ST_ASIO_DEFAULT_UNPACKER unpacker
#endif
namespace st_asio_wrapper
{
......@@ -131,7 +127,6 @@ protected:
size_t size = 0;
BOOST_AUTO(end_time, super::statistic::local_time());
std::vector<boost::asio::const_buffer> bufs;
last_send_msg.clear();
for (BOOST_AUTO(iter, ST_THIS send_msg_buffer.begin()); last_send_msg.empty();)
{
size += iter->size();
......@@ -233,20 +228,23 @@ private:
else
ST_THIS on_send_error(ec);
#ifdef ST_ASIO_WANT_ALL_MSG_SEND_NOTIFY
typename super::in_msg msg;
msg.swap(last_send_msg.back());
#endif
last_send_msg.clear();
boost::unique_lock<boost::shared_mutex> lock(ST_THIS send_msg_buffer_mutex);
ST_THIS sending = false;
//send msg sequentially, that means second send only after first send success
if (!ec)
#ifdef ST_ASIO_WANT_ALL_MSG_SEND_NOTIFY
if (!do_send_msg())
ST_THIS on_all_msg_send(last_send_msg.back());
if (!ec && !do_send_msg())
ST_THIS on_all_msg_send(msg);
#else
if (!ec)
do_send_msg();
#endif
if (!ST_THIS sending)
last_send_msg.clear();
}
protected:
......
......@@ -14,6 +14,11 @@
#define ST_ASIO_WRAPPER_TIMER_H_
#include <boost/container/set.hpp>
#ifdef ST_ASIO_USE_STEADY_TIMER
#include <boost/asio/steady_timer.hpp>
#elif defined(ST_ASIO_USE_SYSTEM_TIMER)
#include <boost/asio/system_timer.hpp>
#endif
#include "st_asio_wrapper_object.h"
......@@ -32,6 +37,17 @@ namespace st_asio_wrapper
class st_timer : public st_object
{
protected:
#ifdef ST_ASIO_USE_STEADY_TIMER
typedef boost::chrono::milliseconds milliseconds;
typedef boost::asio::steady_timer timer_type;
#elif defined(ST_ASIO_USE_SYSTEM_TIMER)
typedef boost::chrono::milliseconds milliseconds;
typedef boost::asio::system_timer timer_type;
#else
typedef boost::posix_time::milliseconds milliseconds;
typedef boost::asio::deadline_timer timer_type;
#endif
struct timer_info
{
enum timer_status {TIMER_OK, TIMER_CANCELED};
......@@ -40,7 +56,7 @@ protected:
timer_status status;
size_t milliseconds;
boost::function<bool (unsigned char)> call_back;
boost::shared_ptr<boost::asio::deadline_timer> timer;
boost::shared_ptr<timer_type> timer;
bool operator <(const timer_info& other) const {return id < other.id;}
};
......@@ -69,7 +85,7 @@ public:
iter = timer_can.insert(ti).first;
timer_can_mutex.unlock();
iter->timer = boost::make_shared<boost::asio::deadline_timer>(boost::ref(io_service_));
iter->timer = boost::make_shared<timer_type>(boost::ref(io_service_));
}
else
timer_can_mutex.unlock_upgrade();
......@@ -139,7 +155,7 @@ protected:
void start_timer(object_ctype& ti)
{
ti.timer->expires_from_now(boost::posix_time::milliseconds(ti.milliseconds));
ti.timer->expires_from_now(milliseconds(ti.milliseconds));
ti.timer->async_wait(make_handler_error(boost::bind(&st_timer::timer_handler, this, boost::asio::placeholders::error, boost::ref(ti))));
}
......
......@@ -224,6 +224,12 @@ private:
else
ST_THIS on_send_error(ec);
#ifdef ST_ASIO_WANT_ALL_MSG_SEND_NOTIFY
typename super::in_msg msg;
msg.swap(last_send_msg);
#endif
last_send_msg.clear();
boost::unique_lock<boost::shared_mutex> lock(ST_THIS send_msg_buffer_mutex);
ST_THIS sending = false;
......@@ -232,13 +238,10 @@ private:
//for UDP in st_asio_wrapper, sending error will not stop the following sending.
#ifdef ST_ASIO_WANT_ALL_MSG_SEND_NOTIFY
if (!do_send_msg())
ST_THIS on_all_msg_send(last_send_msg);
ST_THIS on_all_msg_send(msg);
#else
do_send_msg();
#endif
if (!ST_THIS sending)
last_send_msg.clear();
}
protected:
......
......@@ -12,7 +12,7 @@
#define ST_ASIO_DEFAULT_UNPACKER stream_unpacker //non-protocol
//configuration
#include "../include/ext/st_asio_wrapper_net.h"
#include "../include/ext/st_asio_wrapper_client.h"
using namespace st_asio_wrapper;
using namespace st_asio_wrapper::ext;
......
......@@ -3,13 +3,14 @@
//configuration
#define ST_ASIO_SERVER_PORT 9527
#define ST_ASIO_ASYNC_ACCEPT_NUM 5
#define ST_ASIO_REUSE_OBJECT //use objects pool
//#define ST_ASIO_FORCE_TO_USE_MSG_RECV_BUFFER
#define ST_ASIO_MSG_BUFFER_SIZE 65536
#define ST_ASIO_DEFAULT_UNPACKER stream_unpacker //non-protocol
//configuration
#include "../include/ext/st_asio_wrapper_net.h"
#include "../include/ext/st_asio_wrapper_server.h"
using namespace st_asio_wrapper;
using namespace st_asio_wrapper::ext;
......@@ -91,6 +92,7 @@ int main(int argc, const char* argv[])
//restore configuration
#undef ST_ASIO_SERVER_PORT
#undef ST_ASIO_ASYNC_ACCEPT_NUM
#undef ST_ASIO_REUSE_OBJECT
#undef ST_ASIO_FREE_OBJECT_INTERVAL
#undef ST_ASIO_DEFAULT_PACKER
......
......@@ -3,6 +3,7 @@
//configuration
#define ST_ASIO_SERVER_PORT 9527
#define ST_ASIO_ASYNC_ACCEPT_NUM 5
//#define ST_ASIO_FORCE_TO_USE_MSG_RECV_BUFFER //force to use the msg recv buffer
#define ST_ASIO_ENHANCED_STABILITY
//#define ST_ASIO_DEFAULT_PACKER replaceable_packer
......@@ -99,6 +100,7 @@ int main(int argc, const char* argv[])
//restore configuration
#undef ST_ASIO_SERVER_PORT
#undef ST_ASIO_ASYNC_ACCEPT_NUM
#undef ST_ASIO_FORCE_TO_USE_MSG_RECV_BUFFER
#undef ST_ASIO_ENHANCED_STABILITY
#undef ST_ASIO_DEFAULT_PACKER
......
......@@ -31,7 +31,7 @@
#define ST_ASIO_DEFAULT_UNPACKER prefix_suffix_unpacker
#endif
#include "../include/ext/st_asio_wrapper_net.h"
#include "../include/ext/st_asio_wrapper_client.h"
using namespace st_asio_wrapper;
using namespace st_asio_wrapper::ext;
......
......@@ -6,7 +6,7 @@
//#define ST_ASIO_DEFAULT_UDP_UNPACKER replaceable_udp_unpacker
//configuration
#include "../include/ext/st_asio_wrapper_net.h"
#include "../include/ext/st_asio_wrapper_udp.h"
using namespace st_asio_wrapper;
using namespace st_asio_wrapper::ext;
......
......@@ -26,7 +26,7 @@ release debug : ${target}
${target} : ${objects}
${CXX} -o $@ $^ ${lflag}
${objects} : ${dir}/%.o : %.cpp
${CXX} ${cflag} -E -MMD -MT '$@' -MF ${subst .cpp,.d,${dir}/$<} $< 1>/dev/null
${CXX} ${cflag} -E -MMD -w -MT '$@' -MF ${subst .cpp,.d,${dir}/$<} $< 1>/dev/null
${CXX} ${cflag} -c $< -o $@
.PHONY : clean
......
......@@ -5,7 +5,7 @@
#include <boost/timer/timer.hpp>
#include "../file_server/packer_unpacker.h"
#include "../include/ext/st_asio_wrapper_net.h"
#include "../include/ext/st_asio_wrapper_client.h"
using namespace st_asio_wrapper;
using namespace st_asio_wrapper::ext;
......
......@@ -3,6 +3,7 @@
//configuration
#define ST_ASIO_SERVER_PORT 5050
#define ST_ASIO_ASYNC_ACCEPT_NUM 5
#define ST_ASIO_CLEAR_OBJECT_INTERVAL 60
#define ST_ASIO_ENHANCED_STABILITY
#define ST_ASIO_WANT_MSG_SEND_NOTIFY
......@@ -56,6 +57,7 @@ int main(int argc, const char* argv[])
//restore configuration
#undef ST_ASIO_SERVER_PORT
#undef ST_ASIO_ASYNC_ACCEPT_NUM
#undef ST_ASIO_CLEAR_OBJECT_INTERVAL
#undef ST_ASIO_ENHANCED_STABILITY
#undef ST_ASIO_WANT_MSG_SEND_NOTIFY
......
......@@ -3,7 +3,7 @@
#define FILE_SOCKET_H_
#include "packer_unpacker.h"
#include "../include/ext/st_asio_wrapper_net.h"
#include "../include/ext/st_asio_wrapper_server.h"
using namespace st_asio_wrapper::ext;
class file_socket : public base_socket, public st_server_socket
......
/*
* st_asio_wrapper_net.h
* st_asio_wrapper_client.h
*
* Created on: 2016-7-30
* Author: youngwolf
......@@ -7,19 +7,17 @@
* QQ: 676218192
* Community on QQ: 198941541
*
* extensional, replaceable and indispensable components.
* client related conveniences.
*/
#ifndef ST_ASIO_WRAPPER_NET_H_
#define ST_ASIO_WRAPPER_NET_H_
#ifndef ST_ASIO_WRAPPER_EXT_CLIENT_H_
#define ST_ASIO_WRAPPER_EXT_CLIENT_H_
#include "st_asio_wrapper_packer.h"
#include "st_asio_wrapper_unpacker.h"
#include "../st_asio_wrapper_client.h"
#include "../st_asio_wrapper_tcp_client.h"
#include "../st_asio_wrapper_udp_client.h"
#include "../st_asio_wrapper_server.h"
#ifndef ST_ASIO_DEFAULT_PACKER
#define ST_ASIO_DEFAULT_PACKER packer
......@@ -29,23 +27,12 @@
#define ST_ASIO_DEFAULT_UNPACKER unpacker
#endif
#ifndef ST_ASIO_DEFAULT_UDP_UNPACKER
#define ST_ASIO_DEFAULT_UDP_UNPACKER udp_unpacker
#endif
namespace st_asio_wrapper { namespace ext {
typedef st_server_socket_base<ST_ASIO_DEFAULT_PACKER, ST_ASIO_DEFAULT_UNPACKER> st_server_socket;
typedef st_server_base<st_server_socket> st_server;
typedef st_connector_base<ST_ASIO_DEFAULT_PACKER, ST_ASIO_DEFAULT_UNPACKER> st_connector;
typedef st_sclient<st_connector> st_tcp_sclient;
typedef st_tcp_client_base<st_connector> st_tcp_client;
typedef st_udp_socket_base<ST_ASIO_DEFAULT_PACKER, ST_ASIO_DEFAULT_UDP_UNPACKER> st_udp_socket;
typedef st_sclient<st_udp_socket> st_udp_sclient;
typedef st_udp_client_base<st_udp_socket> st_udp_client;
}} //namespace
#endif /* ST_ASIO_WRAPPER_NET_H_ */
#endif /* ST_ASIO_WRAPPER_EXT_CLIENT_H_ */
......@@ -7,7 +7,7 @@
* QQ: 676218192
* Community on QQ: 198941541
*
* extensional, replaceable and indispensable components.
* extensional common.
*/
#ifndef ST_ASIO_WRAPPER_EXT_H_
......
......@@ -7,11 +7,11 @@
* QQ: 676218192
* Community on QQ: 198941541
*
* packer base class
* packers
*/
#ifndef ST_ASIO_WRAPPER_PACKER_H_
#define ST_ASIO_WRAPPER_PACKER_H_
#ifndef ST_ASIO_WRAPPER_EXT_PACKER_H_
#define ST_ASIO_WRAPPER_EXT_PACKER_H_
#include "st_asio_wrapper_ext.h"
......@@ -158,4 +158,4 @@ private:
}} //namespace
#endif /* ST_ASIO_WRAPPER_PACKER_H_ */
#endif /* ST_ASIO_WRAPPER_EXT_PACKER_H_ */
/*
* st_asio_wrapper_server.h
*
* Created on: 2016-9-7
* Author: youngwolf
* email: mail2tao@163.com
* QQ: 676218192
* Community on QQ: 198941541
*
* server related conveniences.
*/
#ifndef ST_ASIO_WRAPPER_EXT_SERVER_H_
#define ST_ASIO_WRAPPER_EXT_SERVER_H_
#include "st_asio_wrapper_packer.h"
#include "st_asio_wrapper_unpacker.h"
#include "../st_asio_wrapper_server.h"
#ifndef ST_ASIO_DEFAULT_PACKER
#define ST_ASIO_DEFAULT_PACKER packer
#endif
#ifndef ST_ASIO_DEFAULT_UNPACKER
#define ST_ASIO_DEFAULT_UNPACKER unpacker
#endif
namespace st_asio_wrapper { namespace ext {
typedef st_server_socket_base<ST_ASIO_DEFAULT_PACKER, ST_ASIO_DEFAULT_UNPACKER> st_server_socket;
typedef st_server_base<st_server_socket> st_server;
}} //namespace
#endif /* ST_ASIO_WRAPPER_EXT_SERVER_H_ */
......@@ -7,7 +7,7 @@
* QQ: 676218192
* Community on QQ: 198941541
*
* extensional, replaceable and indispensable components.
* ssl related conveniences.
*/
#ifndef ST_ASIO_WRAPPER_EXT_SSL_H_
......
/*
* st_asio_wrapper_udp.h
*
* Created on: 2016-9-7
* Author: youngwolf
* email: mail2tao@163.com
* QQ: 676218192
* Community on QQ: 198941541
*
* udp related conveniences.
*/
#ifndef ST_ASIO_WRAPPER_EXT_UDP_H_
#define ST_ASIO_WRAPPER_EXT_UDP_H_
#include "st_asio_wrapper_packer.h"
#include "st_asio_wrapper_unpacker.h"
#include "../st_asio_wrapper_client.h"
#include "../st_asio_wrapper_udp_client.h"
#ifndef ST_ASIO_DEFAULT_PACKER
#define ST_ASIO_DEFAULT_PACKER packer
#endif
#ifndef ST_ASIO_DEFAULT_UDP_UNPACKER
#define ST_ASIO_DEFAULT_UDP_UNPACKER udp_unpacker
#endif
namespace st_asio_wrapper { namespace ext {
typedef st_udp_socket_base<ST_ASIO_DEFAULT_PACKER, ST_ASIO_DEFAULT_UDP_UNPACKER> st_udp_socket;
typedef st_sclient<st_udp_socket> st_udp_sclient;
typedef st_udp_client_base<st_udp_socket> st_udp_client;
}} //namespace
#endif /* ST_ASIO_WRAPPER_EXT_UDP_H_ */
......@@ -7,11 +7,11 @@
* QQ: 676218192
* Community on QQ: 198941541
*
* unpacker base class
* unpackers
*/
#ifndef ST_ASIO_WRAPPER_UNPACKER_H_
#define ST_ASIO_WRAPPER_UNPACKER_H_
#ifndef ST_ASIO_WRAPPER_EXT_UNPACKER_H_
#define ST_ASIO_WRAPPER_EXT_UNPACKER_H_
#include <boost/array.hpp>
......@@ -439,4 +439,4 @@ protected:
}} //namespace
#endif /* ST_ASIO_WRAPPER_UNPACKER_H_ */
#endif /* ST_ASIO_WRAPPER_EXT_UNPACKER_H_ */
......@@ -24,7 +24,7 @@
* 2016.7.17 version 1.0.1
* Support boost-1.49, it's the minimum version for st_asio_wrapper because of boost::container.
*
* 2016.8.7 version 1.1.0
* 2016.8.7 version 1.1.0
* Add stream_unpacker to receive native data.
* asio_server and test_client are now able to start with configurable thread number.
* Add efficiency statistic for performance tuning, and it can be shut down.
......@@ -42,14 +42,19 @@
* Added a new replaceable buffer shared_buffer.
* Fix bug: if receive buffer overflow and on_msg() returns false (which means using receive buffer),
* st_socket will call on_msg() again and again (asynchronously) for the same msg until receive buffer becomes available.
*
* 2016.9.13 version 1.1.3
* Support optional timers (deadline_timer, steady_timer and system_timer).
* Split ext/st_asio_wrapper_net.h into ext/st_asio_wrapper_client.h, ext/st_asio_wrapper_server.h and ext/st_asio_wrapper_udp.h.
* Add virtual function st_server_base::on_accept_error, it controls whether to continue the acception or not when error occurred.
*
*/
#ifndef ST_ASIO_WRAPPER_H_
#define ST_ASIO_WRAPPER_H_
#define ST_ASIO_WRAPPER_VER 10102 //[x]xyyzz -> [x]x.[y]y.[z]z
#define ST_ASIO_WRAPPER_VERSION "1.1.2"
#define ST_ASIO_WRAPPER_VER 10103 //[x]xyyzz -> [x]x.[y]y.[z]z
#define ST_ASIO_WRAPPER_VERSION "1.1.3"
#ifdef _MSC_VER
static_assert(_MSC_VER >= 1600, "st_asio_wrapper must be compiled with Visual C++ 10.0 or higher.");
......
......@@ -117,6 +117,21 @@ protected:
virtual void uninit() {ST_THIS stop(); stop_listen(); close_all_client();}
virtual bool on_accept(typename Pool::object_ctype& client_ptr) {return true;}
//if you want to ignore this error and continue to accept new connections immediately, return true in this virtual function;
//if you want to ignore this error and continue to accept new connections after a specific delay, start a timer immediately and return false (don't call stop_listen()),
// when the timer ends up, call start_next_accept() in the callback function.
//otherwise, don't rewrite this virtual function or call st_server_base::on_accept_error() directly after your code.
virtual bool on_accept_error(const boost::system::error_code& ec, typename Pool::object_ctype& client_ptr)
{
if (boost::asio::error::operation_aborted != ec)
{
unified_out::error_out("failed to accept new connection because of %s, will stop listening.", ec.message().data());
stop_listen();
}
return false;
}
virtual void start_next_accept()
{
auto client_ptr = ST_THIS create_object(boost::ref(*this));
......@@ -146,8 +161,8 @@ protected:
start_next_accept();
}
else
stop_listen();
else if (on_accept_error(ec, client_ptr))
start_next_accept();
}
protected:
......
......@@ -184,8 +184,8 @@ protected:
#ifdef ST_ASIO_ENHANCED_STABILITY
virtual bool on_exception(const std::exception& e)
{
unified_out::info_out("service pump exception: %s.", e.what());
return true; //continue this boost::asio::io_service::run, if needed, rewrite this to decide whether to continue or not
unified_out::error_out("service pump exception: %s.", e.what());
return true; //continue this io_service::run, if needed, rewrite this to decide whether to continue or not
}
size_t run(boost::system::error_code& ec)
......
......@@ -126,7 +126,6 @@ protected:
size_t size = 0;
auto end_time = super::statistic::local_time();
std::vector<boost::asio::const_buffer> bufs;
last_send_msg.clear();
for (auto iter = std::begin(ST_THIS send_msg_buffer); last_send_msg.empty();)
{
size += iter->size();
......@@ -228,20 +227,23 @@ private:
else
ST_THIS on_send_error(ec);
#ifdef ST_ASIO_WANT_ALL_MSG_SEND_NOTIFY
typename super::in_msg msg;
msg.swap(last_send_msg.back());
#endif
last_send_msg.clear();
boost::unique_lock<boost::shared_mutex> lock(ST_THIS send_msg_buffer_mutex);
ST_THIS sending = false;
//send msg sequentially, that means second send only after first send success
if (!ec)
#ifdef ST_ASIO_WANT_ALL_MSG_SEND_NOTIFY
if (!do_send_msg())
ST_THIS on_all_msg_send(last_send_msg.back());
if (!ec && !do_send_msg())
ST_THIS on_all_msg_send(msg);
#else
if (!ec)
do_send_msg();
#endif
if (!ST_THIS sending)
last_send_msg.clear();
}
protected:
......
......@@ -14,6 +14,11 @@
#define ST_ASIO_WRAPPER_TIMER_H_
#include <boost/container/set.hpp>
#ifdef ST_ASIO_USE_STEADY_TIMER
#include <boost/asio/steady_timer.hpp>
#elif defined(ST_ASIO_USE_SYSTEM_TIMER)
#include <boost/asio/system_timer.hpp>
#endif
#include "st_asio_wrapper_object.h"
......@@ -32,6 +37,23 @@ namespace st_asio_wrapper
class st_timer : public st_object
{
protected:
#if defined(ST_ASIO_USE_STEADY_TIMER) || defined(ST_ASIO_USE_SYSTEM_TIMER)
#ifdef BOOST_ASIO_HAS_STD_CHRONO
typedef std::chrono::milliseconds milliseconds;
#else
typedef boost::chrono::milliseconds milliseconds;
#endif
#ifdef ST_ASIO_USE_STEADY_TIMER
typedef boost::asio::steady_timer timer_type;
#else
typedef boost::asio::system_timer timer_type;
#endif
#else
typedef boost::posix_time::milliseconds milliseconds;
typedef boost::asio::deadline_timer timer_type;
#endif
struct timer_info
{
enum timer_status {TIMER_OK, TIMER_CANCELED};
......@@ -40,7 +62,7 @@ protected:
timer_status status;
size_t milliseconds;
std::function<bool(unsigned char)> call_back;
boost::shared_ptr<boost::asio::deadline_timer> timer;
boost::shared_ptr<timer_type> timer;
bool operator <(const timer_info& other) const {return id < other.id;}
};
......@@ -68,7 +90,7 @@ public:
iter = timer_can.insert(ti).first;
timer_can_mutex.unlock();
iter->timer = boost::make_shared<boost::asio::deadline_timer>(io_service_);
iter->timer = boost::make_shared<timer_type>(io_service_);
}
else
timer_can_mutex.unlock_upgrade();
......@@ -136,7 +158,7 @@ protected:
void start_timer(object_ctype& ti)
{
ti.timer->expires_from_now(boost::posix_time::milliseconds(ti.milliseconds));
ti.timer->expires_from_now(milliseconds(ti.milliseconds));
//return true from call_back to continue the timer, or the timer will stop
ti.timer->async_wait(
make_handler_error([this, &ti](const boost::system::error_code& ec) {if (!ec && ti.call_back(ti.id) && st_timer::object_type::TIMER_OK == ti.status) ST_THIS start_timer(ti);}));
......
......@@ -223,6 +223,12 @@ private:
else
ST_THIS on_send_error(ec);
#ifdef ST_ASIO_WANT_ALL_MSG_SEND_NOTIFY
typename super::in_msg msg;
msg.swap(last_send_msg);
#endif
last_send_msg.clear();
boost::unique_lock<boost::shared_mutex> lock(ST_THIS send_msg_buffer_mutex);
ST_THIS sending = false;
......@@ -231,13 +237,10 @@ private:
//for UDP in st_asio_wrapper, sending error will not stop the following sending.
#ifdef ST_ASIO_WANT_ALL_MSG_SEND_NOTIFY
if (!do_send_msg())
ST_THIS on_all_msg_send(last_send_msg);
ST_THIS on_all_msg_send(msg);
#else
do_send_msg();
#endif
if (!ST_THIS sending)
last_send_msg.clear();
}
protected:
......
......@@ -12,7 +12,7 @@
#define ST_ASIO_DEFAULT_UNPACKER stream_unpacker //non-protocol
//configuration
#include "../include/ext/st_asio_wrapper_net.h"
#include "../include/ext/st_asio_wrapper_client.h"
using namespace st_asio_wrapper;
using namespace st_asio_wrapper::ext;
......
......@@ -3,13 +3,14 @@
//configuration
#define ST_ASIO_SERVER_PORT 9527
#define ST_ASIO_ASYNC_ACCEPT_NUM 5
#define ST_ASIO_REUSE_OBJECT //use objects pool
//#define ST_ASIO_FORCE_TO_USE_MSG_RECV_BUFFER
#define ST_ASIO_MSG_BUFFER_SIZE 65536
#define ST_ASIO_DEFAULT_UNPACKER stream_unpacker //non-protocol
//configuration
#include "../include/ext/st_asio_wrapper_net.h"
#include "../include/ext/st_asio_wrapper_server.h"
using namespace st_asio_wrapper;
using namespace st_asio_wrapper::ext;
......@@ -89,6 +90,7 @@ int main(int argc, const char* argv[])
//restore configuration
#undef ST_ASIO_SERVER_PORT
#undef ST_ASIO_ASYNC_ACCEPT_NUM
#undef ST_ASIO_REUSE_OBJECT
#undef ST_ASIO_FREE_OBJECT_INTERVAL
#undef ST_ASIO_DEFAULT_PACKER
......
......@@ -3,6 +3,7 @@
//configuration
#define ST_ASIO_SERVER_PORT 9527
#define ST_ASIO_ASYNC_ACCEPT_NUM 5
//#define ST_ASIO_FORCE_TO_USE_MSG_RECV_BUFFER //force to use the msg recv buffer
#define ST_ASIO_ENHANCED_STABILITY
//#define ST_ASIO_DEFAULT_PACKER replaceable_packer
......@@ -99,6 +100,7 @@ int main(int argc, const char* argv[])
//restore configuration
#undef ST_ASIO_SERVER_PORT
#undef ST_ASIO_ASYNC_ACCEPT_NUM
#undef ST_ASIO_FORCE_TO_USE_MSG_RECV_BUFFER
#undef ST_ASIO_ENHANCED_STABILITY
#undef ST_ASIO_DEFAULT_PACKER
......
......@@ -29,7 +29,7 @@
#define ST_ASIO_DEFAULT_UNPACKER prefix_suffix_unpacker
#endif
#include "../include/ext/st_asio_wrapper_net.h"
#include "../include/ext/st_asio_wrapper_client.h"
using namespace st_asio_wrapper;
using namespace st_asio_wrapper::ext;
......
......@@ -6,7 +6,7 @@
//#define ST_ASIO_DEFAULT_UDP_UNPACKER replaceable_udp_unpacker
//configuration
#include "../include/ext/st_asio_wrapper_net.h"
#include "../include/ext/st_asio_wrapper_udp.h"
using namespace st_asio_wrapper;
using namespace st_asio_wrapper::ext;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册