file_server.cpp 3.5 KB
Newer Older
1

Y
youngwolf 已提交
2 3
#include <iostream>

4
//configuration
Y
youngwolf 已提交
5
#define ST_ASIO_DEFAULT_PACKER	packer2<>
Y
youngwolf 已提交
6 7
//#define ST_ASIO_RECV_BUFFER_TYPE std::vector<boost::asio::mutable_buffer> //scatter-gather buffer, it's very useful under certain situations (for example, ring buffer).
//#define ST_ASIO_SCATTERED_RECV_BUFFER //used by unpackers, not belongs to st_asio_wrapper
Y
youngwolf 已提交
8
//note, these two macro are not requisite, I'm just showing how to use them.
Y
youngwolf 已提交
9

Y
youngwolf 已提交
10
//all other definitions are in the makefile, because we have two cpp files, defining them in more than one place is risky (
Y
youngwolf 已提交
11
// we may define them to different values between the two cpp files)
12 13
//configuration

14
#include "file_socket.h"
15 16 17

#define QUIT_COMMAND	"quit"
#define RESTART_COMMAND	"restart"
Y
youngwolf 已提交
18 19 20
#define STATUS			"status"
#define STATISTIC		"statistic"
#define LIST_ALL_CLIENT	"list all client"
21

Y
youngwolf 已提交
22 23 24 25 26 27 28 29 30 31
#if !defined(_MSC_VER) && !defined(__MINGW64__) && !defined(__MINGW32__)
void signal_handler(service_pump& sp, boost::asio::signal_set& signal_receiver, const boost::system::error_code& ec, int signal_number)
{
	if (!ec)
		return sp.end_service();

	signal_receiver.async_wait(boost::bind(&signal_handler, boost::ref(sp), boost::ref(signal_receiver), boost::placeholders::_1, boost::placeholders::_2));
}
#endif

32
int main(int argc, const char* argv[])
33
{
Y
youngwolf 已提交
34 35
	puts("this is a file transmission server.");
#if defined(_MSC_VER) || defined(__MINGW64__) || defined(__MINGW32__)
Y
youngowlf 已提交
36
	printf("usage: %s [<port=%d> [ip=0.0.0.0]]\n", argv[0], ST_ASIO_SERVER_PORT);
Y
youngwolf 已提交
37 38 39
#else
	printf("usage: %s [-d] [<port=%d> [ip=0.0.0.0]]\n", argv[0], ST_ASIO_SERVER_PORT);
#endif
40 41 42 43
	if (argc >= 2 && (0 == strcmp(argv[1], "--help") || 0 == strcmp(argv[1], "-h")))
		return 0;
	else
		puts("type " QUIT_COMMAND " to end.");
44

Y
youngwolf 已提交
45 46 47 48 49 50 51 52 53 54
	int index = 0;
	if (argc >= 2 && 0 == strcmp(argv[1], "-d"))
	{
#if defined(_MSC_VER) || defined(__MINGW64__) || defined(__MINGW32__)
		puts("on windows, -d is not supported!");
		return 1;
#endif
		index = 1;
	}

55
	service_pump sp;
Y
youngwolf 已提交
56 57 58 59 60 61
#ifndef ST_ASIO_DECREASE_THREAD_AT_RUNTIME
	//if you want to decrease service thread at runtime, then you cannot use multiple io_context, if somebody indeed needs it, please let me know.
	//with multiple io_context, the number of service thread must be bigger than or equal to the number of io_context, please note.
	//with multiple io_context, please also define macro ST_ASIO_AVOID_AUTO_STOP_SERVICE.
	sp.set_io_context_num(8);
#endif
62
	server_base<file_socket> file_server_(sp);
63

Y
youngwolf 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
	if (argc > 2 + index)
		file_server_.set_server_addr(atoi(argv[1 + index]), argv[2 + index]);
	else if (argc > 1 + index)
		file_server_.set_server_addr(atoi(argv[1 + index]));

#if !defined(_MSC_VER) && !defined(__MINGW64__) && !defined(__MINGW32__)
	if (1 == index)
	{
		boost::asio::signal_set signal_receiver(sp, SIGINT, SIGTERM);
		signal_receiver.async_wait(boost::bind(&signal_handler, boost::ref(sp), boost::ref(signal_receiver), boost::placeholders::_1, boost::placeholders::_2));

		sp.run_service();
		return 0;
	}
#endif
79

Y
youngowlf 已提交
80 81
	sp.start_service();
	while(sp.is_running())
82
	{
Y
youngwolf 已提交
83
		std::string str;
84
		std::getline(std::cin, str);
Y
YangLi 已提交
85 86 87
		if (str.empty())
			;
		else if (QUIT_COMMAND == str)
Y
youngowlf 已提交
88
			sp.stop_service();
89
		else if (RESTART_COMMAND == str)
90
		{
Y
youngowlf 已提交
91 92
			sp.stop_service();
			sp.start_service();
93
		}
Y
youngwolf 已提交
94 95
		else if (STATISTIC == str)
		{
96
			printf("link #: " ST_ASIO_SF ", invalid links: " ST_ASIO_SF "\n\n", file_server_.size(), file_server_.invalid_object_size());
Y
youngwolf 已提交
97 98 99 100
			puts(file_server_.get_statistic().to_string().data());
		}
		else if (STATUS == str)
			file_server_.list_all_status();
101
		else if (LIST_ALL_CLIENT == str)
102
			file_server_.list_all_object();
103 104 105 106
	}

	return 0;
}