コード例 #1
0
ファイル: proxy.cpp プロジェクト: bobalm/udpproxy
void proxySocketThread(int argument)
{

	string sMessage;
	unsigned char* recvBuff = 0;
	int proxyFd = 0;
	int proxyRtpFd = 0;
	int proxyRtcpFd = 0;
	int proxyRtpPort = 0;
	int listenRtpFd = 0;
	int listenRtcpFd = 0;
	int listenRtpPort = 0;
	fd_set fds;	// set of file descriptors to check.
	int nread = 0;	// return from read() 
	int nready = 0;	// # fd's ready. 
	int maxfd = 0;	// fd's 0 to maxfd-1 checked.
	int proxyRtpClientInited = 0;
	int listenRtpClientInited = 0;
	int proxyRtcpClientInited = 0;
	int listenRtcpClientInited = 0;
	int result = 0;
	int sock_len = sizeof(struct sockaddr);
	int readMsgLen = 0;
	int readStep = 0;
	bool readHead = true;
	
	struct sockaddr_in clientAddr;
	struct sockaddr_in proxyRtpCliAddr;
	struct sockaddr_in listenRtpCliAddr;
	struct sockaddr_in proxyRtcpCliAddr;
	struct sockaddr_in listenRtcpCliAddr;

	proxyFd = argument;
	if (0 == proxyFd)
	{
		gOsLog<<"argument is null ,exit from the thread!"<<endl;
		pthread_exit(0); // Notice the cast
	}

	for(int i=0;i<10;i++)
	{
		proxyRtpPort = RTPPortRange.getRTPPort();
		listenRtpPort = RTPPortRange.getRTPPort();

		proxyRtpFd = UDPListen(proxyRtpPort);
		if (1 > proxyRtpFd)
		{
			proxyRtpFd = 0;
			continue;
		}
		proxyRtcpFd = UDPListen(proxyRtpPort+1);

		if (1 > proxyRtcpFd)
		{
			close(proxyRtpFd);
			proxyRtcpFd = 0;
			proxyRtpFd = 0;
			continue;
		}

		listenRtpFd = UDPListen(listenRtpPort);
		if (1 > listenRtpFd)
		{
			close(proxyRtpFd);
			close(proxyRtcpFd);
			proxyRtcpFd = 0;
			proxyRtpFd = 0;
			listenRtpFd = 0;
			continue;
		}

		listenRtcpFd = UDPListen(listenRtpPort+1);
		if (1 > listenRtcpFd)
		{
			close(proxyRtpFd);
			close(proxyRtcpFd);
			close(listenRtpFd);
			proxyRtcpFd = 0;
			proxyRtpFd = 0;
			listenRtpFd = 0;
			listenRtcpFd = 0;
			continue;
		}
		break;
	}

	if (proxyFd < 1 || proxyRtpFd < 1 || proxyRtcpFd < 1
		|| listenRtpFd < 1|| listenRtcpFd < 1 )
	{
		if (proxyRtpFd > 0)
		{
			close(proxyRtpFd);
		}
		if (proxyRtcpFd > 0)
		{
			close(proxyRtcpFd);
		}
		if (listenRtpFd > 0)
		{
			close(listenRtpFd);
		}
		if (listenRtcpFd > 0)
		{
			close(listenRtcpFd);
		}
		close(proxyFd);
		gOsLog<<"can't get 4 udp port.exit thread."<<endl;
		pthread_exit(0); // Notice the cast
	}

	maxfd = listenRtcpFd;
	if (listenRtpFd > maxfd)
	{
		maxfd = listenRtpFd;
	}
	if (proxyRtcpFd > maxfd)
	{
		maxfd = proxyRtcpFd;
	}
	if (proxyRtpFd > maxfd)
	{
		maxfd = proxyRtpFd;
	}
	if (proxyFd > maxfd)
	{
		maxfd = proxyFd;
	}
	
	gOsLog<<"listen UDP on proxyRtp="<<proxyRtpPort<<endl;

	recvBuff = new unsigned char [MAXLINE];
	maxfd ++;
	while (!bSysStop)
	{
		// Set up polling.
		FD_ZERO(&fds);
		FD_SET(proxyFd,&fds);
		FD_SET(proxyRtpFd,&fds);
		FD_SET(proxyRtcpFd,&fds);
		FD_SET(listenRtpFd,&fds);
		FD_SET(listenRtcpFd,&fds);

		struct timeval tv;
		/* Wait up to 30*60 seconds. */
		tv.tv_sec = 1800;
		tv.tv_usec = 0;

		//* Wait for some input.
		nready = select(maxfd, &fds, (fd_set *) 0, (fd_set *) 0, &tv);
		//nready = select(maxfd, &fds, (fd_set *) 0, (fd_set *) 0,NULL);
		switch (nready) 
		{  
		case -1:  
			//error
			gOsLog<<"select  error:"<<strerror(errno)<<endl;
		        break;
		case 0:  
			//timeout 
			gOsLog<<"select timeout, error:"<<strerror(errno)<<endl;
		        break;
		default:  
			break;
		}  
		
		if( FD_ISSET(proxyFd, &fds))
		{
			
			char tcpBuff;
			nread = recv(proxyFd, &tcpBuff, 1, 0);
			// If error or eof, terminate. 
			if(nread < 1)
			{
				gOsLog<<"recv proxyFd error:"<<strerror(errno)<<endl;
				break;
			}

			if (readHead)
			{
				if (0 == readStep && 3 == tcpBuff)
				{
					readStep ++;
				}
				else if (1 == readStep && 0 == tcpBuff)
				{
					readStep++;
				}
				else if (2 == readStep )
				{
					readStep ++;
					readMsgLen = tcpBuff<<8;
					//gOsLog<<"readStep=2,tcpBuff="<<tcpBuff<<",readMsgLen="<<readMsgLen<<endl;
				}
				else if (3 == readStep )
				{
					readStep =0;
					readMsgLen += tcpBuff;
					readMsgLen -=4;//len include 03,00,LH,LL
					readHead = false;
					sMessage = "";
					//gOsLog<<"readStep=3,tcpBuff="<<tcpBuff<<",readMsgLen="<<readMsgLen<<endl;
				}
				else
				{
					readStep = 0;
					readMsgLen = 0;
					readHead = true;
					//no use data read
				}
			}
			else if (readMsgLen > 0)
			{
				sMessage += tcpBuff;
				readMsgLen --;
				if (0 == readMsgLen)
				{
					readMsgLen = 0;
					readHead = true;
					//string sMessage = string((const char*)recvBuff);
					printSysTime();
					gOsLog<<"proxyFd="<<proxyFd<<",read message: "<<sMessage<<endl;
					string sCommand = getTagContent(sMessage,"<Command>","</Command>");
					if ("Close" == sCommand)
					{
						break;
					}
					else if ("Start" == sCommand)
					{
						int len = 0;
						//<Message><ProxyRTP>RTPPort</ProxyRTP><ListenRTP>RTPPort</ListenRTP></Message>
						sMessage = "<Message><ProxyRTP>";
						sMessage += int2String(proxyRtpPort);
						sMessage +="</ProxyRTP><ListenRTP>";
						sMessage += int2String(listenRtpPort);
						sMessage +="</ListenRTP></Message>";
						//3,0,length(H,L),Message
						len = sMessage.length()+4;
	
						//string msg;
						unsigned char sendbuf[1024];
						sendbuf[0]=3;
						sendbuf[1]=0;
						sendbuf[2]=((len >> 8) & 0x0ff);
						sendbuf[3]=(len & 0x0ff);
						memcpy(sendbuf+4, sMessage.data(),len-4);
						sendbuf[len] ='\0';
						len = send(proxyFd, sendbuf, len+1, 0);
						if (0 > len)
						{
						    bSysStop = true;
						    gOsLog<<"error send msg,error:"<<strerror(errno)<<endl;    
						    break;
						}
					}//end of start
					else if (!listenRtpClientInited && "SetClientAddr" == sCommand)
コード例 #2
0
ファイル: networking.cpp プロジェクト: DU-RoboCup/NAO-engine
bool Networking::RunFrame(){
    UDPListen();
    return true;
}