-
Notifications
You must be signed in to change notification settings - Fork 9
/
TCPSocket.h
173 lines (138 loc) · 4.52 KB
/
TCPSocket.h
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Remote Desktop System - remote controlling of multiple PC's
// Copyright (C) 2000-2009 GravyLabs LLC
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#pragma once
#include <set>
#include "afxsock.h"
#include "Packet.h"
#include "Common.h"
#define WM_ACCEPTCONN (WM_APP + 1)
#define WM_MAKECONN (WM_APP + 2)
#define WM_RECEIVEDATA (WM_APP + 3)
#define WM_CLOSECONN (WM_APP + 4)
// TCP stream socket class
class CTCPSocket : public CAsyncSocket
{
typedef CAsyncSocket baseclass;
private:
CTCPSocket(const CTCPSocket & rhs); // no
void operator=(const CTCPSocket & rhs); // no
public:
CTCPSocket();
virtual ~CTCPSocket();
BOOL Create(int nSocketPort = 0);
BOOL Connect(LPCTSTR lpszHostAddress,UINT nHostPort);
void SetParent(HWND hWndParent);
BOOL ShutDown(int nHow = CSocket::both);
BOOL InitTP();
// Helper to send a packet
CTCPSocket & operator << (CPacket & rhs);
// Helper to receive a packet
CTCPSocket & operator >> (CPacket & rhs);
// Helper for sorting
bool operator < (const CTCPSocket & rhs);
protected:
// Low-level socket event callbacks, reposted to parent via WM_APP messages
virtual void OnAccept(int nErrorCode);
virtual void OnConnect(int nErrorCode);
virtual void OnReceive(int nErrorCode);
virtual void OnClose(int nErrorCode);
// Low-level methods to send and receieve data over the socket
virtual int Send(const void * lpBuf,int nBufLen,int nFlags = 0);
virtual int Receive(void * lpBuf,int nBufLen,int nFlags = 0);
private:
LPFN_TRANSMITPACKETS TransmitPackets;
BOOL m_bListening;
HWND m_hWndParent;
};
////////////////////////////////////////////////////////////////////////////
// Window sink message class
class CSocketWndSink : public CWnd
{
// Construction
public:
CSocketWndSink(HWND hWndParent = NULL);
~CSocketWndSink();
void Connect(CString csIp,CString csPort);
BOOL IsConnected();
void ShutDown();
// Generated message map functions
protected:
//{{AFX_MSG(CSocketWndSink)
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnPaint();
//}}AFX_MSG
protected:
// The parent dialog
HWND m_hWndParent;
// The client connectiun
CTCPSocket m_Socket;
BOOL m_bConnected;
DECLARE_MESSAGE_MAP()
// Handled socket event callbacks
afx_msg LRESULT OnMakeConn(WPARAM wParam,LPARAM lParam);
afx_msg LRESULT OnReceiveData(WPARAM wParam,LPARAM lParam);
afx_msg LRESULT OnCloseConn(WPARAM wParam,LPARAM lParam);
};
// Connection to the server that receives DIB updates
class CSocketWndSinkThread : public CWinThread
{
DECLARE_DYNCREATE(CSocketWndSinkThread)
CSocketWndSinkThread();
public:
CSocketWndSinkThread(HANDLE * phWork,HWND hWndParent,CString csIp,CString csPort);
virtual ~CSocketWndSinkThread();
virtual BOOL InitInstance();
virtual BOOL PumpMessage();
virtual int ExitInstance();
BOOL IsConnected();
// Event synchronization
public:
HANDLE m_hPumpMessage;
// Intialization
private:
bool m_bPumpMessage;
protected:
HANDLE * m_phWork;
HWND m_hWndParent;
CString m_csIp;
CString m_csPort;
CSocketWndSink * m_pSocketWnd;
protected:
DECLARE_MESSAGE_MAP()
afx_msg void OnConnectServer(WPARAM wParam,LPARAM lParam);
afx_msg void OnEndThread(WPARAM wParam,LPARAM lParam);
};
////////////////////////////////////////////////////////////////////////////
// Socket message event sink
class CSocketEvent : public CWnd
{
// Construction
public:
CSocketEvent(CWinThread * pThread);
~CSocketEvent();
BOOL IsClosed();
// Generated message map functions
protected:
//{{AFX_MSG(CSocketEvent)
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnPaint();
//}}AFX_MSG
protected:
DECLARE_MESSAGE_MAP()
// Socket events
afx_msg LRESULT OnReceiveData(WPARAM wParam,LPARAM lParam);
afx_msg LRESULT OnCloseConn(WPARAM wParam,LPARAM lParam);
// Back pointers
CWinThread * m_pThread;
BOOL m_bClosed;
};