//南自PSL6000网络103协议处理
#include "stdafx.h"
#include "CreateTask.h"
#include "\fsa3000\dev\env\inc\fjRlibdef.h"
unsigned short g_Net_Rec_Buf_Item_In_Pt_PSL =0;
STRUCT_NetRecv g_Net_Recv_Buf_PSL[MAX_NET_RECV_ITEM_NUM];
//BOOL g_PSL103_Udp_sent = FALSE;
//服务器端socket for PSL
UINT PSLInitTcp( LPVOID pParam )
{
BYTE TaskId = (BYTE)(DWORD)pParam; //第一个启动服务器socket的任务ID
//CChannelSignal *pChannel = Task[TaskId]->pChannel;
int i, TcpsockAddSize,tmp=1;
struct sockaddr_in TcpclientAddr;
struct sockaddr_in TcpserverAddr;
fd_set rd_fds;
SOCKET TcpSocketId=INVALID_SOCKET,AcceptTcpId=INVALID_SOCKET;
//只建一个tcp server , 在外面判断了,这里不可以再判断
//if(SrvSocketPSL.Server_Has_Create_PSL != 0) return 0;
//SrvSocketPSL.Server_Has_Create_PSL = 1;
TcpsockAddSize = sizeof(struct sockaddr_in);
while(1)
{
if(bThreadExit)break;
memset((char*)&TcpserverAddr,0,TcpsockAddSize);
TcpserverAddr.sin_family = AF_INET;
TcpserverAddr.sin_port = htons(SrvSocketPSL.TcpPort_PSL);
TcpserverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
if((TcpSocketId = ::socket(AF_INET,SOCK_STREAM,0))==INVALID_SOCKET)
{
Sleep(800);
continue;
}
if(bind(TcpSocketId,(struct sockaddr *)&TcpserverAddr,TcpsockAddSize)==SOCKET_ERROR)
{
::closesocket(TcpSocketId);
Sleep(800);
continue;
}
if(listen(TcpSocketId,SOMAXCONN)==SOCKET_ERROR)
{
::closesocket(TcpSocketId);
Sleep(800);
continue;
}
FD_ZERO(&rd_fds);
FD_SET(TcpSocketId,&rd_fds);
//进入下面,就会接收多个远方发送的连接请求
while(1)
{
if(bThreadExit)break;
if(select(TcpSocketId+1,&rd_fds,(struct fd_set *)0,(struct fd_set *)0,(struct timeval *)0)<=0)
{
if(bThreadExit)break;
continue;
}
if(FD_ISSET(TcpSocketId,&rd_fds))
{
tmp=1;
if(ioctlsocket(TcpSocketId,FIONBIO,(unsigned long*)&tmp) == SOCKET_ERROR) //非阻塞socket
{
Sleep(20);
continue;
}
AcceptTcpId = accept(TcpSocketId,(struct sockaddr *)&TcpclientAddr,&TcpsockAddSize);
if(AcceptTcpId != INVALID_SOCKET)
{
for(i=0;i<SrvSocketPSL.ScadaNum_PSL;i++) //查找对应的结构体
{
if(TcpclientAddr.sin_addr.s_addr == SrvSocketPSL.Client_PSL[i].ConnIp_PSL)
{
if(SrvSocketPSL.Client_PSL[i].Socket_PSL != INVALID_SOCKET)
{
closesocket(SrvSocketPSL.Client_PSL[i].Socket_PSL);
SrvSocketPSL.Client_PSL[i].Socket_PSL = INVALID_SOCKET;
}
//设置为非阻塞
if(ioctlsocket(AcceptTcpId,FIONBIO,(unsigned long*)&tmp) == SOCKET_ERROR)
{
shutdown(AcceptTcpId,2);
closesocket(AcceptTcpId);
}
else
{
SrvSocketPSL.Client_PSL[i].Socket_PSL = AcceptTcpId; //如果连接的IP和配置的IP相同存放socket
CProtectSignal *pProtect = GetProtectByAddressAndRtuId((SrvSocketPSL.Client_PSL[i].ConnIp_PSL>>24),Task[SrvSocketPSL.Client_PSL[i].TaskId_PSL]->pChannel->m_channel.m_RTU_ID);
if(NULL!=pProtect)
if(pProtect->m_Send103Flag==0)
pProtect->m_Send103Flag = 1; //开始发送总召唤
}
break;
}
}
if(i>=SrvSocketPSL.ScadaNum_PSL) //未找到,需要删除该socket
{
shutdown(AcceptTcpId,2);
closesocket(AcceptTcpId);
}
}
Sleep(500);
}
}
}
if(AcceptTcpId != INVALID_SOCKET)
{
shutdown(AcceptTcpId, 2);
::closesocket(AcceptTcpId);
}
if(TcpSocketId != INVALID_SOCKET)
{
shutdown(TcpSocketId, 2);
::closesocket(TcpSocketId);
}
return 11;
}
//服务器收数据 针对PSL装置
UINT PSLTCPRecv(LPVOID pParam)
{
BYTE TaskId = (BYTE)(DWORD)pParam;
int i, error, RecvLen = 0;
bThreadBusy[LIMIT_TASK_NUM*2+TaskId]=TRUE;
while (true)
{
if(bThreadExit)break;
for(i=0;i<SrvSocketPSL.ScadaNum_PSL;i++) //循环接收报文
{
if(SrvSocketPSL.Client_PSL[i].Socket_PSL != INVALID_SOCKET)
{
CSingleLock SingleLock(ptrSemaphore_PSL[i]);
SingleLock.Lock();
//接收报文,应用层处理后会重置读写指针,所以认为队列不会翻转
RecvLen = recv(SrvSocketPSL.Client_PSL[i].Socket_PSL,(char*)&TCPQUE_PSL[i][WRITE_PSL[i]],BUFFER_SIZE - WRITE_PSL[i],0);
if(RecvLen > 0)
{
WRITE_PSL[i] += RecvLen;
if(WRITE_PSL[i] >= BUFFER_SIZE)WRITE_PSL[i] = 0;
SrvSocketPSL.Client_PSL[i].RecvCnt_PSL = 0;
}
else
{
if(RecvLen == SOCKET_ERROR)
error =::GetLastError();
else
error =0;
SrvSocketPSL.Client_PSL[i].RecvCnt_PSL++;
if((WSAECONNRESET == error)||(WSAECONNABORTED == error)
//||(SrvSocketPSL.Client_PSL[i].RecvCnt_PSL > 10000)
)
{
SrvSocketPSL.Client_PSL[i].RecvCnt_PSL = 0;
if(SrvSocketPSL.Client_PSL[i].Socket_PSL != INVALID_SOCKET)
{
shutdown(SrvSocketPSL.Client_PSL[i].Socket_PSL,2);
closesocket(SrvSocketPSL.Client_PSL[i].Socket_PSL);
SrvSocketPSL.Client_PSL[i].Socket_PSL = INVALID_SOCKET;
CProtectSignal *pProtect = GetProtectByAddressAndRtuId((SrvSocketPSL.Client_PSL[i].ConnIp_PSL>>24),Task[SrvSocketPSL.Client_PSL[i].TaskId_PSL]->pChannel->m_channel.m_RTU_ID);
if(NULL!=pProtect)
pProtect->m_Send103Flag = 0;
}
WRITE_PSL[i] = READ_PSL[i] = 0;
memset(TCPQUE_PSL[i],0xff,BUFFER_SIZE); //初始化为全ff
}
}
SingleLock.Unlock();
}
}
Sleep(5);
}
bThreadBusy[LIMIT_TASK_NUM*2+TaskId]=FALSE;
return 12;
}
/*****************************************************************************
* Net103_Psk_Zk_Init
*****************************************************************************
*
* Created at : 2007年7月18日 15:16:48
* Description : 从端口参数中读取规约相关参数.
*
*
* Parameters : long port - 端口号, 0-31
*
* Return Value: int -1 ---- error.
* Remarks :
*
* Updated By Date Description
* -------------+---------+------------
* | |
****************************************************************************/
void PSL103_Zk_Init(BYTE TaskId,PSK103_PORT_FLAG *Psk103_ram_port_flag)
{
unsigned short cnt;
CChannelSignal *pChannel = Task[TaskId]->pChannel;
if(pChannel == NULL) return;
Psk103_ram_port_flag->psk103_send_dl = 0;
Psk103_ram_port_flag->psk103_recv_dl = 0;
Psk103_ram_port_flag->psk103_allcheck_rii = 0;
Psk103_ram_port_flag->psk103_allcheck_unit_num = 0;
Psk103_ram_port_flag->psk103_state = PSK103_IDLE;
Psk103_ram_port_flag->psk103_allcheck_time = g_Time2048msCounter - PSK103_NET_ALL_CHECK_SPACE_TIME_VALUE + 4; //8秒后启动总召
Psk103_ram_port_flag->psk103_allcheck_unit_time = g_Time2048msCounter - PSK103_UNIT_FIRST_ALL_CHECK_SPACE_TIME_VALUE;
Psk103_ram_port_flag->psk103_allcheck_flag = OFF;
Psk103_ram_port_flag->psk103_xhfg_flag = OFF;
Psk103_ram_port_flag->psk103_xhfg_unit_no = 0;
Psk103_ram_port_flag->psk103_time_unit_no = 0;
Psk103_ram_port_flag->psk103_allcheck_unit_no = 0;
Psk103_ram_port_flag->psk103_net_tran_flag = OFF;
Psk103_ram_port_flag->psk103_Udp_heart_time = g_Time2048msCounter-30; //启动后5秒发送
Psk103_ram_port_flag->psk103_Udp_heart_unit_no = 0;
Psk103_ram_port_flag->ChgDzRyb = OFF;
Psk103_ram_port_flag->psk103_max_net_unit = pChannel->RTU_num;
if (Psk103_ram_port_flag->psk103_max_net_