This repository has been archived by the owner on Jan 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SocketManager.cpp
158 lines (135 loc) · 3.83 KB
/
SocketManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// SocketManager.cpp: implementation of the CSocketManager class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <atlconv.h>
#include "SocketManager.h"
#include "NetKeeperHeader.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
/*
const UINT EVT_CONSUCCESS = 0x0000; // Connection established
const UINT EVT_CONFAILURE = 0x0001; // General failure - Wait Connection failed
const UINT EVT_CONDROP = 0x0002; // Connection dropped
const UINT EVT_ZEROLENGTH = 0x0003; // Zero length message
*/
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CSocketManager::CSocketManager()
: m_pMsgCtrl(NULL)
{
}
CSocketManager::~CSocketManager()
{
}
void CSocketManager::DisplayData(const LPBYTE lpData, DWORD dwCount, const SockAddrIn& sfrom)
{
CString strData;
memcpy(strData.GetBuffer(dwCount), A2CT((LPSTR)lpData), dwCount);
strData.ReleaseBuffer();
if (!sfrom.IsNull())
{
LONG uAddr = sfrom.GetIPAddr();
BYTE* sAddr = (BYTE*) &uAddr;
short nPort = ntohs( sfrom.GetPort() ); // show port in host format...
CString strAddr;
// Address is stored in network format...
strAddr.Format(_T("%u.%u.%u.%u (%d)>"),
(UINT)(sAddr[0]), (UINT)(sAddr[1]),
(UINT)(sAddr[2]), (UINT)(sAddr[3]), nPort);
strData = strAddr + strData;
}
AppendMessage( strData );
}
void CSocketManager::AppendMessage(LPCTSTR strText )
{
if (NULL == m_pMsgCtrl)
return;
/*
if (::IsWindow( m_pMsgCtrl->GetSafeHwnd() ))
{
int nLen = m_pMsgCtrl->GetWindowTextLength();
m_pMsgCtrl->SetSel(nLen, nLen);
m_pMsgCtrl->ReplaceSel( strText );
}
*/
HWND hWnd = m_pMsgCtrl->GetSafeHwnd();
DWORD dwResult = 0;
if (SendMessageTimeout(hWnd, WM_GETTEXTLENGTH, 0, 0, SMTO_NORMAL, 1000L, &dwResult) != 0)
{
int nLen = (int) dwResult;
if (SendMessageTimeout(hWnd, EM_SETSEL, nLen, nLen, SMTO_NORMAL, 1000L, &dwResult) != 0)
{
if (SendMessageTimeout(hWnd, EM_REPLACESEL, FALSE, (LPARAM)strText, SMTO_NORMAL, 1000L, &dwResult) != 0)
{
}
}
}
}
void CSocketManager::SetMessageWindow(CEdit* pMsgCtrl)
{
m_pMsgCtrl = pMsgCtrl;
}
void CSocketManager::OnDataReceived(const LPBYTE lpBuffer, DWORD dwCount)
{
LPBYTE lpData = lpBuffer;
SockAddrIn origAddr;
stMessageProxy msgProxy;
if (IsSmartAddressing())
{
dwCount = __min(sizeof(msgProxy), dwCount);
memcpy(&msgProxy, lpBuffer, dwCount);
origAddr = msgProxy.address;
if (IsServer())
{
// broadcast message to all
msgProxy.address.sin_addr.s_addr = htonl(INADDR_BROADCAST);
WriteComm((const LPBYTE)&msgProxy, dwCount, 0L);
}
dwCount -= sizeof(msgProxy.address);
lpData = msgProxy.byData;
}
// Display data to message list
DisplayData( lpData, dwCount, origAddr );
CString tempStr = (LPSTR)lpData;
if (tempStr.Find("====")!=0)
{
CWnd* pParent = m_pMsgCtrl->GetParent();;
pParent = pParent->GetParent();
pParent->SendMessage( WM_SHOWCHATICO,(WPARAM)(LPCTSTR)tempStr,0);
}
}
///////////////////////////////////////////////////////////////////////////////
// OnEvent
// Send message to parent window to indicate connection status
void CSocketManager::OnEvent(UINT uEvent)
{
if (NULL == m_pMsgCtrl)
return;
CWnd* pParent = m_pMsgCtrl->GetParent();
if (!::IsWindow( pParent->GetSafeHwnd()))
return;
switch( uEvent )
{
case EVT_CONSUCCESS:
AppendMessage( _T("Connection Established\r\n") );
break;
case EVT_CONFAILURE:
AppendMessage( _T("Connection Failed\r\n") );
break;
case EVT_CONDROP:
AppendMessage( _T("Connection Abandonned\r\n") );
break;
case EVT_ZEROLENGTH:
AppendMessage( _T("Zero Length Message\r\n") );
break;
default:
TRACE("Unknown Socket event\n");
break;
}
pParent->PostMessage( WM_UPDATE_CONNECTION, uEvent, (LPARAM) this);
}