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
|
// BufferedSocket.cpp: implementation of the CBufferedSocket class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "BufferedSocket.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//IMPLEMENT_DYNAMIC(CBufferedSocket, CThreadedSocket)
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CBufferedSocket::CBufferedSocket()
{
separator = '\n';
ptr = buf;
connected = false;
InitializeCriticalSection( &m_CritSection );
}
CBufferedSocket::~CBufferedSocket()
{
}
void CBufferedSocket::Accept(CBufferedSocket& rConnectedSocket, SOCKADDR* lpSockAddr , int* lpSockAddrLen )
{
CThreadedSocket::Accept(rConnectedSocket, lpSockAddr , lpSockAddrLen );
rConnectedSocket.connected = true;
}
void CBufferedSocket::OnReceive(int nErrorCode)
{
char *ptr_nl;
long nb_to_read = 0;
long nb;
SOCKADDR addr;
int len = sizeof( addr );
/* limitation taille buffer */
nb_to_read = MAX_BUFFER - ( ptr - buf );
if( nb_to_read == 0 )
{
TRACE("Erreur message trop long sans LF\n");
ptr = buf;
return;
}
nb = ReceiveFrom( ptr, nb_to_read, &addr, &len, MSG_PARTIAL );
if ( nb == SOCKET_ERROR )
{
int err = GetLastError();
if ( err != WSAESHUTDOWN ) // shutdown by remote side ?
TRACE("error Receive %d socket %d\n",GetLastError(),m_hSocket);
Close();
return;
}
if ( nb == 0 ) // shutdown by remote side ?
{
Close();
return;
}
ptr += nb;
ASSERT( ptr < (buf +sizeof( buf )));
*ptr = '\0';
ptr = buf;
while( (ptr_nl = strchr( ptr, '\n' )))
{
*ptr_nl = '\0';
//TRACE("message %s\n", ptr );
OnReceive( ptr );
ptr = ++ptr_nl;
}
if ( *ptr != '\0' )
{ /* recopie ligne incomplete au debut du buffer */
strcpy( buf, ptr );
ptr = buf + strlen(buf);
}
else
{
ptr = buf;
}
}
void CBufferedSocket::OnReceive( char *line )
{
}
void CBufferedSocket::SetSeparator( char sep )
{
separator = sep;
}
void CBufferedSocket::OnWakeup()
{
string msg;
bool empty;
try
{
// on essaye de garder la section critique a l'interieur de la boucle
// pour permettre l'entrelacement avec la partie emetrice
do
{
EnterCriticalSection( &m_CritSection );
empty = buf_out.empty();
if ( !empty ) msg = buf_out.front();
LeaveCriticalSection( &m_CritSection );
//TRACE("CBufferedSocket::OnWakeup Sending buffer %s\n",msg.c_str());
if ( !empty )
{
int lg = msg.length();
CThreadedSocket::Send( msg.c_str(), lg );
EnterCriticalSection( &m_CritSection );
buf_out.pop_front();
LeaveCriticalSection( &m_CritSection );
}
} while ( !empty );
}
catch ( CThreadedSocketException* e )
{
int err = e->GetError();
if ( err == WSAEWOULDBLOCK )
{
// put back the buffer, preserving the order
//EnterCriticalSection( &m_CritSection );
//buf_out.push_front( msg);
//LeaveCriticalSection( &m_CritSection );
}
else TRACE("CBufferedSocket::OnWakeup error %d Sending buffer %s \n",err,msg);
}
}
void CBufferedSocket::OnSend( int nErrorCode )
{
OnWakeup();
}
void CBufferedSocket::OnConnect( int nErrorCode )
{
connected = true;
OnWakeup();
//TRACE("CBufferedSocket::OnConnect buffer empty %d\n",buf_out.IsEmpty());
}
void CBufferedSocket::Send ( const char * data )
{
//BOOL toBeSignaled;
EnterCriticalSection( &m_CritSection );
//toBeSignaled = buf_out.IsEmpty() && connected;
buf_out.push_back( string(data) );
LeaveCriticalSection( &m_CritSection );
//TRACE("CBufferedSocket::Send Adding buffer to send count %d\n",buf_out.size());
if ( connected )
{
// BOOL ok = WSASetEvent( m_hEvent[1] );
// if ( !ok ) TRACE( "CBufferedSocket::Send Error SetEvent %d\n", WSAGetLastError());
}
}
|