summaryrefslogtreecommitdiff
path: root/Ivy/BufferedSocket.cxx
blob: 2e15cab1a260e833690a51cea007a1b9496e17ee (plain)
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
// BufferedSocket.cpp: implementation of the CBufferedSocket class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "BufferedSocket.h"


//////////////////////////////////////////////////////////////////////
// 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, 0/*MSG_PARTIAL*/ );
	if ( nb == SOCKET_ERROR )
		{
		int err = this->GetLastError();
		if ( err != WSAESHUTDOWN ) // shutdown by remote side ?
			TRACE("error Receive %d socket %d\n",this->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::OnSend( int nErrorCode )
{
	string msg;
	bool empty;
	
	// 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 );
	if ( !empty )
		{
//		TRACE("CBufferedSocket::OnSend Sending buffer %s\n",msg.c_str());
	
		int lg = msg.length();
		int sent = CThreadedSocket::Send( msg.c_str(), lg );
		if ( sent == lg )
			{ // emission correcte on enleve le msg de la file 
			EnterCriticalSection( &m_CritSection );
			buf_out.pop_front();
			LeaveCriticalSection( &m_CritSection );	
			}
			else
			{ // erreur 
			int err = this->GetLastError();
			if ( err == WSAEWOULDBLOCK ) // si la file est pleine on sort en silence !
				{
				}
				else 
				TRACE("CBufferedSocket::OnWakeup error %d Sending buffer %s \n",err,msg);
			break;
			}
		}
	} while ( !empty ); 
}

void CBufferedSocket::OnConnect( int nErrorCode )
{
	connected = true;
	StartWriter();
//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 = SignalWriter();
		if ( !ok ) TRACE( "CBufferedSocket::SignalWriter  Error %d\n", this->GetLastError());
//		}
}