st_asio_wrapper_udp_socket.h 13.9 KB
Newer Older
1 2 3 4 5
/*
 * st_asio_wrapper_udp_socket.h
 *
 *  Created on: 2012-3-2
 *      Author: youngwolf
6 7 8
 *		email: mail2tao@163.com
 *		QQ: 676218192
 *		Community on QQ: 198941541
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
 *
 * this class used at both client and server endpoint
 */

#ifndef ST_ASIO_WRAPPER_UDP_SOCKET_H_
#define ST_ASIO_WRAPPER_UDP_SOCKET_H_

#include <boost/array.hpp>
#include <boost/container/list.hpp>

#include "st_asio_wrapper_packer.h"
#include "st_asio_wrapper_timer.h"

using namespace boost::asio::ip;

//#define FORCE_TO_USE_MSG_RECV_BUFFER
//use msg recv buffer all the time, you can gain some performance improvement because st_udp_socket will not
//invoke on_msg() to decide whether to use msg recv buffer or not, but directly push the msgs into
//msg recv buffer. notice: there's no on_msg() virtual function any more.
//this is a compile time optimization

#if defined _MSC_VER
#define size_t_format "%Iu"
#else // defined __GNUC__
#define size_t_format "%tu"
#endif

//in set_server_addr, if the ip is empty, DEFAULT_IP_VERSION will define the ip version,
//or, the ip version will be determined by the ip address.
//tcp::v4() means ipv4 and tcp::v6() means ipv6.
#ifndef UDP_DEFAULT_IP_VERSION
#define UDP_DEFAULT_IP_VERSION udp::v4()
#endif

///////////////////////////////////////////////////
//msg sending interface
#define UDP_SEND_MSG_CALL_SWITCH(FUNNAME, TYPE) \
TYPE FUNNAME(const udp::endpoint& peer_addr, const char* pstr, size_t len, bool can_overflow = false) \
	{return FUNNAME(peer_addr, &pstr, &len, 1, can_overflow);} \
TYPE FUNNAME(const udp::endpoint& peer_addr, const std::string& str, bool can_overflow = false) \
	{return FUNNAME(peer_addr, str.data(), str.size(), can_overflow);}

#define UDP_SEND_MSG(FUNNAME, NATIVE) \
bool FUNNAME(const udp::endpoint& peer_addr, const char* const pstr[], const size_t len[], size_t num, \
	bool can_overflow = false) \
{ \
	mutex::scoped_lock lock(send_msg_buffer_mutex); \
56 57
	return (can_overflow || send_msg_buffer.size() < MAX_MSG_NUM) ? \
		direct_insert_msg(peer_addr, packer_->pack_msg(pstr, len, num, NATIVE)) : false; \
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
} \
UDP_SEND_MSG_CALL_SWITCH(FUNNAME, bool)
//msg sending interface
///////////////////////////////////////////////////

namespace st_asio_wrapper
{

class st_udp_socket : public udp::socket, public st_timer
{
public:
	struct udp_msg
	{
		udp::endpoint peer_addr;
		std::string str;

		void move(udp_msg& other) {peer_addr = other.peer_addr; str.swap(other.str);}
		void move(const udp::endpoint& addr, std::string& tmp_str) {peer_addr = addr; str.swap(tmp_str);}
		bool operator==(const udp_msg& other) const {return this == &other;}
	};
	typedef udp_msg msg_type;
	typedef const msg_type msg_ctype;

public:
	st_udp_socket(io_service& io_service_) : udp::socket(io_service_), st_timer(io_service_),
		sending(false), dispatching(false), packer_(boost::make_shared<packer>()) {}

	void set_local_addr(unsigned short port, const std::string& ip = std::string())
	{
		error_code ec;
		if (ip.empty())
			local_addr = udp::endpoint(UDP_DEFAULT_IP_VERSION, port);
		else
		{
			local_addr = udp::endpoint(address::from_string(ip, ec), port);
			assert(!ec);
		}
	}

	void start()
	{
		if (!get_io_service().stopped())
			async_receive_from(buffer(raw_buff), peer_addr,
				boost::bind(&st_udp_socket::recv_handler, this, placeholders::error, placeholders::bytes_transferred));
	}

	//reset all, be ensure that there's no any operations performed on this st_udp_socket when invoke it
	virtual void reset()
	{
		reset_state();
		clear_buffer();

		error_code ec;
		close(ec);
		open(local_addr.protocol(), ec); assert(!ec);
#ifndef NOT_REUSE_ADDRESS
		set_option(socket_base::reuse_address(true), ec); assert(!ec);
#endif
		bind(local_addr, ec); assert(!ec);
		if (ec) {unified_out::error_out("bind failed.");}
	}
	void reset_state()
	{
		sending = false;
		dispatching = false;
	}
	void clear_buffer()
	{
		send_msg_buffer.clear();
		recv_msg_buffer.clear();
		temp_msg_buffer.clear();
	}

	void disconnect() {force_close();}
	void force_close() {clean_up();}
	void graceful_close() {clean_up();}

	//get or change the packer at runtime
	//udp does not need a unpacker
	boost::shared_ptr<i_packer> inner_packer() const {return packer_;}
	void inner_packer(const boost::shared_ptr<i_packer>& _packer_) {packer_ = _packer_;};

	///////////////////////////////////////////////////
	//msg sending interface
	UDP_SEND_MSG(send_msg, false); //use the packer with native = false to pack the msgs
	UDP_SEND_MSG(send_native_msg, true); //use the packer with native = true to pack the msgs
	//msg sending interface
	///////////////////////////////////////////////////

	//don't use the packer but insert into the send_msg_buffer directly
	bool direct_send_msg(const udp::endpoint& peer_addr, const std::string& str, bool can_overflow = false)
		{return direct_send_msg(peer_addr, std::string(str), can_overflow);}
	bool direct_send_msg(const udp::endpoint& peer_addr, std::string&& str, bool can_overflow = false)
	{
		mutex::scoped_lock lock(send_msg_buffer_mutex);
		if (can_overflow || send_msg_buffer.size() < MAX_MSG_NUM)
			return direct_insert_msg(peer_addr, std::move(str));

		return false;
	}

	//send buffered msgs, return false if send buffer is empty or invalidate status
	bool send_msg()
	{
		mutex::scoped_lock lock(send_msg_buffer_mutex);
		return do_send_msg();
	}

	//generally used after the service stopped
	void direct_dispatch_all_msg()
	{
		//mutex::scoped_lock lock(recv_msg_buffer_mutex);
		if (!recv_msg_buffer.empty() || !temp_msg_buffer.empty())
		{
			recv_msg_buffer.splice(std::end(recv_msg_buffer), temp_msg_buffer);
			st_asio_wrapper::do_something_to_all(recv_msg_buffer, boost::bind(&st_udp_socket::on_msg_handle, this, _1));
			recv_msg_buffer.clear();
		}

		dispatching = false;
	}

	//how many msgs waiting for send
	size_t get_pending_msg_num()
	{
		mutex::scoped_lock lock(send_msg_buffer_mutex);
		return send_msg_buffer.size();
	}

	void peek_first_pending_msg(msg_type& msg)
	{
		msg.str.clear();
		mutex::scoped_lock lock(send_msg_buffer_mutex);
		if (!send_msg_buffer.empty())
			msg = send_msg_buffer.front();
	}

	void pop_first_pending_msg(msg_type& msg)
	{
		msg.str.clear();
		mutex::scoped_lock lock(send_msg_buffer_mutex);
		if (!send_msg_buffer.empty())
		{
			msg.move(send_msg_buffer.front());
			send_msg_buffer.pop_front();
		}
	}

	//clear all pending msgs
	void pop_all_pending_msg(container::list<msg_type>& unsend_msg_list)
	{
		mutex::scoped_lock lock(send_msg_buffer_mutex);
		unsend_msg_list.splice(std::end(unsend_msg_list), send_msg_buffer);
	}

protected:
	virtual void on_recv_error(const error_code& ec) {}
	virtual void on_send_error(const error_code& ec) {}

#ifndef FORCE_TO_USE_MSG_RECV_BUFFER
	//if you want to use your own recv buffer, you can move the msg to your own recv buffer,
	//and return false, then, handle the msg as your own strategy(may be you'll need a msg dispatch thread)
	//or, you can handle the msg at here and return false, but this will reduce efficiency(
	//because this msg handling block the next msg receiving on the same st_socket) unless you can
	//handle the msg very fast(which will inversely more efficient, because msg recv buffer and msg dispatching
	//are not needed any more).
	//
	//return true means use the msg recv buffer, you must handle the msgs in on_msg_handle()
	//notice: on_msg_handle() will not be invoked from within this function
	//
	//notice: using inconstant is for the convenience of std::move
	virtual bool on_msg(msg_type& msg)
		{unified_out::debug_out("recv(" size_t_format "): %s", msg.str.size(), msg.str.data()); return false;}
#endif

	//handling msg at here will not block msg receiving
	//if on_msg() return false, this function will not be invoked due to no msgs need to dispatch
	//notice: using inconstant is for the convenience of std::move
	virtual void on_msg_handle(msg_type& msg)
		{unified_out::debug_out("recv(" size_t_format "): %s", msg.str.size(), msg.str.data());}

#ifdef WANT_MSG_SEND_NOTIFY
	//one msg has sent to the kernel buffer
	virtual void on_msg_send(msg_type& msg) {}
#endif
#ifdef WANT_ALL_MSG_SEND_NOTIFY
	//send buffer goes empty
	virtual void on_all_msg_send(msg_type& msg) {}
#endif

	virtual bool on_timer(unsigned char id, const void* user_data)
	{
		switch (id)
		{
		case 0: //delay dispatch msgs because of recv buffer overflow
			if (dispatch_msg())
				start(); //recv msg sequentially, that means second recv only after first recv success
			else
				return true;
			break;
		case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: //reserved
			break;
		default:
			return st_timer::on_timer(id, user_data);
			break;
		}

		return false;
	}

	void clean_up()
	{
		error_code ec;
		shutdown(udp::socket::shutdown_both, ec);
		close(ec);

		stop_all_timer();
		sending = false;
	}

	bool dispatch_msg()
	{
		if (temp_msg_buffer.empty())
			return true;

		auto dispatch = false;
		mutex::scoped_lock lock(recv_msg_buffer_mutex);
		auto msg_num = recv_msg_buffer.size();
#ifndef FORCE_TO_USE_MSG_RECV_BUFFER //inefficient
		for (auto iter = std::begin(temp_msg_buffer); iter != std::end(temp_msg_buffer);)
			if (!on_msg(*iter))
				temp_msg_buffer.erase(iter++);
			else if (msg_num < MAX_MSG_NUM) //msg recv buffer available
			{
				dispatch = true;
				recv_msg_buffer.splice(std::end(recv_msg_buffer), temp_msg_buffer, iter++);
				++msg_num;
			}
			else
				++iter;
#else //efficient
		if (msg_num < MAX_MSG_NUM) //msg recv buffer available
		{
			dispatch = true;
			msg_num = MAX_MSG_NUM - msg_num; //max msg number this time can handle
			auto begin_iter = std::begin(temp_msg_buffer), end_iter = std::end(temp_msg_buffer);
			if (temp_msg_buffer.size() > msg_num) //some msgs left behind
			{
				auto left_num = temp_msg_buffer.size() - msg_num;
				//find the minimum movement
				end_iter = left_num > msg_num ? std::next(begin_iter, msg_num) : std::prev(end_iter, left_num);
			}
			else
				msg_num = temp_msg_buffer.size();
			//use msg_num to avoid std::distance() call, so, msg_num must correct
			recv_msg_buffer.splice(std::end(recv_msg_buffer), temp_msg_buffer, begin_iter, end_iter, msg_num);
		}
#endif

		if (dispatch)
			do_dispatch_msg();
		lock.unlock();

		return temp_msg_buffer.empty();
	}

	void recv_handler(const error_code& ec, size_t bytes_transferred)
	{
		if (!ec && bytes_transferred > 0)
		{
			std::string tmp_str(raw_buff.data(), bytes_transferred);
			temp_msg_buffer.push_back(msg_type());
			temp_msg_buffer.back().move(peer_addr, tmp_str);
			auto all_dispatched = dispatch_msg();

			if (all_dispatched)
				start(); //recv msg sequentially, that means second recv only after first recv success
			else
				set_timer(0, 50, nullptr);
		}
#ifdef _MSC_VER
		else if (WSAECONNREFUSED == ec.value())
			start();
#endif
		else
			on_recv_error(ec);
	}

	void send_handler(const error_code& ec, size_t bytes_transferred)
	{
		if (!ec)
		{
			assert(bytes_transferred > 0);
#ifdef WANT_MSG_SEND_NOTIFY
			on_msg_send(last_send_msg);
#endif
		}
		else
			on_send_error(ec);
		//under windows, send a msg to addr_any may cause sending errors, please note
		//for udp in st_asio_wrapper, sending error will not stop the following sending.

		mutex::scoped_lock lock(send_msg_buffer_mutex);
		sending = false;
		//send msg sequentially, that means second send only after first send success
		if (!do_send_msg())
#ifdef WANT_ALL_MSG_SEND_NOTIFY
			on_all_msg_send(last_send_msg)
#endif
			;
	}

	void msg_handler()
	{
		on_msg_handle(last_dispatch_msg); //must before next msg dispatch to keep sequence
		mutex::scoped_lock lock(recv_msg_buffer_mutex);
		dispatching = false;
		//dispatch msg sequentially, that means second dispatch only after first dispatch success
		do_dispatch_msg();
	}

	//must mutex send_msg_buffer before invoke this function
	bool do_send_msg()
	{
		auto state = !get_io_service().stopped() && is_open();
		if (!state)
			sending = false;
		else if (!sending && !send_msg_buffer.empty())
		{
			sending = true;
			last_send_msg.move(send_msg_buffer.front());
			async_send_to(buffer(last_send_msg.str), last_send_msg.peer_addr, boost::bind(&st_udp_socket::send_handler, this,
				placeholders::error, placeholders::bytes_transferred));
			send_msg_buffer.pop_front();
		}

		return sending;
	}

	//must mutex recv_msg_buffer before invoke this function
	void do_dispatch_msg()
	{
		auto& io_service_ = get_io_service();
		auto state = !io_service_.stopped();
		if (!state)
			dispatching = false;
		else if (!dispatching && !recv_msg_buffer.empty())
		{
			dispatching = true;
			last_dispatch_msg.move(recv_msg_buffer.front());
			io_service_.post(boost::bind(&st_udp_socket::msg_handler, this));
			recv_msg_buffer.pop_front();
		}
	}

	//must mutex send_msg_buffer before invoke this function
	bool direct_insert_msg(const udp::endpoint& peer_addr, std::string&& str)
	{
		if (!str.empty())
		{
			send_msg_buffer.push_back(msg_type());
			send_msg_buffer.back().move(peer_addr, str);
			do_send_msg();

			return true;
		}

		return false;
	}

protected:
	msg_type last_send_msg, last_dispatch_msg;

	//keep size() constant time would better, because we invoke it frequently, so don't use std::list(gcc)
	container::list<msg_type> send_msg_buffer;
	mutex send_msg_buffer_mutex;
	bool sending;

	//keep size() constant time would better, because we invoke it frequently, so don't use std::list(gcc)
	//using this msg recv buffer or not is decided by the return value of on_msg()
	//see on_msg() for more details
	container::list<msg_type> recv_msg_buffer;
	mutex recv_msg_buffer_mutex;
	bool dispatching;

	//if on_msg() return true, which means use the msg recv buffer,
	//st_udp_socket will invoke dispatch_msg() when got some msgs. if the msgs can't push into recv_msg_buffer
	//because of recv buffer overflow, st_udp_socket will delay 50 milliseconds(nonblocking) to invoke
	//dispatch_msg() again, and now, as you known, temp_msg_buffer is used to hold these msgs temporarily.
	container::list<msg_type> temp_msg_buffer;

	boost::shared_ptr<i_packer> packer_;
	array<char, MAX_MSG_LEN> raw_buff;
	udp::endpoint peer_addr;

	udp::endpoint local_addr;
};

} //namespace

#endif /* ST_ASIO_WRAPPER_UDP_SOCKET_H_ */