socket长连接,断线重连案例

void SendMsgToStreamSer(void* pData)
{
//#ifndef WIN32
	signal(SIGPIPE,SIG_IGN);
	VT_WEBSOCKET_SERVICE::CWebSocketVideoMsgProc* pSer = (VT_WEBSOCKET_SERVICE::CWebSocketVideoMsgProc*)pData;
	CSock client;
	while (0>client.Connect("127.0.0.1",9100))
	{
		IVE_Logger_AddLog(LOGLEVEL_INFO,"WebSocketVideoMsg.log","SendMsgToStreamSer","与推流服务建立连接失败");
		scSleep(1000);
	}
	IVE_Logger_AddLog(LOGLEVEL_INFO,"WebSocketVideoMsg.log","SendMsgToStreamSer","与推流服务建立连接成功");
	unsigned char Header[3];
	int len = sizeof(PushServerMsg);
	Header[0] = '$';
	Header[1] = (unsigned char)((len & 0xFF00) >> 8);
	Header[2] = (unsigned char)(len & 0xFF);
	while(true)
	{
		int retStat = -1;
		PushServerMsg msg;
		if (pSer->m_queue.size()==0)
		{
			scSleep(1000);
			continue;
		}
		scLockWMutex(pSer->m_queueRWMutex);
		msg = pSer->m_queue.front();
		pSer->m_queue.pop();
		scUnlockRWMutex(pSer->m_queueRWMutex);
		retStat = client.Send((char*)&Header,3);
		if (retStat < 0)
		{
			if (client.Connect("127.0.0.1",9100) == 0)
			{
				IVE_Logger_AddLog(LOGLEVEL_INFO,"WebSocketVideoMsg.log","SendMsgToStreamSer","与推流服务重连成功,%d",__LINE__);
				scSleep(100);
				int ret=client.Send((char*)&Header,3);
				if(ret < 0)
				{
					IVE_Logger_AddLog(LOGLEVEL_INFO,"WebSocketVideoMsg.log","SendMsgToStreamSer","给推流服务重发失败");
				}
			}
			else
			{
				while(0>client.Connect("127.0.0.1",9100))
				{
					IVE_Logger_AddLog(LOGLEVEL_INFO,"WebSocketVideoMsg.log","SendMsgToStreamSer","与推流服务连接断开,尝试重连");
					scSleep(1000);
				}
				IVE_Logger_AddLog(LOGLEVEL_INFO,"WebSocketVideoMsg.log","SendMsgToStreamSer","与推流服务重连成功,%d",__LINE__);
				continue;
			}
			
		}
		retStat = client.Send((char*)&msg,len);
		if (retStat< 0)
		{
			if (client.Connect("127.0.0.1",9100) == 0)
			{
				IVE_Logger_AddLog(LOGLEVEL_INFO,"WebSocketVideoMsg.log","SendMsgToStreamSer","与推流服务重连成功,%d",__LINE__);
				scSleep(100);
				int ret=client.Send((char*)&Header,3);
				if(ret < 0)
				{
					IVE_Logger_AddLog(LOGLEVEL_INFO,"WebSocketVideoMsg.log","SendMsgToStreamSer","给推流服务重发失败");
				}
				ret=client.Send((char*)&msg,len);
				if(ret < 0)
				{
					IVE_Logger_AddLog(LOGLEVEL_INFO,"WebSocketVideoMsg.log","SendMsgToStreamSer","给推流服务重发失败");
				}
				else
				{
					if(msg.MsgType == MSG_PUSH_RTSP_STREAM||msg.MsgType ==MSG_PUSH_SIP_STREAM)
					{
						syslog_info("VIDEO_SESSION:request push video taskId:%s pushServerUrl:%s ReqRtspUrl:%s\n",msg.StreamId,msg.pushServerUrl,msg.ReqRtspUrl);
						IVE_Logger_AddLog(LOGLEVEL_INFO,"WebSocketVideoMsg.log","SendMsgToStreamSer","VIDEO_SESSION:request push video taskId:%s pushServerUrl:%s ReqRtspUrl:%s",msg.StreamId,msg.pushServerUrl,msg.ReqRtspUrl);
					}
					else
					{
						syslog_info("VIDEO_SESSION:request stop video taskId:%s pushServerUrl:%s ReqRtspUrl:%s\n",msg.StreamId,msg.pushServerUrl,msg.ReqRtspUrl);
						IVE_Logger_AddLog(LOGLEVEL_INFO,"WebSocketVideoMsg.log","SendMsgToStreamSer","VIDEO_SESSION:request stop video taskId:%s pushServerUrl:%s ReqRtspUrl:%s\n",msg.StreamId,msg.pushServerUrl,msg.ReqRtspUrl);
					}
				}
				
			}
			else
			{
				while(0>client.Connect("127.0.0.1",9100))
				{
					IVE_Logger_AddLog(LOGLEVEL_INFO,"WebSocketVideoMsg.log","SendMsgToStreamSer","与推流服务连接断开,尝试重连");
					scSleep(1000);
				}
				IVE_Logger_AddLog(LOGLEVEL_INFO,"WebSocketVideoMsg.log","SendMsgToStreamSer","与推流服务重连成功,%d",__LINE__);
				continue;
			}	
		}
		else
		{
			if(msg.MsgType == MSG_PUSH_RTSP_STREAM||msg.MsgType ==MSG_PUSH_SIP_STREAM)
			{
				syslog_info("VIDEO_SESSION:request push video taskId:%s pushServerUrl:%s ReqRtspUrl:%s\n",msg.StreamId,msg.pushServerUrl,msg.ReqRtspUrl);
				IVE_Logger_AddLog(LOGLEVEL_INFO,"WebSocketVideoMsg.log","SendMsgToStreamSer","VIDEO_SESSION:request push video taskId:%s pushServerUrl:%s ReqRtspUrl:%s",msg.StreamId,msg.pushServerUrl,msg.ReqRtspUrl);
			}
			else
			{
				syslog_info("VIDEO_SESSION:request stop video taskId:%s pushServerUrl:%s ReqRtspUrl:%s\n",msg.StreamId,msg.pushServerUrl,msg.ReqRtspUrl);
				IVE_Logger_AddLog(LOGLEVEL_INFO,"WebSocketVideoMsg.log","SendMsgToStreamSer","VIDEO_SESSION:request stop video taskId:%s pushServerUrl:%s ReqRtspUrl:%s\n",msg.StreamId,msg.pushServerUrl,msg.ReqRtspUrl);
			}
		}	
	}
	client.Close();
//#endif
	return ;
}
欢迎关注问我团队公众号:

在这里插入图片描述

你可能感兴趣的:(socket长连接,断线重连案例)