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

📄 udpserver.c

📁 基于实时嵌入式系统的voip系统(real time embeded system)。主要难点在实时处理语音信号。语音信号基于其自身特点
💻 C
字号:
/**************************************************************************
/* udpServer.c - UDP Server
*
*  Author - Ed Hursey
*  Date - November 2005
*  Note - This file is based on the udp server example
*         from the VxWorks Netguide manual.
*  Description - This file contains the task for the udp server.
*  It simply spins waiting for requests.  When one is received
*  it gives a semaphore.  It also handles changing modes when a
*  change mode request is receivied.
**************************************************************************/
/* includes */
#include "vxWorks.h"
#include "sockLib.h"
#include "inetLib.h"
#include "stdioLib.h"
#include "strLib.h"
#include "ioLib.h"
#include "fioLib.h"
#include "voip.h"

/**************************************************************************
*   Function - udpServer
*   Parameters - none
*   Returns - Never, or ERROR if a resources could not be allocated.
*   Purpose - read from UDP socket. If audio message (type = 3) is
*      received, then gives a semaphore indicating message
*      was received.  Otherwise dispatches proper actions according to
*      other message types.
*      The message is put in the global receivedRequest object.
/**************************************************************************/
STATUS udpServer (void)
{
   struct sockaddr_in serverAddr; /* server's socket address */
   struct sockaddr_in clientAddr; /* client's socket address */
   int sockAddrSize; /* size of socket address structure */
   int sFd; /* socket file descriptor */
   char inetAddr[INET_ADDR_LEN];  /* buffer for client's inet addr */
   int recvResult = 0;

   /* set up the local address */
   sockAddrSize = sizeof (struct sockaddr_in);
   bzero ((char *) &serverAddr, sockAddrSize);
   serverAddr.sin_len = (u_char) sockAddrSize;
   serverAddr.sin_family = AF_INET;
   serverAddr.sin_port = htons (SERVER_PORT_NUM);
   serverAddr.sin_addr.s_addr = htonl (INADDR_ANY);

   /* create a UDP-based socket */
   if ((sFd = socket (AF_INET, SOCK_DGRAM, 0)) == ERROR)
   {
      perror ("socket");
      return (ERROR);
   }

   /* bind socket to local address */
   if (bind (sFd, (struct sockaddr *) &serverAddr, sockAddrSize) == ERROR)
   {
      perror ("bind");
      close (sFd);
      return (ERROR);
   }

   /* read data from a socket and satisfy requests */
   FOREVER
   {
		recvResult = recvfrom (sFd, (char *) &receivedRequest, sizeof (receivedRequest), 0,
         (struct sockaddr *) &clientAddr, &sockAddrSize);

      if (recvResult == ERROR)
      {
         perror ("recvfrom");
         close (sFd);
         return (ERROR);
      }

		/*if we got something*/
		if( recvResult > 0 )
      {
			/*request type 1, means we are switching modes
			  stop dac and switch modes but wait to start until other side ready*/
			if( receivedRequest.request_type == 1 )
         {
            localSwitchMode(receivedRequest.mode);
	 		}
	 		/*request type 2, means that other side is done switching modes, so we can resume voip*/
         else if( receivedRequest.request_type == 2)
         {
            resumeVOIP();
         }
         else  /*assume anything else is an audio message*/
         {
            semGive(SEM_RECEIVED);
         }
      }

   }
}

⌨️ 快捷键说明

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