📄 net_proc.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 */
/* */
/* MAIN_TASK.C */
/* */
/* DESCRIPTION */
/* */
/* The Start Function of the Application */
/* */
/* AUTHOR */
/* */
/* Morgan Lin */
/* */
/* DATA STRUCTURES */
/* */
/* */
/* FUNCTIONS */
/* */
/* NET_Exit */
/* PRINTF */
/* ServerProc */
/* ClientProc */
/* */
/* DEPENDENCIES */
/* */
/* nms.c */
/* */
/* HISTORY */
/* */
/* NAME DATE REMARKS */
/* */
/* */
/******************************************************************************/
/* Include */
#include "PUB\pub_incl.h"
#include "PUB\pub_defi.h"
#include "PUB\pub__msg.h"
#include "PUB\modulers.h"
#include "NMS.H"
#include "NET_PROC.H"
#define MAX_COMMAND_LENGTH 200
#define INTERVALTIME 1
TCPIP_SERVER_FSM ServerFsm;
devtimer_handle NetCtrlT0handle;
UC MsgFlag[Flag_Len];
UC NetLinkCot;
STATIC BOOL InitTimerComplete=FALSE;
/*
* Global Vars defined in other files, difined in nms.c
*/
extern DV_DEVICE_ENTRY *ETHER_Device;
extern STATUS SendPackageToDrv_Interrupt(DV_DEVICE_ENTRY *device);
/* function define in nms.c */
extern INT channel_send(int socketWhich, char *buffer, int len, int flags) ;
extern INT channel_recv(int socketWhich, char *buffer, int len, int flags) ;
extern INT channel_close(int socket) ;
extern INT channel_connect(char *cli_ip_addr, char *subnet, char *serv_ip_addr, int port) ;
extern INT channel_listen(int listenchannel, char *peer_addr) ;
extern INT channel_open_as_server(char *serv_ip_addr, char *subnet, int port, int maxconnect) ;
/*****************************************************
* Next lines define the extern vars and procedures
*
******************************************************/
void NET_Exit(int a) ;
/*
* functions defines in other files
*/
extern void PRINTF(char* string, ...) ;
/* This function provides an infinite loop for the program to terminate in.
The local variable a is an exit code which indicates where the program
aborted. */
void NET_Exit(int a)
{
int b = a;
while(1)
NU_Sleep (TICKS_PER_SECOND);
}
/*----------------------------------------------*/
/* Send Msg part */
/*----------------------------------------------*/
/* sender = PID_NET_SOCKET_FSM */
STATIC VOID ServerFsmSendMsg( UC receiver, UC msg_type, US len, VOID *data )
{
SendLetterToMail( PID_NET_SOCKET_FSM, receiver, msg_type, 0xFF, len, data );
}
STATIC VOID NetManSendMsg( US len, VOID *data )
{
SendLetterToMail( PID_NET_MAN, PID_NET_CTRL_FSM, 0xFF, 0xFF, len, data );
}
/*---------------------------------------------*/
STATIC VOID InitOneServerFsm( US port_num )
{
ServerFsm[port_num].State = INVALID_SOCKET;
ServerFsm[port_num].Socket = -1;
ServerFsm[port_num].ResemblePos = 0;
}
STATIC VOID StopAllServerTimer( UI port_num )
{
if( InitTimerComplete == TRUE )
Stop_Devtimer((devtimer_CB *)NetCtrlT0handle, port_num);
}
VOID ClearOneServerFsm( US port_num )
{
INT Socketd;
/* close socket */
Socketd = ServerFsm[port_num].Socket;
if( Socketd >= 0 )
channel_close( Socketd );
StopAllServerTimer( port_num );
InitOneServerFsm( port_num );
}
STATIC VOID SocketErrorHdl( US port_num )
{
/* report comm information */
ServerFsmSendMsg( PID_SYSMAN, MSG_NET_SOCKET_ERROR, 0, NULL );
}
VOID ServerTimerOut( UI port_num, UC timer_name )
{
SocketErrorHdl( port_num );
}
BOOL InitServerFsmTimer( VOID )
{
NetCtrlT0handle = Init_Devtimer(MAX_CLIENT_NUM, ServerTimerOut );
return TRUE;
}
VOID TrigServerFsmTimer( VOID )
{
if( InitTimerComplete == TRUE )
Trig_Devtimer( (devtimer_CB *)NetCtrlT0handle );
}
STATIC VOID InserviceServerFsm( US port_num, INT newsocket )
{
/* Update ServerFsm */
ServerFsm[port_num].State = IN_SERVICE_SOCKET;
ServerFsm[port_num].Socket = newsocket;
/* start timer */
Start_Devtimer((devtimer_CB *)NetCtrlT0handle, port_num, NET_CTRL_TIMER0_LEN, NET_CTRL_TIMER0_NAME);
}
BOOL GetInServiceSocketPortNum( US *port_num )
{
US i;
*port_num = SHORT_INVALID_VALUE;
for( i=0; i<MAX_CLIENT_NUM; i++ )
{
if( ServerFsm[i].State == IN_SERVICE_SOCKET )
{
*port_num = i;
return TRUE;
}
}
return FALSE;
}
STATIC VOID NetBufferDispatch( US port_num, VOID *buffer, UI length )
{
if( length >= MAX_MSG_LEN )
return;
NetManSendMsg( length, buffer );
/* restart timer */
Start_Devtimer((devtimer_CB *)NetCtrlT0handle,port_num,NET_CTRL_TIMER0_LEN,NET_CTRL_TIMER0_NAME);
}
STATIC BOOL AllocServerFsmUser( US *port_num )
{
US i;
STATIC US CurrServerFsmIndex=0;
for( i=0; i<MAX_CLIENT_NUM; i++ )
{
if( ServerFsm[CurrServerFsmIndex].State == INVALID_SOCKET )
{
*port_num = CurrServerFsmIndex;
CurrServerFsmIndex++;
if( CurrServerFsmIndex == MAX_CLIENT_NUM )
CurrServerFsmIndex = 0;
return TRUE;
}
else
{
CurrServerFsmIndex++;
if( CurrServerFsmIndex == MAX_CLIENT_NUM )
CurrServerFsmIndex = 0;
}
}
return FALSE;
}
STATIC VOID InitServerFsm( VOID )
{
US i;
NetLinkCot = INVALID_VALUE;
MsgFlag[0] = 0x55;
MsgFlag[1] = 0xAA;
MsgFlag[2] = 0x55;
for(i=0;i<MAX_CLIENT_NUM;i++)
InitOneServerFsm(i);
if( InitServerFsmTimer() == TRUE )
InitTimerComplete = TRUE;
}
STATIC VOID ReCombine( US port_num, UC * RecvBuf, US RecvLen )
{
US ResemblePos, PacketLen, DataLen;
UC *ResembleBuf;
US TotalLen,i;
INT Res;
ResemblePos = ServerFsm[port_num].ResemblePos;
ResembleBuf = ServerFsm[port_num].ResembleBuf;
TotalLen = ResemblePos + RecvLen;
PacketLen = 0;
if( TotalLen > MAX_RESEMBLE_LEN )
return;
else
memcpy(ResembleBuf+ResemblePos, RecvBuf, RecvLen); /* combine buf */
for(i = 0; i<=(TotalLen-TRANS_DATA_HEADER_LEN); i++)
{
Res = memcmp(ResembleBuf+i, MsgFlag , Flag_Len) ;
if( Res == 0 ) /* equal */
{
DataLen = ResembleBuf[TRANS_DATA_DLEN_POS+i]*256+ResembleBuf[TRANS_DATA_DLEN_POS+i+1];
PacketLen += TRANS_DATA_HEADER_LEN + DataLen;
if(TotalLen-i-PacketLen >= 0)
{
NetBufferDispatch(port_num, ResembleBuf+i+Flag_Len, PacketLen-Flag_Len);
i += PacketLen - 1;
PacketLen = 0;
}
else
break;
}
}
ServerFsm[port_num].ResemblePos = TotalLen-i;
memcpy(ResembleBuf, ResembleBuf+i, TotalLen-i);
}
STATIC VOID TcpipSendRecvHdl( US port_num )
{
STATUS status;
CHAR buffer[1600];
UNSIGNED len ;
INT bytes_sent, bytes_received;
INT socketd;
if( ServerFsm[port_num].State != IN_SERVICE_SOCKET ) /* invalid socket */
return;
socketd = ServerFsm[port_num].Socket;
if( socketd < 0 )
return;
/* Send data handle */
status = NU_Receive_From_Pipe(&Pipe_NetManagerCenter,(buffer+3),1,&len,NU_NO_SUSPEND);
if (status == NU_SUCCESS)
{
/* we don't block the send , if can't send now, we will unget the */
/* the message to the pipe */
NU_Fcntl(socketd, NU_SETFLAG, NU_FALSE);
/* add send header */
len += 3;
memcpy( buffer, MsgFlag, 3);
/* Send the datagram. */
bytes_sent = channel_send(socketd, buffer, len, 0);
/* If the data was not sent, we have a problem. */
if ( (bytes_sent != len) || ( (bytes_sent < 0) && (bytes_sent != NU_NO_BUFFERS) ) )
return;
}
/* Receive data handle part */
NU_Fcntl(socketd, NU_SETFLAG, NU_FALSE);
bytes_received = NU_Recv(socketd, buffer, 1024, 0); /* Go to get message . */
if( bytes_received > 0 )
ReCombine( port_num, buffer, bytes_received );
}
void ServerProc( void )
{
INT socketd = -1;
INT newsocket;
STATUS status;
US i, port_num;
CHAR agent_ip[] = {192,168,8,88};
CHAR agent_subnet[] = {255,255,255,0} ;
INT agent_port = 2000;
InitServerFsm( );
/* Create server socket */
while (1)
{
socketd = channel_open_as_server(agent_ip, agent_subnet, agent_port, 5) ;
if (socketd >= 0)
{
NU_Push(socketd);
break ;
}
NU_Sleep(20);
}
while (1)
{
NU_Fcntl(socketd, NU_SETFLAG, NU_FALSE);
newsocket = channel_listen(socketd, NU_NULL);
if( newsocket >= 0 )
{
/* valid socket */
if( AllocServerFsmUser( &port_num ) == TRUE )
InserviceServerFsm( port_num, newsocket );
else
channel_close( newsocket );
}
for(i=0; i<MAX_CLIENT_NUM; i++)
TcpipSendRecvHdl( i );
/* sleep for an interval time */
NU_Sleep(INTERVALTIME);
}
}
VOID ClientProc(VOID)
{
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -