debug_assistant.cpp 4.1 KB
Newer Older
Y
youngwolf 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

#include <iostream>

//configuration
#define ASCS_SERVER_PORT	9527
#define ASCS_REUSE_OBJECT	//use objects pool
#define ASCS_MAX_OBJECT_NUM	1024
//configuration

#include <ascs/ext/tcp.h>
#include <ascs/ext/udp.h>
using namespace ascs;
using namespace ascs::tcp;
using namespace ascs::ext;
using namespace ascs::ext::tcp;

#define QUIT_COMMAND	"quit"
Y
youngwolf 已提交
18 19 20 21 22 23 24
#define STATUS			"status"

template<typename Object>
class timed_object_pool : public object_pool<Object>
{
private:
	typedef object_pool<Object> super;
W
wolf 已提交
25
	typedef typename super::tid tid; //old gcc needs this
Y
youngwolf 已提交
26 27 28 29 30 31 32 33

protected:
	timed_object_pool(service_pump& service_pump_) : super(service_pump_) {}

	void start()
	{
		super::start();

W
wolf 已提交
34
		this->set_timer(super::TIMER_END, 1000 * 60, [this](tid id)->bool {
Y
youngwolf 已提交
35 36 37 38 39 40 41 42 43 44
			auto now = time(nullptr);
			this->do_something_to_all([&now](typename super::object_ctype& object_ptr) {
				if (object_ptr->get_statistic().last_recv_time + 10 * 60 < now)
					object_ptr->force_shutdown();
			});

			return true;
		});
	}
};
Y
youngwolf 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

class echo_socket : public server_socket
{
public:
	echo_socket(i_server& server_) : server_socket(server_) {}

protected:
	//msg handling: send the original msg back(echo server)
	virtual bool on_msg_handle(out_msg_type& msg) {return send_msg(std::move(msg));}
	//msg handling end
};

class echo_stream_socket : public server_socket_base<dummy_packer<std::string>, stream_unpacker>
{
public:
	echo_stream_socket(i_server& server_) : server_socket_base<dummy_packer<std::string>, stream_unpacker>(server_) {}

protected:
	//msg handling: send the original msg back(echo server)
	virtual bool on_msg_handle(out_msg_type& msg) {return send_native_msg(std::move(msg));}
	//msg handling end
};

class single_udp_service : public ascs::ext::udp::single_socket_service
{
public:
	single_udp_service(service_pump& service_pump_) : ascs::ext::udp::single_socket_service(service_pump_) {}

protected:
	//msg handling: send the original msg back(echo server)
Y
youngwolf 已提交
75 76
	virtual bool on_msg_handle(out_msg_type& msg) {return direct_send_msg(std::move(msg));} //packer and unpacker have the same type of message
	//virtual bool on_msg_handle(out_msg_type& msg) {return send_native_msg(msg.peer_addr, std::move(msg));} //packer and unpacker have different types of message
Y
youngwolf 已提交
77 78 79
	//msg handling end
};

80
int main(int argc, const char* argv[])
Y
youngwolf 已提交
81
{
82 83 84
	auto daemon = false;
	if (argc >= 2 && 0 == strcmp(argv[1], "-d"))
	{
85
#if defined(_MSC_VER) || defined(__MINGW64__) || defined(__MINGW32__)
86 87 88 89 90 91 92 93 94
		puts("on windows, -d is not supported!");
		return 1;
#endif

		//setbuf(stdout, nullptr);
		setvbuf(stdout, nullptr, _IOLBF, 0);
		daemon = true;
	}

Y
youngwolf 已提交
95 96 97 98 99 100
	puts("echo server with length (2 bytes big endian) + body protocol: 9527\n"
		"echo server with non-protocol: 9528\n"
		"echo server udp: 9528\n"
		"type " QUIT_COMMAND " to end.");

	service_pump sp;
Y
youngwolf 已提交
101 102
	server_base<echo_socket, timed_object_pool<echo_socket>> echo_server(sp);
	server_base<echo_stream_socket, timed_object_pool<echo_stream_socket>> echo_stream_server(sp);
Y
youngwolf 已提交
103 104 105 106 107
	single_udp_service udp_service(sp);

	echo_stream_server.set_server_addr(9528);
	udp_service.set_local_addr(9528);

108
#if !defined(_MSC_VER) && !defined(__MINGW64__) && !defined(__MINGW32__)
109 110
	if (daemon)
	{
Y
youngwolf 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
		asio::signal_set signal_receiver(sp, SIGINT, SIGTERM, SIGUSR1);
		std::function<void (const asio::error_code&, int)> signal_handler = [&](const asio::error_code& ec, int signal_number) {
			if (!ec)
			{
				if (SIGUSR1 == signal_number)
				{
					echo_server.list_all_status();
					echo_stream_server.list_all_status();
				}
				else
					return sp.end_service();
			}

			signal_receiver.async_wait([&signal_handler](const asio::error_code& ec, int signal_number) {signal_handler(ec, signal_number);});
		};
		signal_receiver.async_wait([&signal_handler](const asio::error_code& ec, int signal_number) {signal_handler(ec, signal_number);});
127 128 129 130

		sp.run_service();
		return 0;
	}
131 132
#else
	(void) daemon;
Y
youngwolf 已提交
133
#endif
134

Y
youngwolf 已提交
135 136 137 138 139 140 141 142 143
	sp.start_service();
	while (sp.is_running())
	{
		std::string str;
		std::getline(std::cin, str);
		if (str.empty())
			;
		else if (QUIT_COMMAND == str)
			sp.stop_service();
Y
youngwolf 已提交
144 145 146 147 148
		else if (STATUS == str)
		{
			echo_server.list_all_status();
			echo_stream_server.list_all_status();
		}
Y
youngwolf 已提交
149 150 151 152 153 154 155
		else
		{
		}
	}

	return 0;
}