示例#1
0
/**
 * This function will print out all data specific to the parserState provided
 * @param _vecToPrint  vector all all elements
 * @param _lineState   ParserState of line from XML File
 * @param _lineNum     current line's number
 * @param _strLine     current line
 * @param _lastElement The tag name of the last opening tag
 */
void printXMLData( vector<Element*>& _vecToPrint, ParserState& _lineState, 
        int _lineNum, string& _strLine, string _lastElement )
{
  //if ( == ELEMENT_OPENING_TAG)
  if (1 == 1)
  {
    // Remove whitespace from the line
    removeWhiteSpace(_strLine);

    // Print line number and content
    cout << _lineNum << ". " << _strLine << endl;
    cout << "---------------------------------------------------------------------" << endl;

    // Print line parser state
    ShowState(_lineState);

    // Handle parser state specific actions
    if (_lineState == ELEMENT_OPENING_TAG)
    {    
      cout << "-- Adding '" << _lastElement << "' to Vector (Stack)" << endl;
      printVector(_vecToPrint);
    }
    else if (_lineState == ELEMENT_CLOSING_TAG)
    {
      cout << "-- Removing '" << _lastElement << "' from Vector (Stack)" << endl;
      printVector(_vecToPrint);
    }
    else if (_lineState == ELEMENT_NAME_AND_CONTENT || _lineState == SELF_CLOSING_TAG)
    {
      if (_lineState == ELEMENT_NAME_AND_CONTENT)
      {
        cout << "--   TAG NAME: '" << _lastElement << "'" << endl;
        cout << "--   TAG CONTENT: '" << getTagContent(_strLine) << "'" << endl;
      }

      cout << "-- Vector (Stack) unchanged" << endl;
    }
  }
}
示例#2
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)
示例#3
0
文件: proxy.cpp 项目: bobalm/udpproxy
						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)
					{
						string clientAddr = getTagContent(sMessage,"<Addr>","</Addr>");
						int port = atoi(getTagContent(sMessage,"<RTPPort>","</RTPPort>").c_str());
						
						gOsLog<<"listenRtpClientInited."<<endl;
						listenRtpClientInited = 1;
						listenRtpCliAddr.sin_family = AF_INET;     // host byte order
						listenRtpCliAddr.sin_port = htons(port);
						listenRtpCliAddr.sin_addr.s_addr = inet_addr(clientAddr.c_str()); // short, network byte order
						memset(&(listenRtpCliAddr.sin_zero), '\0', 8); // zero the rest of the struct

						listenRtcpClientInited = 1;
						listenRtcpCliAddr.sin_family = AF_INET;     // host byte order
						listenRtcpCliAddr.sin_port =  htons(port+1);
						listenRtcpCliAddr.sin_addr.s_addr = inet_addr(clientAddr.c_str()); // short, network byte order
						memset(&(listenRtcpCliAddr.sin_zero), '\0', 8); // zero the rest of the struct
					}
示例#4
0
std::string MusicPlaylist::getText()
{
	return getTagContent();
}
示例#5
0
int ProxySock::receiveData()
{
  int result = 0;
  if (m_fd < 1)
  {
    gOsLog<<"m_fd="<<m_fd<<endl;
    m_fd = 0;
    return RC_CLOSE_SOCKET; 
  }

  unsigned char tcpBuff;
  int nread = 0;
  nread = recv(m_fd, &tcpBuff, 1, 0);
  // If error or eof, terminate. 
  if(nread < 0)
  {
    gOsLog<<"m_fd="<<m_fd<<",name="<<m_name<<endl;
    gOsLog<<"recv m_fd error:"<<strerror(errno)<<endl;
    m_isDeleteAble = true;
    return RC_CLOSE_SOCKET;
  }
  else if (0 == nread)
  {
    gOsLog<<"nread=0!,m_fd="<<m_fd<<endl;
    m_isDeleteAble = true;
    return RC_CLOSE_SOCKET;
  }
  
  //gOsLog<<"tcpBuff="<<tcpBuff<<endl;
  if (m_isReadHead)
  {
    if (0 == m_readStep && 3 == tcpBuff)
    {
      m_readStep ++;
    }
    else if (1 == m_readStep && 0 == tcpBuff)
    {
      m_readStep++;
    }
    else if (2 == m_readStep )
    {
      m_readStep ++;
      m_lengHigh = tcpBuff;
      //m_readMsgLen = tcpBuff<<8;
      //gOsLog<<"readStep=2,m_lengHigh="<<(int)m_lengHigh<<endl;
    }
    else if (3 == m_readStep )
    {
      m_readStep =0;
      m_lengLow = tcpBuff;
      m_readBuffLen = (m_lengHigh << 8) + m_lengLow ;
      m_recvedMsgLen = m_readBuffLen;
      m_readMsgLen = m_readBuffLen - 4;//len include 03,00,LH,LL
      m_isReadHead = false;
      //gOsLog<<"readStep=3,m_lengLow="<<(int)m_lengLow<<",readMsgLen="<<m_readMsgLen<<endl;
      if (1 > m_readMsgLen || 99999< m_readMsgLen)
      {
        m_readStep = 0;
        m_readMsgLen = 0;
        m_readBuffLen = 0;
        m_recvedMsgLen = 0;
        m_isReadHead = true;
        return result;
      }
      
      if (0 == m_readBuff)
      {
        m_readBuff = new unsigned char[m_readMsgLen+8];
        m_readBuffLen = m_readMsgLen+8;
      }
      else if (m_readBuffLen < (m_readMsgLen+8))
      {
        delete[] m_readBuff;
        m_readBuff = new unsigned char[m_readMsgLen+8];
        m_readBuffLen = m_readMsgLen+8;
        
      }
      m_pReadBuffTmp = m_readBuff;
      *m_pReadBuffTmp++ = 0x03;
      *m_pReadBuffTmp++ = 0x00;
      *m_pReadBuffTmp++ = m_lengHigh;
      *m_pReadBuffTmp++ = m_lengLow;

      //gOsLog<<"readStep=3,m_lengLow="<<m_lengLow<<",readMsgLen="<<m_readMsgLen<<endl;
    }
    else
    {
      m_readStep = 0;
      m_readMsgLen = 0;
      m_readBuffLen = 0;
      m_recvedMsgLen = 0;
      m_isReadHead = true;
      //no use data read
    }
  }
  else if (m_readMsgLen > 0)
  {
    *m_pReadBuffTmp++ = tcpBuff;
    m_readMsgLen --;
    if (0 == m_readMsgLen)
    {
      //read a packge
      m_readStep = 0;
      m_isReadHead = true;
      printSysTime();
      gOsLog<<"read data from: "<<m_name<<endl;     
      if (false == m_isProxySock)
      {
        //this is listened signal socket
        if (0 == m_pairSock)
        {
          onSetUp();
          if (0 == m_pairSock)
          {
            gOsLog<<"m_pairSock is null,close connected socket."<<endl;
            m_isDeleteAble = true;
            return RC_CLOSE_SOCKET;
          }
        }
        if (RC_CLOSE_SOCKET == m_pairSock->sendData(m_readBuff,m_recvedMsgLen))
        {
          result = RC_CLOSE_SOCKET;
        }
        return result;
      }

      //this is proxy socket
      if ('<' == *(m_readBuff+4) && 'M' == *(m_readBuff+5))
      {
        //this is command   
        if ('\0' != *(m_pReadBuffTmp-1))
        {
          *m_pReadBuffTmp = '\0';
        }
        string sMessage = (const char*)(m_readBuff+4);
        gOsLog<<"read message from "<<m_name<<",get="<<sMessage<<endl;
        string sCommand = getTagContent(sMessage,"<Command>","</Command>");
        if ("Close" == sCommand)
        {
          m_isDeleteAble = true;
          return RC_CLOSE_SOCKET;
        }
        else if ("Start" == sCommand)
        {
          string uid =  getTagContent(sMessage,"<UID>","</UID>");
          ProxySock* pProxySock = 0;
          pProxySock = findProxySockByUid(uid);
          if (pProxySock)
          {
            rmFdList.push_back(pProxySock->getFd());
          }
          m_uid = uid;
        }//end of start
        else
        {
          gOsLog<<"unknow command"<<endl;             
        }
      }//end of command 
      else
      {
        //this is signal
        if (0 == m_pairSock)
        {
          gOsLog<<m_name<<" m_pairSock is null,drop the packge."<<endl;
          return result;
        }
        
        if (RC_CLOSE_SOCKET == m_pairSock->sendData(m_readBuff,m_recvedMsgLen))
        {
          result = RC_CLOSE_SOCKET;
        }
        return result;
      }//end of proxy signal
    }//end of process msg
  }
  else
  {
    m_readStep = 0;
    m_readMsgLen = 0;
    m_readBuffLen = 0;
    m_recvedMsgLen = 0;
    m_isReadHead = true;
  }
  return result;
}