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

📄 nms.c

📁 基于vxworks操作系统的电话语音平台系统
💻 C
📖 第 1 页 / 共 3 页
字号:
           	if ((NU_Bind(socketd, servaddr, 0))>=0)
           	{
           		NU_Deallocate_Memory(servaddr) ;
               	/* Set the push bit for faster transmission. */
               	NU_Push (socketd);
               	/* be ready to accept connection requests */
               	status = NU_Listen(socketd, maxconnect);
               	if (status == NU_SUCCESS)
               	{
               		/*
               		 * Insert the new listen sockted into Command_Listen_Daemon_List
              		 */
         	    	status = NU_Allocate_Memory(&System_Memory, &listen_daemon, 
         	    							(UNSIGNED)sizeof(LISTEN_DAEMON),NU_NO_SUSPEND);
    				if (status != NU_SUCCESS)
    				{
        				BYPrintf ("Can not create memory for listen daemon.\n");
        				return MSG_MEMORY_ERR;
    				}
    				listen_daemon = (LISTEN_DAEMON *)normalize_ptr(listen_daemon) ;
    				UTL_Zero(listen_daemon, sizeof(LISTEN_DAEMON));
    	
    				listen_daemon->port = port ;
    				listen_daemon->socketd = socketd ;
    				listen_daemon->connectnum = 0 ;
    				listen_daemon->next = NU_NULL ;
			    	Listen_Daemon_List_Insert(Command_Task_List.head, listen_daemon) ;	 
		
					return socketd ;
				}
				else
					return MSG_LISTEN_ERR ;
			}
			else
				return MSG_BIND_ERR ;    
		}
		else
			return MSG_MEMORY_ERR ;  
	}
	else
		return MSG_SOCKET_ERR ;
}


/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      OpenCommand                                               	 */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/* 		This Procedure use NU_ACCEPT to receive an connection from the   */
/*		peer host														 */
/* RETURN															     */
/*		new socketd .													*/
/*																		*/
/*************************************************************************/
/* 
 * when In debug, if define this vars as local, they can be watched correctly
 * in ICE , so should define them as global.
 */

INT OpenCommand(INT listenchannel, CHAR *peer_addr) 
{
	VOID           		*pointer;
    STATUS              status;
	INT                 newsock;
    struct addr_struct  client_addr;
    UINT32				*other_addr ;

	NU_PIPE				*sendpipe ;
	NU_PIPE				*recvpipe ;
	NU_TASK				*child_task ;

    SOCKET_TASK			*task_entry ;
    

	 
    /* block in NU_Accept until a client attempts 
       connection 
     */
     newsock = NU_Accept(listenchannel, &client_addr, 0);
     if (newsock <0)
     	return MSG_SOCKET_ACCEPT_ERR ;
     
     if (peer_addr != NU_NULL)
     {
     	other_addr = (UINT32 *)&client_addr.id ;

     	if ((UINT32)(*peer_addr) != (UINT32)(*other_addr))
     		return MSG_NOT_MATCH_ADDR ;
     }
     
     if (newsock >= 0)
     {
		BYPrintf("\n other site has connected to me.\n ");

		/*
 		 * Now Create send, recv pipes for this task
 		 */
    	/* Create sendpipe.  */
    	status = NU_Allocate_Memory(&System_Memory, (VOID **)&sendpipe, (UNSIGNED)sizeof(NU_PIPE), NU_NO_SUSPEND);
    	if (status != NU_SUCCESS)
    	{
        	BYPrintf ("Can not create memory for sendpipe.\n");
        	return MSG_MEMORY_ERR;
    	}
//    	sendpipe = (NU_PIPE *)normalize_ptr(sendpipe) ;
    	UTL_Zero(sendpipe, sizeof(NU_PIPE));
    	
    	status = NU_Allocate_Memory(&System_Memory, &pointer, 
    											MAX_COMMAND*MAX_COMMAND_LENGTH+100,
                                				NU_NO_SUSPEND);
    	if (status != NU_SUCCESS)
    	{
        	BYPrintf ("Can not create memory for sendpipe.\n");
        	return MSG_MEMORY_ERR;
    	}
    
    	status = NU_Create_Pipe(sendpipe, "sendpipe", pointer, MAX_COMMAND*MAX_COMMAND_LENGTH, 
    								NU_VARIABLE_SIZE,MAX_COMMAND_LENGTH, NU_FIFO);
    	if (status != NU_SUCCESS)
    	{
        	BYPrintf ("Can not create sendpipe.\n");
        	return MSG_SEND_PIPE_ERR;
    	}
    	
    	/* Create recvpipe.  */
    	status = NU_Allocate_Memory(&System_Memory, (VOID **)&recvpipe, (UNSIGNED)sizeof(NU_PIPE), NU_NO_SUSPEND);
    	if (status != NU_SUCCESS)
    	{
        	BYPrintf ("Can not create memory for recvpipe.\n");
        	return MSG_MEMORY_ERR;
    	}
//    	recvpipe = (NU_PIPE *)normalize_ptr(recvpipe) ;
    	UTL_Zero(recvpipe, sizeof(NU_PIPE));

    	status = NU_Allocate_Memory(&System_Memory, &pointer, 
    											MAX_COMMAND*MAX_COMMAND_LENGTH+100,
                                				NU_NO_SUSPEND);
    	if (status != NU_SUCCESS)
    	{
        	BYPrintf ("Can not create memory for recvpipe.\n");
        	return MSG_MEMORY_ERR;
    	}
    	status = NU_Create_Pipe(recvpipe, "recvpipe", pointer, MAX_COMMAND*MAX_COMMAND_LENGTH, 
    								NU_VARIABLE_SIZE,MAX_COMMAND_LENGTH, NU_FIFO);
    	if (status != NU_SUCCESS)
    	{
        	BYPrintf ("Can not create recvpipe.\n");
        	return MSG_RECV_PIPE_ERR;
    	}

        /*
         * create a task entry, and add it to Command_Task_List
         */
    	status = NU_Allocate_Memory(&System_Memory, &task_entry, (UNSIGNED)sizeof(SOCKET_TASK),NU_NO_SUSPEND);
    	if (status != NU_SUCCESS)
    	{
        	BYPrintf ("Can not create memory for sendpipe.\n");
        	return MSG_MEMORY_ERR;
    	}
//    	task_entry = (SOCKET_TASK *)normalize_ptr(task_entry) ;
    	UTL_Zero(task_entry, sizeof(SOCKET_TASK));
    	
    	task_entry->sendpipe = sendpipe ;
    	task_entry->recvpipe = recvpipe ;
    	task_entry->task = child_task ; 	
    	task_entry->socketd = newsock ;
    	task_entry->next = NU_NULL ;
    	Task_List_Insert(Command_Task_List.head, task_entry) ;	 
		
		/*
 		 * Now create the Child_Task that recv the command from the peer host
 		 */
    	status = NU_Allocate_Memory(&System_Memory, (VOID **)&child_task, (UNSIGNED)sizeof(NU_TASK),NU_NO_SUSPEND);
    	if (status != NU_SUCCESS)
    	{
        	BYPrintf ("Can not create memory for child task.\n");
        	return MSG_MEMORY_ERR;
    	} 		 
//    	child_task = (NU_TASK *)normalize_ptr(child_task) ;
    	UTL_Zero(child_task, sizeof(NU_TASK));

		status = NU_Allocate_Memory(&System_Memory, &pointer, 2000, NU_NO_SUSPEND);
		if (status != NU_SUCCESS)
    	{
        	BYPrintf ("Can not allocate memory.\n");
        	return MSG_MEMORY_ERR;
	    }
    	
    	status = NU_Create_Task(child_task, "CHILDTASK", Child_Task,
                            1, task_entry, pointer, 2000, 2, 0, NU_PREEMPT, NU_START);
    	if (status != NU_SUCCESS)
    	{
        	BYPrintf ("Cannot create child task\n");
        	return  MSG_TASK_ERR;
    	}
    	NU_Sleep(2);
    	
        return newsock ;
	}
	else
		return MSG_SOCKET_ACCEPT_ERR ;
} 

/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      CloseCommand                                               	 	 */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/* 		This Procedure Close the channel that high layer apps use.       */
/*																		 */
/* RETURN															     */
/*		status .		 												 */
/*																		 */
/*************************************************************************/ 
STATUS CloseCommand(INT channel, INT closemode, INT timeout)
{
	CHAR pipe_name[8];
	VOID *start_address;
	UNSIGNED pipe_size;
	UNSIGNED available;
	UNSIGNED messages;
	OPTION message_type;
	UNSIGNED message_size;
	OPTION suspend_type;
	UNSIGNED tasks_suspended;
	NU_TASK *first_task;
	
	STATUS status ;
	INT		i ;

  	SOCKET_TASK *current_task ;	

  	if (closemode == CLOSE_RETURNIMMEDIATE)
  	{
		for(current_task = Command_Task_List.head ; current_task != NU_NULL ;current_task=current_task->next)
		{
			if (current_task->socketd == channel)
			{
				status = NU_Pipe_Information(current_task->sendpipe, pipe_name, &start_address,
											&pipe_size, &available, &messages,
											&message_type, &message_size,
											&suspend_type, &tasks_suspended,
											&first_task);
				if (messages != 0)
					return MSG_SENDPIPE_HAS_MORE_MSGS_ERR ;
				
				status = NU_Pipe_Information(current_task->recvpipe, pipe_name, &start_address,
											&pipe_size, &available, &messages,
											&message_type, &message_size,
											&suspend_type, &tasks_suspended,
											&first_task);
				if (messages != 0)
					return MSG_RECVPIPE_HAS_MORE_MSGS_ERR ;
			}
		}
	}
	else if (closemode == CLOSE_WAIT)
	{
		for(current_task = Command_Task_List.head ; 
					current_task != NU_NULL ;current_task=current_task->next)
		{
			if (current_task->socketd == channel)
			{
				if (timeout > 0)
				{
					for (i=0 ; i<timeout ; i=i+2)
					{
						status = NU_Pipe_Information(current_task->sendpipe, pipe_name, &start_address,
											&pipe_size, &available, &messages,
											&message_type, &message_size,
											&suspend_type, &tasks_suspended,
											&first_task);
						if (messages != 0)
						{
							NU_Sleep(2) ;
							continue ;
						}
				
						status = NU_Pipe_Information(current_task->recvpipe, pipe_name, &start_address,
											&pipe_size, &available, &messages,
											&message_type, &message_size,
											&suspend_type, &tasks_suspended,
											&first_task);
						if (messages != 0)
						{
							NU_Sleep(2) ;
							continue ;
						}
					}
				}
			}
		}
	}
	else // CLOSE_IMMEDIATE
	{
	}
	
	if ((NU_Close_Socket(channel)) != NU_SUCCESS)
  	{
    	BYPrintf("\nError from NU_Close_Socket.");
    	return MSG_CHANNEL_CLOSE_ERR;
  	}
  	Task_List_Delete(Command_Task_List.head, channel) ;
  	return NU_SUCCESS ;
}

⌨️ 快捷键说明

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