-
Notifications
You must be signed in to change notification settings - Fork 4
/
CPConnection.h
205 lines (175 loc) · 6.48 KB
/
CPConnection.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
Copyright [2010] [Richard Bross]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
//! CPConnection.h: interface for the CPConnection class.
#if !defined(AFX_CPCONNECTION_H__34ACB6B3_8B03_11D3_AFA6_00C04F6E1532__INCLUDED_)
#define AFX_CPCONNECTION_H__34ACB6B3_8B03_11D3_AFA6_00C04F6E1532__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif //! _MSC_VER > 1000
#include "CBaseSocket.h"
class CPConnection;
//! Unique signature for each connection
class CConnectionSig
{
public:
CConnectionSig::CConnectionSig()
{
};
~CConnectionSig()
{
};
//! Operators
friend bool operator==(const CConnectionSig& csig1, const CConnectionSig& csig2)
{
if (csig1.pConnection == csig2.pConnection && csig1.ulTimeStamp == csig2.ulTimeStamp)
return (TRUE);
else
return (FALSE);
};
friend bool operator<(const CConnectionSig& csig1, const CConnectionSig& csig2)
{
if (csig1.pConnection < csig2.pConnection)
return (TRUE);
if (csig1.pConnection == csig2.pConnection && csig1.ulTimeStamp < csig2.ulTimeStamp)
return (TRUE);
return (FALSE);
};
friend bool operator>(const CConnectionSig& csig1, const CConnectionSig& csig2)
{
if (csig1.pConnection > csig2.pConnection)
return (TRUE);
if (csig1.pConnection == csig2.pConnection && csig1.ulTimeStamp > csig2.ulTimeStamp)
return (TRUE);
return (FALSE);
};
public:
CPConnection *pConnection;
unsigned long ulTimeStamp;
};
//! CPConnection - base class for a socket connection that can be
//! handled by a CPHandler socket connection handler.
class CPServer;
class CPHandler;
class CPConnectionHandler;
class CPConnection : public CBaseSocket
{
friend CPHandler;
friend CPConnectionHandler;
friend CPServer;
//! Operations
public:
enum
{
CS_BUFFER_SIZE = 0x2000, //! 8K
CS_OUTBUFFER_SIZE = 0x8000 //! 32K
};
CPConnection(CBaseSocket *pOwner, struct sockaddr *pAddr = NULL, int iMaxSendBufferSize = CS_OUTBUFFER_SIZE);
virtual ~CPConnection(); //! No public destructor
enum ECONNECT
{
CONNECT_UNUSED,
CONNECT_PENDING,
CONNECT_ATTEMPT_FAILED,
CONNECT_SUCCESS,
CONNECT_CLOSE_REQUESTED,
CONNECT_DISCONNECTED,
CONNECT_FAILURE
};
virtual BOOL Create(int af, //! Address family. Only tested with AF_INET.
int type, //! Stream or datagram (SOCK_STREAM or SOCK_DRAM)
int protocol, //! Usually 0 for inet sockets
UINT nPort = 0, //! Port
LPCSTR lpAddr = NULL, //! Inet address in the standard xxx.xxx.xxx.xxx format
BOOL bReuse = FALSE, //! Allow reuse of this socket
BOOL bBind = TRUE, //! Default to bind the socket. Not needed if this is the result of an ::accept
int iLinger = -1); //! Removes from handler queue by closing socket
//! Terminate a connection and close the socket
virtual void TerminateConnection();
//! Return the time the connection was initiated
virtual time_t GetTimeConnected();
//! Get socket address
virtual struct sockaddr_in GetSocketAddress();
//! Asynchronously close the socket. If a handler hasn't
//! been assigned, this calls TerminateConnection.
//! Otherwise, the handler waits until the send buffer is
//! clear and then closes the socket.
virtual void AsyncTerminateConnection();
//! Get connection status
ECONNECT GetConnectionStatus();
//! Has all data submitted by send been transferred?
//! Only valid after AsyncTerminateConnection has been called.
BOOL IsSendComplete();
protected:
//! Receive the data to a socket
virtual int ReceiveData();
//! Send data from a socket
virtual int SendData();
//! Send data. If iMaxSendBufferSize is exceeded, the return will be FALSE,
//! and the sender must wait until some data is sent before proceeding.
virtual BOOL SendDataOut(LPSTR lpData, int nPacketSize, int nTotalLength);
//! Pure virtual function to process data
virtual BOOL ProcessData(unsigned char *lpData, int iLen) = 0;
//! Get handshake. Return TRUE if ptr and int filled
//! This is sent when a connection is established.
//! Redefine to have server send a connection message.
virtual BOOL GetHandshake(unsigned char **ucHS, int &iLen)
{
return (FALSE);
};
//! When removed from the handler, this gets called
virtual void Disconnected()
{
return;
};
//! Unprotected close socket
virtual void UnprotectedCloseSocket();
protected:
//! Called by the handler itself when it accepts the connection
virtual void SetHandler(CPHandler *pHandler);
//! Called by SetHandler to indicate that a connection is about about to be handled
//! SetHandler is called within CPHandler::AddConnection
virtual int HandleConnection(void *pParm = NULL)
{
return 0;
};
public:
BOOL bRecvPause;
protected:
CPHandler *cPHandler;
int iMaxSendBuffer;
//! Input buffer
unsigned char ucBuffer[CS_BUFFER_SIZE];
//! Output buffer
unsigned char *pOutBuffer;
//! Current size of the output buffer
int iOBSize;
//! What's left to send in the out buffer
int iLeftToSend;
//! Index of bytes to send
int iSendIndex;
CBaseSocket *cOwner;
time_t tConnected;
struct sockaddr_in sAddr;
//! There is data to be written
BOOL bSendData;
//! No room left in send buffer. Block until some sending occurs
BOOL bSendBufferHighWaterMark;
ECONNECT eConnected;
CConnectionSig cSignature;
//! Timestamp of when a close was requested
long lCloseRequestTS;
int iTotalRecvd;
int iTotalSent;
int iTotalSendData;
};
#endif //! !defined(AFX_CPCONNECTION_H__34ACB6B3_8B03_11D3_AFA6_00C04F6E1532__INCLUDED_)