?? netagentthread.cpp
字號:
#include "stdafx.h"
#include "envInit.h"
#include "spIPCComm.h"
using namespace commIPC;
//
SOCKET InitNetworkListen(LPCSTR pszAddr,int iPort)
{
//準備監聽
int iLocalPort =iPort;
SOCKET sockListen = SPCreateListenSocket(pszAddr,iLocalPort);
if(sockListen == SOCKET_ERROR)
{
GetConsoleWnd(1)->printf("[error] init network listen socketed");
return SOCKET_ERROR;
}
return sockListen;
}
//網絡監聽線程
DWORD NetworkListenThread(CAgentIni *pIni)
{
SOCKET sockListen = InitNetworkListen(NULL,GetAgentIni()->m_iLocalPort);
if(sockListen == SOCKET_ERROR)
return 1;
GetConsoleWnd(0)->printf("[info] 開始等待連接");
while(1)
{//開始接受連接
struct sockaddr adr;
int iAdrSize= sizeof(adr);
SOCKET sockClient = accept(sockListen,&adr,&iAdrSize);
if(sockClient>0)
{//連接成功后產生處理線程序
LPCSTR pszRmtAddr = inet_ntoa(((sockaddr_in*)&adr)->sin_addr);
char szTemp[200];
sprintf(szTemp,"[info] client connected from %s:%d@%d",pszRmtAddr,ntohs(((sockaddr_in*)&adr)->sin_port),sockClient);
GetConsoleWnd(0)->printf(szTemp);
DWORD dwSeqHandle = GetConnectionIDSeq()->GetNext();
DWORD dwID;
char szProlog[40];
sprintf(szProlog,"%s_netsockcontain",GetAgentName());
CNetSocketContainer *pnsCon= new CNetSocketContainer(sockClient,NULL,(UINT)dwSeqHandle,szProlog);
HANDLE hThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ClientAndServerDataExchange,(LPVOID)pnsCon,0,&dwID);
//
CloseHandle(hThread);
}
}
return 0;
}
void _append_to_file(LPCSTR pszFName,void* pD,int iL)
{//輸出到文件
if(GetAgentIni()->m_iDebugOut != 1)
return;
if(iL <=0)
return;
FILE* pFile=fopen(pszFName,"ab");
if(pFile)
{
int iWrote = fwrite(pD,iL,1,pFile);
fclose(pFile);
GetConsoleWnd(0)->printf("[file] append to %s file write item=%d bytes=%d",pszFName,iWrote,iWrote*iL);
}
else
{
GetConsoleWnd(1)->printf("[error] append to %s file error",pszFName);
}
}
DWORD ClientAndServerDataExchange(CNetSocketContainer* pnsContainer)
{//建立到真正服務器間連接,然后采用輪詢的方式在C、S間轉發數據,每次輪詢延時1S
pnsContainer->AddRefer();
SOCKET sockClient = pnsContainer->m_sockClient;
SOCKET sockServer =SPCreateConnectSocket(GetAgentIni()->m_szServerIP,
GetAgentIni()->m_iServerPort,
(GetAgentIni()->m_szLocalIP.GetLength()!=0)?(LPCSTR)GetAgentIni()->m_szLocalIP:NULL);
if(sockServer != SOCKET_ERROR)
{
GetConsoleWnd(0)->printf("[info] Connect to Server OK %s:%d",GetAgentIni()->m_szServerIP,GetAgentIni()->m_iServerPort);
pnsContainer->m_sockServer = sockServer;
char szTitleCS[32],szTitleSC[32],szFileCS[256],szFileSC[256];
sprintf(szTitleCS,"{%s} C->S",pnsContainer->GetContainerName());
sprintf(szTitleSC,"{%s} S->C",pnsContainer->GetContainerName());
sprintf(szFileCS,"Data\\%d_%s_client_2_server.txt",GetAgentIni()->m_iCurrentSeq,pnsContainer->GetContainerName());
sprintf(szFileSC,"Data\\%d_%s_server_2_client.txt",GetAgentIni()->m_iCurrentSeq,pnsContainer->GetContainerName());
CTCPSocket sClient(pnsContainer->m_sockClient,FALSE);
CTCPSocket sServer(pnsContainer->m_sockServer,FALSE);
while(1)
{
if(SOCKET_ERROR == RecvAndSendBetween2Hosts(pnsContainer,sClient,sServer,szTitleCS,szFileCS))
{//從客戶端接收并發送到服務器
break;
}
if(SOCKET_ERROR == RecvAndSendBetween2Hosts(pnsContainer,sServer,sClient,szTitleSC,szFileSC))
{//從服務器接收并發送到客戶端
break;
}
}
}
else
GetConsoleWnd(1)->printf("[error] Connect to Server error %s:%d",GetAgentIni()->m_szServerIP,GetAgentIni()->m_iServerPort);
GetConsoleWnd(0)->printf("[info] {%s} Exit",pnsContainer->GetContainerName());
pnsContainer->CloseAll();
pnsContainer->ReleaseRefer();
delete pnsContainer;
return 0;
}
int RecvAndSendBetween2Hosts(CNetSocketContainer* pnsContainer,CIPCComm &sFrom,CIPCComm &sTo,LPCSTR pszTitle,LPCSTR pszFile)
{//從第一個Socket收數據,發送到第二個Socket
int iRead=0,iWrote=0;
//接受數據
int iRet = sFrom.TestAndRecv(1,pnsContainer->m_iBufferLen,pnsContainer->m_pbBuffer,iRead);
if(iRet ==SP_ERR_NETWORK || iRet==SP_ERR_GENERAL)
{
GetConsoleWnd(1)->printf("[error] %s Receive Error",pszTitle);
return SOCKET_ERROR;
}
if(iRet ==SP_ERR_SUCCESS || iRet==SP_ERR_NOT_FINISH)
{
GetConsoleWnd(0)->printf("[info] %s Receive OK length=%d",pszTitle,iRead);
//記錄數據
_append_to_file(pszFile,pnsContainer->m_pbBuffer,iRead);
//轉發數據
iRet = sTo.Send(iRead,pnsContainer->m_pbBuffer,iWrote);
if(iRet ==SP_ERR_NETWORK || iRet==SP_ERR_GENERAL)
{
GetConsoleWnd(1)->printf("[error] %s Send Error",pszTitle);
return SOCKET_ERROR;
}
GetConsoleWnd(0)->printf("[info] %s Send OK length=%d",pszTitle,iWrote);
return iWrote;
}
// GetConsoleWnd(0)->printf("%s Send OK length=%d",pszTitle,iWrote);
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -