summaryrefslogtreecommitdiff
path: root/Ivy/BufferedSocket.cxx
blob: 3335e099151c17908c725e6463fc70ac50cb176d (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
154
155
156
157
158
159
160
161
162
163
164
165
166
// BufferedSocket.cpp: implementation of the CBufferedSocket class.
//
//////////////////////////////////////////////////////////////////////

#include "IvyStdAfx.h"
#include "BufferedSocket.h"

#define BUFFER_SIZE 4096

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CBufferedSocket::CBufferedSocket()
{
	separator = '\n';
	buffer_size = BUFFER_SIZE;
    buffer = (char*)malloc( buffer_size );
	current_ptr = buffer;
	connected = false;
	InitializeCriticalSection( &m_CritSection );
}

CBufferedSocket::~CBufferedSocket()
{
	free( buffer );
}
void CBufferedSocket::Accept(CBufferedSocket& rConnectedSocket, SOCKADDR* lpSockAddr , int* lpSockAddrLen )
{
	CThreadedSocket::Accept(rConnectedSocket, lpSockAddr ,  lpSockAddrLen );
	rConnectedSocket.connected = true;
}
void CBufferedSocket::OnReceive(int nErrorCode) 
{
    char *ptr_sep;
	char *ptr;
    size_t nb_to_read = 0;
    size_t nb;
		
	SOCKADDR addr;
	size_t len = sizeof( addr  );
	
    /* limitation taille buffer */
    nb_to_read = buffer_size - (current_ptr - buffer );
    if( nb_to_read == 0 )
		{
		buffer_size *= 2; /* twice old size */
		buffer = (char*)realloc( buffer, buffer_size );
		if (!buffer )
		{
		TRACE("HandleSocket Buffer Memory Alloc Error\n");
		exit(0);
		}
		TRACE( "Buffer Limit reached realloc new size %ld\n", buffer_size );
		nb_to_read = buffer_size - (current_ptr - buffer );
		}
	nb = ReceiveFrom( current_ptr, nb_to_read, &addr, &len, 0/*MSG_PARTIAL*/ );
	ASSERT ( memchr( current_ptr, 0, nb) == NULL );
	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;
		}

	current_ptr += nb;
	ptr = buffer;
	while ((ptr_sep = (char*)memchr (ptr, separator,  current_ptr - ptr )))
		{
		*ptr_sep = '\0';
		//TRACE("message %s\n", ptr );
		OnReceive( ptr );
		ptr = ++ptr_sep;
		//ASSERT( ptr < (buffer + buffer_size) );
		}
	if (ptr  < current_ptr )
		{ /* recopie ligne incomplete au debut du buffer */
		len = current_ptr - ptr;
		memcpy (buffer, ptr, len  );
		current_ptr = buffer + len;
		}
		else
		{
		current_ptr = buffer;
		}         


}

void CBufferedSocket::OnReceive( char *line )
{
}
void CBufferedSocket::SetSeparator( char sep )
{
	separator = sep;
}
void CBufferedSocket::OnSend( int nErrorCode )
{
	ivy::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());
	
		size_t lg = msg.length();
		size_t 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();
			switch ( err ){
				case WSAEWOULDBLOCK: // si la file est pleine on sort en silence !
					break;
				case WSAECONNABORTED: // broken pipe on sort en silence
					break;
				default:
					TRACE("CBufferedSocket::OnWakeup error %d Sending buffer %s \n",err,msg.c_str());
					break;
			}
			break;
			}
		}
	} while ( !empty ); 
}

void CBufferedSocket::OnConnect( int nErrorCode )
{
	connected = true;
	StartWriter();
//	TRACE("CBufferedSocket::OnConnect  buffer size %d\n",buf_out.size());
}	
void CBufferedSocket::Send ( const char * data )
{
	//BOOL toBeSignaled;
	EnterCriticalSection( &m_CritSection );
	//toBeSignaled = buf_out.IsEmpty() && connected;
	buf_out.push_back( ivy::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());
//		}
}