示例#1
0
int32_t RUDPObject::bind(int32_t rudp_id, const Inet_Addr& local_addr)
{
	RUDPSocket* rudp_session = get_socket(rudp_id);
	if(rudp_session == NULL)
	{
		RUDP_FATAL("rudp_session is NULL, rudp socket id = " << rudp_id);
		return -1;
	}

	for(uint8_t i = 0; i < adapter_array_.size(); ++i)
	{
		if(adapter_array_[i] != NULL && local_addr == adapter_array_[i]->get_local_addr())
		{
			if(rudp_session->bind(i, adapter_array_[i]->get_title()) != 0)//绑定本地的一个UDP发送接口
			{
				RUDP_FATAL("bind failed, rudp socket = " << rudp_id);
				return -1;
			}
			else
				return 0;
		}
	}

	return -1;
}
示例#2
0
void RUDPSocket::process_syn_ack(BinStream& strm, const Inet_Addr& remote_addr)
{
    RUDP_INFO("recv syn ack from " << remote_addr << ", rudp id = " << rudp_id_);

    RUDP()->delete_peer_index(remote_rudp_id_, remote_addr_);

    RUDP_INFO("recv syn ack from " << remote_addr);
    if(state_ != RUDP_CONNECTED)
    {
        RUDP_FATAL("state != RUDP_CONNECTED");
        return ;
    }

    PARSE_RUDP_MESSAGE(strm, RUDPSyn2AckPacket, ack, "parse syn_ack failed!");
    //计算RTT
    uint64_t now_ts = CBaseTimeValue::get_time_value().msec();
    uint32_t rtt = static_cast<uint32_t>(now_ts > ack.remote_ts_ ? (now_ts - ack.remote_ts_) : 5);
    ccc_.set_rtt(rtt);

    RUDP_INFO("rtt = " << rtt << ", rudp socket id = " << rudp_id_);

    //触发一个写事件
    if(event_handler_ != NULL)
        event_handler_->rudp_output_event(rudp_id_);
}
示例#3
0
uint32_t RUDPObject::send_bandwidth(int32_t rudp_id)
{
	RUDPSocket* rudp_session = get_socket(rudp_id);
	if(rudp_session == NULL)
	{
		RUDP_FATAL("rudp_session is NULL, rudp socket id = " << rudp_id);
		return 0;
	}

	return rudp_session->send_bandwidth();
}
示例#4
0
int32_t	RUDPObject::get_error(int32_t rudp_id)
{
	RUDPSocket* rudp_session = get_socket(rudp_id);
	if(rudp_session == NULL)
	{
		RUDP_FATAL("rudp_session is NULL, rudp socket id = " << rudp_id);
		return -1;
	}

	return rudp_session->get_error();
}
示例#5
0
int32_t RUDPObject::recv(int32_t rudp_id, uint8_t* data, int32_t data_size)
{
	RUDPSocket* rudp_session = get_socket(rudp_id);
	if(rudp_session == NULL)
	{
		RUDP_FATAL("rudp_session is NULL, rudp socket id = " << rudp_id);
		return -1;
	}

	return rudp_session->recv(data, data_size);
}
示例#6
0
int32_t RUDPObject::recv_cache_size(int32_t rudp_id)
{
	RUDPSocket* rudp_session = get_socket(rudp_id);
	if(rudp_session == NULL)
	{
		RUDP_FATAL("rudp_session is NULL, rudp socket id = " << rudp_id);
		return 0;
	}

	return rudp_session->get_recv_cache_size();
}
示例#7
0
int32_t RUDPObject::setoption(int32_t rudp_id, int32_t op_type, int32_t op_value)
{
	RUDPSocket* rudp_session = get_socket(rudp_id);
	if(rudp_session == NULL)
	{
		RUDP_FATAL("rudp_session is NULL, rudp socket id = " << rudp_id);
		return -1;
	}

	return rudp_session->setoption(op_type, op_value);
}
示例#8
0
void RUDPObject::force_close_socket(int32_t rudp_id)
{
	RUDPSocket* rudp_session = get_socket(rudp_id);
	if(rudp_session == NULL)
	{
		RUDP_FATAL("rudp_session is NULL, rudp socket id = " << rudp_id);
		return;
	}

	rudp_session->force_close();
}
示例#9
0
void RUDPObject::bind_event_handle(int32_t rudp_id, RUDPEventHandler* event_handler)
{
	RUDPSocket* rudp_session = get_socket(rudp_id);
	if(rudp_session == NULL)
	{
		RUDP_FATAL("rudp_session is NULL, rudp socket id = " << rudp_id);
		return ;
	}

	return rudp_session->set_event_handler(event_handler);
}
示例#10
0
void RUDPObject::register_event(int32_t rudp_id, int32_t marks)
{
	RUDPSocket* rudp_session = get_socket(rudp_id);
	if(rudp_session == NULL)
	{
		RUDP_FATAL("rudp_session is NULL, rudp socket id = " << rudp_id);
		return ;
	}

	rudp_session->register_event(marks);
}
示例#11
0
int32_t RUDPObject::connect(int32_t rudp_id, const Inet_Addr& remote_addr)
{
	RUDPSocket* rudp_session = get_socket(rudp_id);
	if(rudp_session == NULL)
	{
		RUDP_FATAL("rudp_session is NULL, rudp socket id = " << rudp_id);
		return -1;
	}

	return rudp_session->connect(remote_addr);
}
示例#12
0
int32_t RUDPSocket::recv(uint8_t* data, int32_t data_size)
{
    if(state_ != RUDP_CONNECTED || data_size <= 0)
    {
        RUDP_FATAL("recv failed! state_ != RUDP_CONNECTED or data size = 0, rudp id = " << rudp_id_);
        error_code_ = RUDP_SEND_ERROR;
        return -1;
    }

    return recv_buffer_.read(data, data_size);

}
示例#13
0
int32_t RUDPObject::get_peer_addr(int32_t rudp_id, Inet_Addr& addr)
{
	RUDPSocket* rudp_session = get_socket(rudp_id);
	if(rudp_session == NULL)
	{
		RUDP_FATAL("rudp_session is NULL, rudp socket id = " << rudp_id);
		return -1;
	}

	addr = rudp_session->get_peer_addr();
	return 0;
}
示例#14
0
int32_t RUDPSocket::bind(uint8_t index, uint8_t title)
{
    if(rudp_id_ == -1 || state_ != RUDP_IDLE)
    {
        RUDP_FATAL("rudp socket bind failed!");
        error_code_ = RUDP_BIND_FAIL;
        return -1;
    }

    local_index_ = index;
    local_title_ = title;

    return 0;
}
示例#15
0
int32_t RUDPSocket::setoption(int32_t op_type, int32_t op_value)
{
    if(rudp_id_ == -1 || state_ > RUDP_FIN_STATE)
    {
        RUDP_FATAL("setoption failed!");

        return -1;
    }

    int32_t ret = 0;
    switch(op_type)
    {
    case RUDP_KEEPLIVE:
        RUDP_INFO("keep live intnal = " << op_value << "ms, rudp socket id = " << rudp_id_);
        keeplive_intnal_ = op_value > DEFAULT_KEEPLIVE ? op_value : DEFAULT_KEEPLIVE;
        break;

    case RUDP_NAGLE:
        if(op_value == 0)
        {
            send_buffer_.set_nagle(false);
            RUDP_INFO("cancel nagle, rudp socket id = " << rudp_id_);
        }
        else
        {
            send_buffer_.set_nagle(true);
            RUDP_INFO("set nagle, rudp socket id = " << rudp_id_);
        }
        break;

    case RUDP_RECV_BUFF_SIZE:
        break;

    case RUDP_SEND_BUFF_SIZE:
        send_buffer_.set_buffer_size(op_value > MIN_SEND_BUFFER_SIZE ? op_value : MIN_SEND_BUFFER_SIZE);
        RUDP_INFO("set send buffer, buffer size = " << op_value);
        break;

    case RUDP_TIMEOUT_COUNT:
        timeout_count_ = op_value > 2 ? op_value : 2;
        RUDP_INFO("set rudp timeout delay = " << timeout_count_ * 6 << "s");
        break;

    default:
        ret = -1;
    }

    return ret;
}
示例#16
0
int32_t RUDPSocket::send(const uint8_t* data, int32_t data_size)
{
    if(state_ != RUDP_CONNECTED || data_size <= 0)
    {
        RUDP_FATAL("send failed! state_ != RUDP_CONNECTED or data size = 0, rudp id = " << rudp_id_);
        error_code_ = RUDP_SEND_ERROR;
        return -1;
    }

    int32_t ret = send_buffer_.send(data, data_size);
    if(ret <= 0)
    {
        RUDP_INFO("send buffer is full, rudp socket id = " << rudp_id_);
        error_code_ = RUDP_SEND_EAGIN;
    }

    return ret;
}
示例#17
0
int32_t RUDPObject::get_local_addr(int32_t rudp_id, Inet_Addr& addr)
{
	RUDPSocket* rudp_session = get_socket(rudp_id);
	if(rudp_session == NULL)
	{
		RUDP_FATAL("rudp_session is NULL, rudp socket id = " << rudp_id);
		return -1;
	}

	uint8_t index = rudp_session->get_local_index();
	if(adapter_array_[index] != NULL)
	{
		addr = adapter_array_[index]->get_local_addr();
		return 0;
	}
	else
		return -1;
}
示例#18
0
int32_t RUDPSocket::connect(const Inet_Addr& remote_addr)
{
    if(rudp_id_ == INVALID_RUDP_HANDLE || state_ != RUDP_IDLE 
        || local_index_ == 255 || remote_rudp_id_ != INVALID_RUDP_HANDLE)
    {
        RUDP_FATAL("rudp connect failed! rudp socket id = " << rudp_id_);
        error_code_ = RUDP_CONNECT_FAIL;
        return -1;
    }

    remote_addr_ = remote_addr;
    check_sum_ = rand() % 65536; //产生一个会话的CHECK SUM

    RUDP_INFO("state = RUDP_CONNECTING, rudp id = " << rudp_id_ << ", remote addr = " << remote_addr_);
    set_state(RUDP_CONNECTING);

    RUDP_DEBUG("send syn, rudp socket id = " << rudp_id_);
    send_syn();
    ccc_.init(send_buffer_.get_buffer_seq() - 1);
    set_timer(CONNECT_DELAY);
    send_count_ ++;

    return 0;
}
示例#19
0
void RUDPObject::process(IRUDPAdapter* adapter, BinStream& strm, const Inet_Addr& remote_addr)
{
	uint8_t index = INVALID_ADAPTER_INDEX; 
	if(adapter != NULL)
		index = adapter->get_index();

	if(adapter_array_[index] == NULL)
	{
		RUDP_FATAL("adapter is not attach!!!");
		return ;
	}

	PARSE_RUDP_HEAD(rudp_head, "parse rudp head failed!!");

	RUDPSocket *rudp_session = get_socket(rudp_head.remote_rudp_id_);
	if(rudp_session != NULL)
	{
		rudp_session->process(rudp_head.msg_id_, rudp_head.check_sum_, strm, remote_addr);
		return ;
	}

	//处理SYN连接请求
	if(rudp_head.msg_id_ == RUDP_SYN && rudp_head.remote_rudp_id_ == INVALID_RUDP_HANDLE)
	{
		PARSE_RUDP_MESSAGE(strm, RUDPSynPacket, syn, "parse syn failed!");

		//在半开连接中找
		rudp_session = find_by_peer_id(syn.local_rudp_id_, remote_addr);
		if(rudp_session == NULL)
		{
			int32_t new_rudp_id = create_socket();
			if(new_rudp_id == INVALID_RUDP_HANDLE)
			{
				send_syn2(rudp_head.remote_rudp_id_, SYSTEM_SYN_ERROR, rudp_head.check_sum_, remote_addr, adapter);
				RUDP_INFO("send syn2 to " << remote_addr);
				return ;
			}

			if(this->bind(new_rudp_id, adapter->get_local_addr()) != 0)
			{
				free_sockets(new_rudp_id);

				send_syn2(rudp_head.remote_rudp_id_, SYSTEM_SYN_ERROR, rudp_head.check_sum_, remote_addr, adapter);
				RUDP_INFO("send syn2 to " << remote_addr);
				return ;
			}

			//建立远程SOCKET ID与本地SOCKET ID的关系,防止重复建立连接
			RUDPPeerInfo info(syn.local_rudp_id_, remote_addr);
			peer_socket_ids_[info] = new_rudp_id;

			rudp_session = get_socket(new_rudp_id);
			rudp_session->set_check_sum(rudp_head.check_sum_);

			//提示上层EVENT HANDLE进行ACCEPT,可以在上层进行SOCKET属性设置
			if(listener_ != NULL)
				listener_->rudp_accept_event(new_rudp_id);
		}

		//进行消息处理
		rudp_session->process_syn(syn, remote_addr);
	}
	else
	{
		RUDP_WARNING("error msg, msg id =  " << (uint16_t)rudp_head.msg_id_ << ", rudp id = " << rudp_head.remote_rudp_id_ << ", remote addr = "<< remote_addr);
	}
}