📄 tcpclnt.cpp
字号:
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright (C), 2005, TopBond Tech. Co., Ltd.
* File name: TCPCLNT.CPP
* Module name: 以客户端方式建立TCP连接的模块
* Author/Date: WuHuan 06/02/14
* Version: Ver 1.0
* Description: 以客户端方式(或称主动方式)建立TCP连接系列函数的.CPP文件
* Others: 如有改动,请在下面的历史记录登记,谢谢合作!
------------------History-----------------------------
* Modifier/Date: //修订人及修订日期
* Modify Reason: //修订原因
* Modification: //修订的内容和位置的简要说明
------------------------------------------------------
* Modifier/Date: //修订人及修订日期
* Modify Reason: //修订原因
* Modification: //修订的内容和位置的简要说明
------------------------------------------------------
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include <stdio.h>
#include <string.h>
#include <dos.h>
#include <time.h>
#include "etr_ppp.h"
#include "tcpclnt.h"
#include "keymenu.h"
#define LINKTOUT 800 //设置超时时间的Tick值,一个Tick值约为55ms
extern int HeartBeat; //心跳报文外部变量的声明,原型在MAIN.CPP中
extern int TalkTime;
class TCPClientManager* TCPManager[2]; //定义一个TCPClientManager类的对象
SERVER_ID& SERVER_ID::operator = (SERVER_ID& SID)
{
strcpy( IPStr, SID.IPStr );
Port = SID.Port;
return *this;
}
//初始化TCPClientManager类的成员变量
TCPClientManager::TCPClientManager( )
{
pHost = new struct SERVER_ID;
strcpy( pHost->IPStr, "10.0.0.2" );
pHost->Port = 1001;
ConnState = CLOSED;
Timeout = 0;
TalkTimeout = 0; //add by wuhuan 2006.7.14
Conno = -1;
netprocessnum = 0;
}
//TCPClientManager类的析构函数
TCPClientManager::~TCPClientManager( )
{
delete pHost;
}
void TCPClientManager::Init( struct SERVER_ID* pSvrID )
{
strcpy( pHost->IPStr, pSvrID->IPStr );
pHost->Port = pSvrID->Port;
}
// return connect state
CONN_STATE TCPClientManager::Running( )
{
int i, myport;
//static int processnum=0;
//netprocessnum++;
//if(processnum > 1)
//if(netprocessnum > 1)
//{
NetPackagePro();
//netprocessnum =1;
//}
// step1: processing opened connect
switch( ConnState )
{
case OPEN:
// check if opened conn is timeout then
if( ConnIsFinished( Conno )||ConnIsFatal( Conno) )
{
ConnClose( Conno, 1 );
ConnState = CLOSED;
Timeout = 0;
//TalkTimeout = 0; //add by wuhuan 2006.7.28:9:57
Conno = -1;
if(GetKPState==LOCK) printf( "Server%d Conn Closed\n",ServerNum );
}
break;
case START:
// step: check if the conn is established
if( ConnIsEstablished( Conno ) )
{
// connection established at last!
ConnState = OPEN;
if(GetKPState==LOCK) printf( "Server%d Conn is Established!\n",ServerNum );
SetTimeoutCnt( 1 );
SetTalkTimeoutCnt( TalkTime );
}
if( IsTimeout( ) )
{
if(GetKPState==LOCK) printf( "Server%d Conn is timeout!\n",ServerNum );
ConnClose( Conno, 1 );
ConnState = CLOSED;
Timeout = 0;
Conno = -1;
}
break;
case CLOSED:
myport = MyPort( );
Conno = ConnOpen( pHost->IPStr, "TCP/IP", myport, pHost->Port, NONBLOCKOPEN );
if( Conno < 0 )
{
// goto error processing...
ConnState = FAIL;
return ConnState;
}
SetTimeoutCnt( LINKTOUT );
ConnState = START;
break;
case FAIL:
//ConnState = CLOSED;
Timeout = 0;
//TalkTimeout = 0; //add by wuhuan 2006.7.28:9:57
Conno = -1;
if(GetKPState==LOCK) printf( "Server%d TCP FAIL\n",ServerNum );
break;
}
return ConnState; // data is unavailable
}
int TCPClientManager::Close( )
{
int i1;
if( ConnState == CLOSED ) return 0;
if( ConnState != FAIL ) i1 = ConnClose( Conno, 1 );
ConnState = CLOSED;
Timeout = 0;
// TalkTimeout = 0; //add by wuhuan 2006.7.28:9:57
if(GetKPState==LOCK) printf( "ConnNum Close = %d\n", Conno );
Conno = -1;
return i1;
}
///////////////////////////////////////////////////////add by wuhuan 2006.7.14
// 1 Tick = 55ms; NumTick=0: no timeout
int TCPClientManager::SetTalkTimeoutCnt( long NumTick )
{
TalkTimeout = NumTick;
if( NumTick > 0 ) TalkStartTick = clock( );
return 0;
}
// return = 1: time out, =0: not yet!
int TCPClientManager::TalkIsTimeout( )
{
unsigned long CurrentTick;
if( TalkTimeout == 0 ) return 0;
CurrentTick = clock( );
if( CurrentTick >= (TalkStartTick + TalkTimeout) )
{
TalkStartTick = CurrentTick;
return 1;
}
return 0;
}
// 1 Tick = 55ms; NumTick=0: no timeout
int TCPClientManager::SetTimeoutCnt( long NumTick )
{
Timeout = NumTick;
if( NumTick > 0 ) StartTick = clock( );
return 0;
}
// return = 1: time out, =0: not yet!
int TCPClientManager::IsTimeout( )
{
unsigned long CurrentTick;
if( Timeout == 0 ) return 0;
CurrentTick = clock( );
if( CurrentTick >= (StartTick+Timeout) )
{
StartTick = CurrentTick;
return 1;
}
return 0;
}
int TCPClientManager::WriteData( char* DatBuf, int DatLen )
{
int i1;
if( Conno<0 ) return EBADF;
for( i1=0; i1<1000; i1++ )
if( ConnCanSend( Conno, DatLen ) ) break;
if( i1<1000 )
{
i1 = ConnWrite( Conno, DatBuf, DatLen, 1 );
return i1;
}
return ENOBUFS;
}
int TCPClientManager::ReadData( char* DatBuf, int DatLen )
{
int i1;
if( Conno<0 ) return EBADF;
if( ConnHasData( Conno ) )
{
i1 = ConnRead( Conno, DatBuf, DatLen );
return i1;
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -