Beispiel #1
0
void NetWork::event_loop()
{
	TRACE(__PRETTY_FUNCTION__);

	//最大时间间隔是1s
	int timeout = 1000;
	IOEvent events[128];
	while (!_stop) {
		memset(events, 0, sizeof(events));
		int n = _sock_event->get_events(timeout, events, 128);
		for (int i = 0; i < n; i++) {
			IOEvent &ev = events[i];
			TcpConnection *conn = ev.conn;
			conn->add_ref();
			if (ev._read_ocurr)
				conn->on_read_event();
			if (ev._write_ocurr) {
				conn->on_write_event();
			}
			conn->release();
		}
		//定时处理
		time_process(time(NULL));
	}

}
Beispiel #2
0
TcpConnection *NetWork::OnlineUser::getconn(int handle)
{
	Guard g(_mutex);
	TcpConnection *conn = NULL;
	ONLINE_USER_ITER iter = _conns.find(handle);
	if (iter != _conns.end()) {
		conn = iter->second;
		conn->add_ref();
	}

	return conn;
}
Beispiel #3
0
//暂不支持timeout清理工作
void NetWork::OnlineUser::check_timeout(std::map<int, int> &timeout_list)
{
	TcpConnection *conn = NULL;
	time_t now = time(0);

	Guard g(_mutex);
	ONLINE_USER_ITER iter = _conns.begin();
	list<int> timeout_conns;
	for (; iter != _conns.end(); ++iter) {
		conn = iter->second;
		conn->add_ref();

		//连接超时检测
		if (conn->getstate() == CONNECTING || conn->getstate() == CRYPTREGING) {
			//连接超时时间是5s
			if (now - conn->get_last_active_time() > 5) {
				LOGW("handle :%d connect timeout", conn->gethandle());
				//!!!注意:close不能再此处调用!否则与外层的_conns循环混到一起了
				//close(conn->gethandle());
				//onClose(conn->gethandle(), SOCKET_ERROR_TIMEOUT);
				conn->set_last_error(SOCKET_ERROR_CONN_TIMEOUT);
				timeout_list.insert(make_pair((int) conn->gethandle(), conn->get_last_error()));
			}
		} else if (conn->getstate() == CONNECTED) {
			//10分钟没有任何响应就强制回收吧
			if (now - conn->get_last_active_time() > 60 * 10) {
				LOGW("handle :%d timeout", conn->gethandle());
				conn->set_last_error(SOCKET_ERROR_TIMEOUT);
				timeout_list.insert(make_pair((int) conn->gethandle(), conn->get_last_error()));
			}
		}

		conn->release();
	}

	return;
}