示例#1
0
int32_t CoreUDPHandler::send(BinStream& bin_strm, const Inet_Addr& remote_addr)
{
    int32_t rc = sock_dgram_.send(bin_strm.get_rptr(), bin_strm.data_size(), remote_addr);
    if(rc < 0)
    {
        if(XEAGAIN == error_no() || XEINPROGRESS == error_no()) //插入一个写事件,防止SOCKET异常
        {
            CORE_WARNING("core udp send EAGAIN");
            REACTOR_INSTANCE()->register_handler(this, MASK_WRITE);

            bin_strm.rewind(true);
            return 0;
        }
        else
        {
            bin_strm.rewind(true);
            CORE_FATAL("core udp send failed, remote addr = " << remote_addr);
            return -1;
        }

        bin_strm.rewind(true);
    }

    THROTTLER()->add_udp_packet(bin_strm.data_size(), true);

    bin_strm.rewind(true);
    return rc;
}
示例#2
0
int sock_acceptor::open(const Address& local_addr, int recv_bufsize, int send_bufsize)
{
    STACK_TRACE_LOG();

    int rc = m_socket_.open(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (0 != rc)
    {
        LOG(ERROR)("sock_acceptor::open error, open error, errno:%d", error_no());
        return -1;
    }

    Socket::setnonblock(Descriptor(m_socket_));

    //ÉèÖÃÍøÂçµ×²ãÊÕ·¢»º³åÇø³¤¶È
    Socket::setsockopt(Descriptor(m_socket_), SOL_SOCKET, SO_RCVBUF, reinterpret_cast<char*>(&recv_bufsize), sizeof(recv_bufsize));
    int real_rcvbufsize = 0;
    socklen_t nsize = sizeof(real_rcvbufsize);
    Socket::getsockopt(Descriptor(m_socket_), SOL_SOCKET, SO_RCVBUF, reinterpret_cast<char*>(&real_rcvbufsize), &nsize);
    if (real_rcvbufsize != recv_bufsize)
    {
        LOG(WARN)("sock_acceptor::open error, setsockopt so_rcvbuf failed. set_size:%d real_size:%d.", recv_bufsize, real_rcvbufsize);
    }    

    Socket::setsockopt(Descriptor(m_socket_), SOL_SOCKET, SO_SNDBUF, reinterpret_cast<char*>(&send_bufsize), sizeof(send_bufsize));
    int real_sndbufsize = 0;
    Socket::getsockopt(Descriptor(m_socket_), SOL_SOCKET, SO_SNDBUF, reinterpret_cast<char*>(&real_sndbufsize), &nsize);
    if (real_sndbufsize != send_bufsize)
    {
        LOG(WARN)("sock_acceptor::open error, setsockopt so_sndbuf failed. set_size:%d real_size:%d.", send_bufsize, real_sndbufsize);
    }

    int val = 1;
    Socket::setsockopt(Descriptor(m_socket_), SOL_SOCKET, SO_REUSEADDR, (char*)&val, sizeof(val));

    rc = m_socket_.bind(local_addr);
    if (0 != rc)
    {
        LOG(ERROR)("sock_acceptor::open error, bind error, errno:%d", error_no());
        m_socket_.close();
        return -1;
    }

    rc = m_socket_.listen(10240);
    if (0 != rc)
    {
        LOG(ERROR)("sock_acceptor::open error, listen error, errno:%d", error_no());
        m_socket_.close();
        return -1;
    }

    return 0;
}
示例#3
0
int32_t CConnection::handle_exception(BASE_HANDLER handle)
{
	CORE_DEBUG("handle_exception, error = " << error_no());
	if(state_ != CONN_IDLE)
	{
		//通知服务器节点管理模块
		CONN_MANAGER()->on_del_connection(this);

		//通知上层连接断开
		MSG_PROCESSOR()->on_disconnect(server_id_, this);
		
		sock_stream_.close();

		reset();
		//将连接对象返回给连接池
		CONNECTION_POOL.push_obj(this);
		CORE_DEBUG("push conn = " << this);
	}
	else
	{
		sock_stream_.close();
	}
	
	return 0;
}
示例#4
0
int32_t RecvUDPHandler::send(BinStream& bin_strm, const Inet_Addr& remote_addr)
{
	//if(rand() % 50 == 1)
	//	return 0;

	int32_t rc = sock_dgram_.send(bin_strm.get_rptr(), bin_strm.data_size(), remote_addr);
	if(rc < 0)
	{
		if(XEAGAIN == error_no() || XEINPROGRESS == error_no()) //插入一个写事件,防止SOCKET异常
		{
			REACTOR_INSTANCE()->register_handler(this, MASK_WRITE);

			return 0;
		}
		else
		{
			return -1;
		}
	}

	return rc;
}
示例#5
0
int sock_acceptor::handle_output()
{
    STACK_TRACE_LOG();
    LOG(ERROR)("sock_acceptor::handle_output, errno:%d", error_no());
    return -1;
}
示例#6
0
int sock_acceptor::handle_input()
{
    STACK_TRACE_LOG();

    uint32_t count = 0;
    while (true)
    {
        Address remote_addr;
        Descriptor accept_socket = m_socket_.accept(remote_addr);
        LOG(INFO)("sock_acceptor::handle_input accept_socket:%d, accept_times:%u", accept_socket, ++count);
        if ((INVALID_SOCKET == accept_socket) || (0 > accept_socket))
        {
            // 0 == error_no(): not client connecting keep acceptor
            // The socket is marked non-blocking and no connections are present to be accepted.
            if ( 0 == error_no() || SYS_EAGAIN == error_no() || SYS_EWOULDBLOCK == error_no() || SYS_EMFILE == error_no() || ENFILE == error_no() )
            {
                return 0;
            }

            // exception
            LOG(ERROR)("sock_acceptor::handle_input, accept error, errno:%d", error_no());
            return -2;
        }

        // got a new connection
        Socket::setnonblock(accept_socket);

        int32_t net_id = m_net_messenger_->acquire_net_id(net_id_holder::ID_TCP_STREAM);
        if (0 == net_id)
        {
            LOG(ERROR)("sock_acceptor::handle_input, acquire net id error, errno:%d", error_no());
            return -1;
        }

        sock_stream* stream = sock_stream::m_pool_->Create();
        if ( NULL == stream )
        {
            LOG(ERROR)("assert: sock_acceptor::handle_input, new sock_stream is NULL");
            return -1;
        }

        stream->init(m_net_messenger_, m_packet_splitter_, m_user_data_, net_id, remote_addr, accept_socket);
        
        //×¢²á¶Áдʼþ
        int rc = m_reactor_->enable_handler(stream, ihandler::EM_READ | ihandler::EM_WRITE);
        if (0 != rc)
        {
            LOG(ERROR)("sock_acceptor::handle_input, enable_handler MASK_READ error");
            stream->close();
            stream->Destroy();
            return 0;
        }

        // throw a net event
        net_event* netev = net_event::m_pool_->Create();
        if ( NULL == netev )
        {
            LOG(ERROR)("assert: sock_connector::handle_input, new netev is NULL");
            return -1;
        }

        netev->m_net_ev_t_ = net_event::NE_ACCEPT;
        netev->m_net_id_ = stream->m_net_id_;
        netev->m_user_data_ = m_user_data_;
        netev->m_remote_addr_ = remote_addr;

        m_net_messenger_->push_event(netev);
    }

    return 0;
}
示例#7
0
int32_t CCoreTCPListener::open(const Inet_Addr& local_addr)
{
	if(acceptor_.open(local_addr, true) == 0)
	{
		REACTOR_INSTANCE()->register_handler(this, MASK_READ);

		CORE_INFO("open tcp listener, listener addr = " << local_addr);
		return 0;
	}
	else
	{
		CORE_ERROR("open tcp listener failed, listener addr = " << local_addr << ",error = " << error_no());
		return -1;
	}
}
示例#8
0
int32_t CEpollReactor::event_loop()
{
	if(epfd_ == -1)
	{
		return -1;
	}

	int32_t count = epoll_wait(epfd_, events_, nevent_, epoll_delay_);
	if(count < 0)
	{
		if(error_no() == EINTR)
		{
			return 0;
		}

		return -1;
	}

	CEventHandler* handler = 0;
	BASE_HANDLER handler_id = -1;

	for(register int32_t i = 0; i < count; ++i)
	{
		handler = (CEventHandler *)events_[i].data.ptr;
		if(handler == 0)
		{
			continue;
		}

		handler_id = handler->get_handle();
		
		if(events_[i].events & EPOLLIN) //���¼�
		{
			int32_t rc = handler->handle_input(handler_id);

			check_connection(rc, handler);
			if(rc < 0)
			{
				continue;
			}
		}
		
		if(events_[i].events & EPOLLOUT) //д�¼�
		{
			int32_t rc = handler->handle_output(handler_id);

			check_connection(rc, handler);
			if(rc < 0)
			{
				continue;
			}
		}

		if((events_[i].events & EPOLLHUP) /*| (events_[i].events & EPOLLRDHUP)*/) //�ر��¼�
		{
			delete_handler(handler);

			handler->handle_close(handler_id, MASK_TIMEOUT);
			continue;
		}

		if(events_[i].events & EPOLLERR)//�쳣�¼�
		{
			delete_handler(handler);

			handler->handle_close(handler_id, MASK_TIMEOUT);
		}
	}

	//��������¼���
	if(count >= nevent_ && nevent_ < MAX_NEVENT) //8192��LIB EVENT�е�����¼���
	{
		nevent_ = 2 * nevent_;
	}

	//ɨ�趨ʱ��
	uint64_t cur_ts = CBaseTimeValue::get_time_value().msec();
	if(cur_ts > prev_ts_ + 5)
	{
		epoll_delay_ = timer_queue_.expire();

		//ɨ���ڲ�����
		if(msg_proc_ != 0)
		{
			msg_proc_->processor();
		}

		prev_ts_ = cur_ts;
	}
	else
		epoll_delay_ = 5;

	return 0;
}