📄 nms.c
字号:
/*************************************************************************/
/* */
/* Copyright (c) 1998 - 1999 ZZX Communicatoin Technology, Inc. */
/* */
/* PROPRIETARY RIGHTS of ZZX Communication Technology are involved in the*/
/* subject matter of this material. All manufacturing, reproduction, */
/* use, and sales rights pertaining to this subject matter are governed */
/* by the license agreement. The recipient of this software implicitly */
/* accepts the terms of the license. */
/* */
/*************************************************************************/
/******************************************************************************/
/* */
/* FILENAME */
/* */
/* NMS.C */
/* */
/* DESCRIPTION */
/* */
/* The Start Function of the Application */
/* */
/* AUTHOR */
/* */
/* Morgan Lin */
/* */
/* DATA STRUCTURES */
/* */
/* */
/* FUNCTIONS */
/* */
/* InitCommand */
/* OpenCommand */
/* SendCommand */
/* RecvCommand */
/* CloseCommand */
/* Child_Task */
/* */
/* channel_open_as_server */
/* channel_send */
/* channel_recv */
/* channel_close */
/* channel_connect */
/* channel_listen */
/* */
/* DEPENDENCIES */
/* */
/* netdriver.c */
/* */
/* HISTORY */
/* */
/* NAME DATE REMARKS */
/* */
/* */
/******************************************************************************/
/* Include */
#include "NMS.H"
/*
define MACRO DEFINES
*/
#define MAX_COMMAND_LENGTH 200
#define MAX_COMMAND 200
#define COMMAND_INTERVAL 100
#define MAX_LISTEN_DAEMON 100
//#define NETDEMOTEST
/*
Global Vars defined in this file
*/
/* Used by application*/
SOCKET_TASK_LIST Command_Task_List ;
LISTEN_DAEMON_LIST Command_Listen_Daemon_List ;
/*
Procedures defined in this file
*/
INT InitCommand(CHAR *serv_ip_addr, CHAR *subnet, INT port, INT maxconnect) ;
STATUS CloseAllCommand(INT listenchannel) ;
INT OpenCommand( INT listenchannel, CHAR *peeraddr) ;
INT CloseCommand(INT channel, INT closemode, INT timeout) ;
STATUS SendCommand(INT channel, CHAR *buffer, INT length, INT timeout) ;
STATUS RecvCommand(INT channel, CHAR *buffer, INT length, INT *actual_length, INT timeout) ;
INT InitClientConnect(CHAR *client_ip_addr, CHAR *subnet, CHAR *serv_ip_addr, INT port) ;
void Child_Task(UNSIGNED argc, VOID *argv);
INT channel_send(int socketWhich, char *buffer, int len, int flags) ;
INT channel_recv(int socketWhich, char *buffer, int len, int flags) ;
INT channel_close(int socket) ;
INT channel_connect(char *cli_ip_addr, char *subnet, char *serv_ip_addr, int port) ;
INT channel_listen(int listenchannel, char *peer_addr) ;
INT channel_open_as_server(char *serv_ip_addr, char *subnet, int port, int maxconnect) ;
/*****************************************************
* Next lines define the extern vars and procedures
*
*****************************************************/
/*
vars defined in main_task.c
*/
extern NU_MEMORY_POOL System_Memory;
/*
defined at ati/net/src/net.c, also used by net init
*/
extern STATUS NET_Ether_Input(VOID) ;
extern STATUS NET_Ether_Send (NET_BUFFER *, DV_DEVICE_ENTRY *,
SCK_SOCKADDR_IP *, RTAB_ROUTE *);
/*
defined in pub/netdrive.c, used by net init
*/
void PRINTF(char* string, ...) ;
extern STATUS TransMsgToSI(SI scc_num, VOID *buf, SI length) ;
extern STATUS EthernetInit(CHAR *ip, CHAR *subnet, INT mode) ;
/*
defined in task_list.c
*/
extern STATUS Task_List_Init(SOCKET_TASK_LIST *socket_task_list) ;
extern STATUS Task_List_Insert(SOCKET_TASK *socket_task_head, SOCKET_TASK *task_entry) ;
extern STATUS Task_List_Delete(SOCKET_TASK *socket_task_head, INT channel);
/*
defined in main_task.c
*/
extern void NET_Exit(int);
/*************************************************************************/
/* */
/* FUNCTION */
/* */
/* Child_Task */
/* */
/* DESCRIPTION */
/* */
/* Read a stream socket one buffer at a time */
/* and write each buffer to the recvpipe. */
/* and also recv command from sendpipe, and send it to the other */
/* peer, Return when the connection is terminated. */
/* */
/*************************************************************************/
void Child_Task(UNSIGNED argc, VOID *argv)
{
int connected = 1;
INT i ;
CHAR buffer[MAX_COMMAND_LENGTH+1] ;
UNSIGNED actsize;
UNSIGNED bytes_recv, bytes_sent;
STATUS status;
SOCKET_TASK *current_task ;
INT connectsocketd ;
NU_PIPE *sendp ;
NU_PIPE *recvp ;
/*
* argc should be 1, and argv should point to the socket_task
*/
current_task = (SOCKET_TASK *)(argv) ;
connectsocketd = current_task->socketd ;
sendp = current_task->sendpipe ;
recvp = current_task->recvpipe ;
/*
* Now not use the global allocated memory, always something wrong..
*/
/*
status = NU_Allocate_Memory(&System_Memory, &buffer,MAX_COMMAND_LENGTH+1, NU_SUSPEND);
*/
while(connected)
{
/* turn on the "block during a read" flag */
NU_Fcntl(connectsocketd, NU_SETFLAG, NU_FALSE);
status = NU_Recv(connectsocketd, buffer, (UINT16)MAX_COMMAND_LENGTH, 0);
if (status == NU_NOT_CONNECTED)
{
connected = 0;
continue;
}
bytes_recv = status ;
if (bytes_recv > 0)
{
/*
* send it to high layer appS
*/
status = NU_Send_To_Pipe(recvp,
(VOID *)buffer, bytes_recv, NU_NO_SUSPEND);
if (status != NU_SUCCESS)
{
if (status == NU_PIPE_FULL)
BYPrintf("recv pipe is full\n") ;
else
BYPrintf("recv pipe error\n") ;
}
}
status = NU_Receive_From_Pipe(sendp, (VOID *) &buffer,
(UNSIGNED)MAX_COMMAND_LENGTH, (UNSIGNED *)&actsize, NU_NO_SUSPEND);
if (status != NU_SUCCESS)
{
if (status != NU_PIPE_EMPTY)
BYPrintf (" receive a message from recvpipe err");
}
else if ((actsize > 0) && (actsize < MAX_COMMAND_LENGTH) )
{
NU_Fcntl(connectsocketd, NU_SETFLAG, NU_FALSE);
bytes_sent = NU_Send(connectsocketd, buffer, actsize, 0);
if (bytes_sent != actsize)
{
BYPrintf("NU_Send error") ;
/*
* reenter the command into sendpiped to be used by next time
*/
status = NU_Send_To_Front_Of_Pipe(sendp,
(VOID *)buffer, actsize, NU_NO_SUSPEND);
}
}
/* these line should be remove when in release version, for it is
test with netdemo
*/
#ifdef NETDEMOTEST
if( (status != NU_SUCCESS) || (actsize <=0))
{
//in test mode
NU_Fcntl(connectsocketd, NU_SETFLAG, NU_FALSE);
actsize = 10 ;
bytes_sent = NU_Send(connectsocketd, buffer, actsize, 0);
}
#endif
/********/
NU_Sleep(COMMAND_INTERVAL) ;
}
/* close the connection , and remove the entry from the Command_Task_List*/
NU_Close_Socket(current_task->socketd) ;
CloseCommand(connectsocketd, CLOSE_IMMEDIATE, 0) ;
// NU_Suspend_Task(NU_Current_Task_Pointer());
// NET_Exit(21);
}
/*************************************************************************/
/* */
/* FUNCTION */
/* */
/* InitCommand */
/* */
/* DESCRIPTION */
/* */
/* This Procedure will init the command communicatioin will */
/* Will needed . */
/* */
/*************************************************************************/
INT InitCommand(CHAR *serv_ip_addr, CHAR *subnet, INT port, INT maxconnect)
{
INT socketd;
struct addr_struct *servaddr;
VOID *pointer;
STATUS status;
struct addr_struct client_addr;
LISTEN_DAEMON *listen_daemon ;
/* first init ethernet */
EthernetInit(serv_ip_addr, subnet, 1) ;
for(listen_daemon = Command_Listen_Daemon_List.head ;
listen_daemon != NU_NULL ; listen_daemon = listen_daemon->next)
{
if (listen_daemon->port == port)
return listen_daemon->socketd ;
}
/* open a connection via the socket interface */
if ((socketd = NU_Socket(NU_FAMILY_IP, NU_TYPE_STREAM, 0)) >=0 )
{
status = NU_Allocate_Memory(&System_Memory, &pointer,
sizeof(struct addr_struct), NU_SUSPEND);
if (status == NU_SUCCESS)
{
servaddr = (struct addr_struct *)pointer;
/* 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 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -