151 lines
3.0 KiB
C++
151 lines
3.0 KiB
C++
#include <fcntl.h>
|
||
#include <sys/stat.h>
|
||
|
||
#if defined(_UNIX_)
|
||
#include <stdlib.h>
|
||
#include <string.h>
|
||
#include <unistd.h>
|
||
#include <stdio.h>
|
||
#include <netdb.h>
|
||
#include <sys/socket.h>
|
||
#include <time.h>
|
||
#include <stdarg.h>
|
||
#elif defined(WIN32)
|
||
#include <direct.h>
|
||
#include <io.h>
|
||
#endif
|
||
|
||
#include "netapi.h"
|
||
//#include "afert.h"
|
||
|
||
void _byte_swap(int ilen,void *addr)
|
||
{
|
||
if( (ilen%2!=0)||(ilen<=0) ) //字节数应是2的整数倍
|
||
return;
|
||
unsigned char sp,*src;
|
||
src = (unsigned char *)addr;
|
||
for(int i = 0;i<ilen/2;i++)
|
||
{
|
||
BYTE_SWAP(i,ilen-1-i);//例,对整数n,byte_swap(sizeof(n),n),b0--b3,b1--b2.
|
||
}
|
||
return;
|
||
}
|
||
|
||
void _get_host_name(char *name)
|
||
{
|
||
|
||
char host[100];
|
||
gethostname(host,99);
|
||
char *host1 = strchr(host,'.');
|
||
if(host1!=NULL) *host1 = 0;
|
||
if(strlen(host)>=HOSTNAMELEN) host[0] = 0;
|
||
else strcpy(name,host);
|
||
}
|
||
|
||
int _get_last_error()
|
||
{
|
||
#if defined (WIN32)
|
||
return WSAGetLastError();
|
||
#elif defined (_UNIX_)
|
||
|
||
#if defined (__osf__)
|
||
return _Geterrno();
|
||
#elif defined (_SOLARIS)
|
||
int ret = errno;
|
||
return errno;
|
||
#endif
|
||
#endif
|
||
}
|
||
|
||
|
||
void _delay_time(int n) //usec
|
||
{
|
||
#if defined (WIN32)
|
||
Sleep(n/1000);
|
||
#elif defined (_UNIX_)
|
||
struct timeval tt;
|
||
int ret;
|
||
while(TRUE)
|
||
{
|
||
tt.tv_sec=n/1000000;
|
||
tt.tv_usec=n%1000000;
|
||
ret=select(0,0,0,0,&tt);
|
||
if(ret<0){
|
||
if(_get_last_error()==EINTR)
|
||
{
|
||
n=n/2;
|
||
if(n>=50000) continue;
|
||
}
|
||
}
|
||
break;
|
||
}
|
||
#endif
|
||
}
|
||
|
||
/*
|
||
char _get_chan_status(int groupno,int chno,char mainFepFlag)
|
||
{
|
||
if(groupno<0 || groupno>=GROUP_MAX) return CHANSTOP;
|
||
if(chno<0 || chno>=CHANNUMS) return CHANSTOP;
|
||
|
||
char name[HOSTNAMELEN];
|
||
_get_host_name(name);
|
||
|
||
HEAD txhead,rxhead;
|
||
strcpy(txhead.src.host,name);
|
||
strcpy(txhead.src.user,"FEP");
|
||
txhead.cmdtype = ChanDataGram;
|
||
txhead.userdata1 = chno;
|
||
txhead.userdata2 = 0;
|
||
txhead.len = 0;
|
||
|
||
NetStatus cfg;
|
||
|
||
char* main = cfg.GetMainServerHost(groupno,FEP_SERVER);
|
||
|
||
if(mainFepFlag)
|
||
{
|
||
if(strcmp(main,"")==0) return CHANSTOP;
|
||
|
||
int n = cfg.GetHostNo(groupno,main);
|
||
Tcpip link(n,NET_TYPE_CMD,CMDPORT);
|
||
if(link.TxHead(&txhead)==FALSE) return CHANSTOP;
|
||
if(link.RxHead(&rxhead)==FALSE) return CHANSTOP;
|
||
if(rxhead.userdata1!=chno) return CHANSTOP;
|
||
return rxhead.userdata2;
|
||
}
|
||
else
|
||
{
|
||
int8u status[SERVER_NODE_MAX];
|
||
for(int i=0; i<SERVER_NODE_MAX; i++)
|
||
status[i] = CHANSTOP;
|
||
|
||
for(i=0; i<SERVER_NODE_MAX; i++)
|
||
{
|
||
char* hostname = cfg.GetHostName(groupno,i,FEP_SERVER);
|
||
if(cfg.GetHostRstate(groupno,hostname)<=HOST_STATUS_STOP) continue;
|
||
if(strcmp(hostname,"")==0) continue;
|
||
if(strcmp(hostname,main)==0) continue;
|
||
|
||
int n = cfg.GetHostNo(groupno,hostname);
|
||
Tcpip link(n,NET_TYPE_CMD,CMDPORT);
|
||
if(link.TxHead(&txhead)==FALSE) continue;
|
||
if(link.RxHead(&rxhead)==FALSE) continue;
|
||
if(rxhead.userdata2==CHANRUN) return CHANRUN;
|
||
status[i] = rxhead.userdata2;
|
||
}
|
||
|
||
for(i=0; i<SERVER_NODE_MAX; i++)
|
||
if(status[i]==CHANRATE) return CHANRATE;
|
||
|
||
for(i=0; i<SERVER_NODE_MAX; i++)
|
||
if(status[i]==CHANUNSYN) return CHANUNSYN;
|
||
|
||
for(i=0; i<SERVER_NODE_MAX; i++)
|
||
if(status[i]==CHANSEARCH) return CHANSEARCH;
|
||
}
|
||
|
||
return CHANSTOP;
|
||
}
|
||
|
||
*/ |