⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 socket_control_protocol_handler.cpp

📁 程序实现了在ip协议之上加一层RUDP协议
💻 CPP
字号:
// SOCKET_Control_Protocol_Handler.cpp: implementation of the SOCKET_Control_Protocol_Handler class.
//
//////////////////////////////////////////////////////////////////////

#include "SOCKET_Control_Protocol_Handler.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

SOCKET_Control_Protocol_Handler* SOCKET_Control_Protocol_Handler::pinstance = NULL;


/*
	initial, make sure the relate module get the single object
*/
SOCKET_Control_Protocol_Handler::SOCKET_Control_Protocol_Handler()
{
	retransmit_timing2_ptr=SOCKET_ReTransmit_Timing2_Protocol_Handler::get_instance();
	create_message_protocol_handler_ptr=Socket_Create_Message_Protocol_Handler::get_instance();
	rudp_enunpacket_manager_ptr=RUDP_EnUnPacket_Handler::get_instance();
	activity_list_manager_ptr=SOCKET_ACTIVITY_LIST_Handler::get_instance();
	socket_sr_ptr=Socket_SR::get_instance();
	sequence_manager_ptr=SOCKET_Sequence_Manager::get_instance();
	receive_data_buff_ptr=SOCKET_Receive_Data_Buff::get_instance();
}

SOCKET_Control_Protocol_Handler::~SOCKET_Control_Protocol_Handler()
{
	
}


/*
  factory pattern, make sure there is only one object of this class
*/
SOCKET_Control_Protocol_Handler* SOCKET_Control_Protocol_Handler::get_instance ()
{
	if (pinstance == NULL)
	{
		pinstance = new SOCKET_Control_Protocol_Handler;
	}
	return pinstance;
}


/*
  get message from application layer or mdot layer, analyse the message and make
  different respondence according to the value of the message's Event field
*/
bool  SOCKET_Control_Protocol_Handler::f_SOCKET_Control_Message_Analyse_Handler(Message *msg)
{
	switch(msg->Event)
	{
		case App_Receive_Success: f_SOCKET_delete_data_buff_Packet(msg->SubMsg);
		break;
		case MDOT_Receive_Success: f_SOCKET_delete_data_buff_Packet(msg->SubMsg);
		break;
		// user login
		case User_Login: f_SOCKET_User_Login(1,msg->Data[0], msg->DataPtr,msg->Length);
		break;
	}
	return 1;
}


/*
  get RUDP from receiver buffer,  analyse it and make different 
  respondence according to the value of the message's Event field
*/
bool  SOCKET_Control_Protocol_Handler::f_SOCKET_Control_RUDP_Analyse_Handler(Data_Queue_STR* data_queue_str)
{
	RUDP* rudp;
	rudp=(RUDP*)data_queue_str->rudp_data;
	
//包类型1是ACK包,包类型2是登陆包,包类型3是登陆包的响应包
	switch(rudp->packet_type)
	{
		case '1': f_SOCKET_ACK_Handler(rudp->S_deviceID, rudp->packet_seq, data_queue_str->RUPDTag);
		break;
		// user login
		case '2': f_SOCKET_Check_User(rudp->S_deviceID,rudp->D_deviceID,data_queue_str->rudp_data+sizeof(RUDP));
		break;
		case '3': f_SOCKET_User_Login_Notify(rudp->S_deviceID,*(data_queue_str->rudp_data+sizeof(RUDP)));
		break;
	}
	return 1;
}


/*
  once sender rececive ack from receiver, delete the packet in the retranssimision list
*/
bool  SOCKET_Control_Protocol_Handler::f_SOCKET_ACK_Handler(unsigned int deviceID, unsigned int seq, unsigned int rudp_tag)
{
	Message msgtp,msg;
	cout<<"源设备号"<<deviceID<<endl;
	cout<<"ACK"<<seq<<"没来之前"<<retransmit_timing2_ptr->get_vector_length()<<endl;
	msgtp=retransmit_timing2_ptr->f_SOCKET_Timing2_find_item(deviceID,seq);
//产生消息通知上层发送成功
	msg=create_message_protocol_handler_ptr->f_SOCKET_App_Send_Success(&msgtp);
//删除重传队列相应的项
	retransmit_timing2_ptr->f_SOCKET_Timing2_Remove(deviceID,seq);
	cout<<"ACK"<<seq<<"来之后"<<retransmit_timing2_ptr->get_vector_length()<<endl;
	return 1;
}


/*
  once receiver receive the desired packet, send back ack to sender
*/
bool SOCKET_Control_Protocol_Handler::f_SOCKET_ACK_Send(unsigned int s_deviceID, unsigned int d_deviceID, unsigned int packet_seq)
{
	cout<<"服务端返回ACK包的序列号"<<packet_seq<<endl;
	RUDP rudp=rudp_enunpacket_manager_ptr->f_enRUDPPacket(s_deviceID, d_deviceID, '1', sizeof(RUDP)+0, packet_seq);
	Addr_Str addr_str = activity_list_manager_ptr->f_Find_ip_and_port_by_deviceID(d_deviceID);
//	socket_sr_ptr->create_des_address((char*)&addr_str.ip,addr_str.port);
	socket_sr_ptr->create_des_address("192.168.32.9",6666);
	socket_sr_ptr->app_rudp(rudp,NULL, 0);
	socket_sr_ptr->send();
	return 1;
}


/*
  user login from application
*/
bool SOCKET_Control_Protocol_Handler::f_SOCKET_User_Login(unsigned int s_deviceID, unsigned int d_deviceID, char* password, unsigned int pass_length)
{
	cout<<"User Login"<<endl;
	RUDP rudp=rudp_enunpacket_manager_ptr->f_enRUDPPacket(s_deviceID, d_deviceID, '2', sizeof(RUDP)+pass_length, 0);
	Addr_Str addr_str = activity_list_manager_ptr->f_Find_ip_and_port_by_deviceID(d_deviceID);
//	socket_sr_ptr->create_des_address((char*)&addr_str.ip,addr_str.port);
	socket_sr_ptr->create_des_address("192.168.32.9",6666);
	socket_sr_ptr->app_rudp(rudp,password, pass_length);
	socket_sr_ptr->send();
	return 1;
}


/*
  receier check the login user is legal or not
*/
bool SOCKET_Control_Protocol_Handler::f_SOCKET_Check_User(unsigned int s_deviceID,unsigned int d_deviceID,char* password)
{
	cout<<"this function is to check the user's legality"<<endl;
	cout<<"the password here is: "<<password<<endl;
	cout<<"the ID: "<<d_deviceID<<endl;
	char* get_password = activity_list_manager_ptr->f_Find_password_by_deviceID(d_deviceID);
	cout<<"get the passwword: "<<get_password<<endl;
	if(!strcmp(password,get_password))
	{
		f_SOCKET_User_Login_Response(d_deviceID,s_deviceID,'t');
		f_SOCKET_User_Login_Notify(s_deviceID,'t');
	}
	else
	{
	  f_SOCKET_User_Login_Response(d_deviceID,s_deviceID,'f');
	}
}


/*
  according to method f_SOCKET_Check_User, make different response to the user
*/
bool SOCKET_Control_Protocol_Handler::f_SOCKET_User_Login_Response(unsigned int s_deviceID, unsigned int d_deviceID,char login_result)
{
	RUDP rudp=rudp_enunpacket_manager_ptr->f_enRUDPPacket(s_deviceID, d_deviceID, '3', sizeof(RUDP)+ 1, 0);
	Addr_Str addr_str = activity_list_manager_ptr->f_Find_ip_and_port_by_deviceID(d_deviceID);
//	socket_sr_ptr->create_des_address((char*)&addr_str.ip,addr_str.port);
	socket_sr_ptr->create_des_address("192.168.32.9",6666);
	socket_sr_ptr->app_rudp(rudp,&login_result,1);
	socket_sr_ptr->send();
	return 1;
}


/*
  once sender receive response from receiver, create message to notify application
*/
bool SOCKET_Control_Protocol_Handler::f_SOCKET_User_Login_Notify(unsigned int d_deviceID, char login_result)
{
	Message msg;
	if(login_result=='t')
	{
		msg=Socket_Create_Message_Protocol_Handler::f_SOCKET_App_Connected_Notify(d_deviceID);
		cout<<"deviceID_true="<<d_deviceID<<endl;
		sequence_manager_ptr->add(d_deviceID);
	}
	else
	{
		msg=Socket_Create_Message_Protocol_Handler::f_SOCKET_App_Request_Conn_Failure(d_deviceID,(unsigned int)0);
		cout<<"false"<<endl;
	}
	return 1;
}


/*
  once receive receive message, delete the data buff, according to rudp tag
*/
void SOCKET_Control_Protocol_Handler::f_SOCKET_delete_data_buff_Packet(unsigned int rudp_tag)
{
	receive_data_buff_ptr->remove(rudp_tag);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -