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

📄 nms.c

📁 基于vxworks操作系统的电话语音平台系统
💻 C
📖 第 1 页 / 共 3 页
字号:

/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      SendCommand                                               	 	 */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/* 		This Procedure send the command from high layer applications     */
/*      and send it to the other side through the pipe.					 */
/*		peer host														 */
/*																		 */
/* RETURN															     */
/*		status .		 												 */
/*																		 */
/*************************************************************************/ 

STATUS SendCommand(INT channel, CHAR *buffer, INT length, INT timeout)
{
	STATUS 		status ;
	SOCKET_TASK	*current_task ;
	
	for (current_task = Command_Task_List.head ; current_task != NU_NULL;)
	{
		if ((channel >= 0) & (current_task->socketd == channel))
			break ;
		current_task = current_task->next ;
	}
	
	if (current_task == NU_NULL)
		return MSG_NO_MATCH_TASK ;
    
    if (timeout == 0)   
		status = NU_Send_To_Pipe(current_task->sendpipe,buffer,length, NU_SUSPEND);	
	else
		status = NU_Send_To_Pipe(current_task->sendpipe,buffer,length, timeout);	

	if (status != NU_SUCCESS)
		return MSG_SEND_COMMAND_ERR ;
	
	return NU_SUCCESS ;
}		

/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      RecvCommand                                               	 	 */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/* 		This Procedure recv the command from the other end,              */
/*      through the recvpipe Pipe.										 */
/*																		 */
/* RETURN															     */
/*		status .		 												 */
/*																		 */
/*************************************************************************/ 	

STATUS RecvCommand(INT channel, CHAR *buffer, INT length, INT *actual_length, INT timeout)
{
	STATUS			status ;
	SOCKET_TASK		*current_task ;
	UINT32			bytesrecv ;
	
	for (current_task = Command_Task_List.head ; current_task != NU_NULL;)
	{
		if ((channel > 0) & (current_task->socketd == channel))
			break ;
		current_task = current_task->next ;
	}
	
	if (current_task == NU_NULL)
		return MSG_NO_MATCH_TASK ;
	
    if (timeout == 0)   
		status = NU_Receive_From_Pipe(current_task->recvpipe, buffer,
											length,&bytesrecv, NU_SUSPEND);	
	else
		status = NU_Receive_From_Pipe(current_task->recvpipe,buffer,
											length,&bytesrecv, timeout);	

	if (status != NU_SUCCESS)
		return MSG_RECV_COMMAND_ERR ;
	
	*actual_length  = bytesrecv  ;
	
	return NU_SUCCESS ;
} 

/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      CloseAllCommand                                               	 	 */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/* 		This Procedure Close the channel that high layer apps use.       */
/*																		 */
/* RETURN															     */
/*		status .		 												 */
/*																		 */
/*************************************************************************/ 
STATUS CloseAllCommand(INT listenchannel)
{
  if ((NU_Close_Socket(listenchannel)) != NU_SUCCESS)
  {
     BYPrintf("\nError from NU_Close_Socket.");
     return MSG_CHANNEL_CLOSE_ERR;
  }
  Listen_Daemon_List_Delete(Command_Listen_Daemon_List.head, listenchannel) ;
}


/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      InitClientConnect                                          		 */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/* 		This Procedure will init the command communicatioin will		 */
/*		Will needed .													 */
/*																		 */
/*************************************************************************/
INT InitClientConnect(CHAR  *cli_ip_addr, CHAR *subnet, CHAR *serv_ip_addr, INT port) 
{
    INT                 socketd;
	struct addr_struct  servaddr;
    VOID                *pointer;
    STATUS              status;
    

	/* first init ethernet */
	EthernetInit(cli_ip_addr, subnet, 0) ;
    
    /* open a connection via the socket interface */
    if ((socketd = NU_Socket(NU_FAMILY_IP, NU_TYPE_STREAM, 0)) >=0 )
    {
       	/* fill in a structure with the server address */
       	servaddr.family    = NU_FAMILY_IP;
       	servaddr.port      = port;
       	memcpy(&(servaddr.id), serv_ip_addr, 4);

        if (( status = NU_Connect (socketd, (struct addr_struct *)&servaddr, 0)) >=0 )
      	{
         	/* Set the push bit for faster transmission. */
          	/*NU_Push (socketd);*/
			return socketd ;
        }
		else
	 		return status ;    
	}
	else
		return MSG_SOCKET_ERR ;
}



/*********************************************************************/
/* channel functions 												  */
/* channel_send														  */
/* channel_recv														  */
/* channel_close													  */
/* channel_connect													  */
/* channel_listen													  */
/* channel_open_as_server											  */
/**********************************************************************/	

INT	channel_send(int socketWhich, char *buffer, int len, int flags) 
{
	int trynum ;
	int nCount ;

	for(trynum = 0 ; trynum <5; trynum++)
	{
		/* turn on the "block during a read" flag */
       	/*NU_Fcntl(socketWhich, NU_SETFLAG, NU_BLOCK);*/
    	nCount = NU_Send(socketWhich, (CHAR *)buffer, len, 0);
   		if (nCount == 0 ) 
   		{
   			NU_Sleep(3) ;
			continue ;
		}
		else
			break ;
	}
	
	return nCount ;
}

INT	channel_recv(int socketWhich, char *buffer, int len, int flags)
{
	int trynum ;
	int nCount ;

	/* if in 10 seconds , can't receive any reply, then exit*/
	for(trynum = 0 ; trynum <100; trynum++)
	{
		/* turn off the "block during a read" flag */
       	NU_Fcntl(socketWhich, NU_SETFLAG, NU_FALSE);
		nCount = NU_Recv(socketWhich, buffer, len, 0);
   		if (nCount == 0 ) 
   		{
   			NU_Sleep(3) ;
			continue ;
		}
		else
			break ;
	}
	return nCount ;
}

INT	channel_close(int socket) 
{
	NU_Close_Socket(socket) ;
}

INT channel_connect(char *cli_ip_addr, char *subnet, char *serv_ip_addr, int port)
{
	INT                 socketd;
	struct addr_struct  servaddr;
	STATUS              status;


	/* first init ethernet */
	EthernetInit(cli_ip_addr, subnet, 0) ;

	/* open a connection via the socket interface */
	if ((socketd = NU_Socket(NU_FAMILY_IP, NU_TYPE_STREAM, 0)) >=0 )
	{
		/* fill in a structure with the server address */
		servaddr.family    = NU_FAMILY_IP;
		servaddr.port      = port;
		memcpy(&(servaddr.id), serv_ip_addr, 4);

		if (( status = NU_Connect(socketd, (struct addr_struct *)&servaddr, 0)) >=0 )
		{
			/* Set the push bit for faster transmission. */
          	/*NU_Push (socketd);*/
           	return socketd ;
        }
		else
		{
			if (socketd >= 0)
				channel_close(socketd) ;
	 		return status ;    
	 	}
	}
	else
		return socketd ;
}

INT channel_listen(int listenchannel, char *peer_addr)
{
	int		newsock ;
   	int		status;
	UINT32	*other_addr ;
	struct addr_struct  client_addr;
    
    newsock = NU_Accept(listenchannel, &client_addr, 0);
    if (newsock < 0)
     	return newsock ;
     
    if (peer_addr != NU_NULL)
    {
     	other_addr = (UINT32 *)&client_addr.id ;

		if ((UINT32)(*peer_addr) != (UINT32)(*other_addr))
     		return -1 ;
	}
    
    return newsock ;
}


INT channel_open_as_server(char *serv_ip_addr, char *subnet, int port, int maxconnect)
{
	INT                 socketd;
	struct addr_struct  servaddr;
	STATUS              status;

	/* first init ethernet */
	EthernetInit(serv_ip_addr, subnet, 1) ;

	/* creat a socket */
	if ((socketd = NU_Socket(NU_FAMILY_IP, NU_TYPE_STREAM, 0)) < 0 )
		return -1 ;

	/* fill in a structure with the server address */
	servaddr.family    = NU_FAMILY_IP;
	servaddr.port      = port;
	memcpy(&servaddr.id, serv_ip_addr, 4);

	/* make an NU_Bind() call to bind the server's address */
	if ((NU_Bind(socketd, &servaddr, 0)) < 0)
		return -1;

	/* Set the push bit for faster transmission. */
	/*NU_Push (socketd);*/

	/* be ready to accept connection requests */
	status = NU_Listen(socketd, maxconnect);

	return status ;
}

⌨️ 快捷键说明

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