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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2000, 2010 Oracle and/or its affiliates.
*
* OpenOffice.org - a multi-platform office productivity suite
*
* This file is part of OpenOffice.org.
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* OpenOffice.org 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 Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
*
************************************************************************/
// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_automation.hxx"
/*************************************************************************
*
* ATTENTION
* This file is intended to work inside and outside the StarOffice environment.
* Only adaption of file commtypes.hxx should be necessary. Else it is a bug!
*
************************************************************************/
#include "packethandler.hxx"
#include <automation/commtypes.hxx>
#include <automation/commdefines.hxx>
#include "communiio.hxx"
#include <osl/diagnose.h>
/**
Forces switch to multichannel headers even for old communication Method
**/
#define FORCE_MULTI_CHANNEL_HEADERS
PacketHandler::PacketHandler( ITransmiter* pTransmitter_, IReceiver* pReceiver_, comm_BOOL bMC )
: pTransmitter( pTransmitter_ )
, pReceiver( pReceiver_ )
, bMultiChannel( bMC )
{
}
unsigned char PacketHandler::CalcCheckByte( comm_UINT32 nBytes )
{
comm_UINT16 nRes = 0;
nRes += HIBYTE( HIWORD( nBytes ) ) ^ 0xf0;
nRes += LOBYTE( HIWORD( nBytes ) ) ^ 0x0f;
nRes += HIBYTE( LOWORD( nBytes ) ) ^ 0xf0;
nRes += LOBYTE( LOWORD( nBytes ) ) ^ 0x0f;
nRes ^= HIBYTE( nRes );
return LOBYTE( nRes );
}
#define READ_SOCKET( pBuffer, nLength )\
if ( !bWasError )\
{\
bWasError |= pReceiver->ReceiveBytes( pBuffer, nLength ) != C_ERROR_NONE;\
}
#define READ_SOCKET_LEN( pBuffer, nLength, nTotal )\
READ_SOCKET( pBuffer, nLength );\
if ( !bWasError )\
{nTotal += nLength;}
comm_BOOL PacketHandler::ReceiveData( void* &pData, comm_UINT32 &nLen )
{
DBG_ASSERT( !pData, "pData should be NULL -> memory leak" );
nLen = 0;
pData = NULL;
comm_BOOL bWasError = sal_False;
comm_BOOL bForceMultiChannelThisPacket = sal_False;
if ( pReceiver )
{
comm_UINT32 nBytes = 0;
nReceiveProtocol = CM_PROTOCOL_OLDSTYLE;
nReceiveHeaderType = CH_NoHeader;
READ_SOCKET( &nBytes, sizeof(nBytes) )
if ( bWasError )
return sal_False;
if ( 0xFFFFFFFF == nBytes ) // Expliziter Request f�r dieses Datenpaket auf MultiChannel umzuschalten
{
READ_SOCKET( &nBytes, sizeof(nBytes) )
if ( bWasError )
return sal_False;
bForceMultiChannelThisPacket = sal_True;
}
nBytes = NETDWORD( nBytes );
if ( bMultiChannel || bForceMultiChannelThisPacket )
{
comm_UINT32 nReadSoFar = 0;
comm_UINT32 nHeaderReadSoFar = 0;
// Pr�fbyte f�r L�ngenangabe
unsigned char nLenCheck = 0;
READ_SOCKET_LEN( &nLenCheck, 1, nReadSoFar );
// Stimmt das Pr�fbyte?
bWasError |= nLenCheck != CalcCheckByte( nBytes );
comm_UINT16 nHeaderBytes;
READ_SOCKET_LEN( &nHeaderBytes, 2, nReadSoFar );
nHeaderBytes = NETWORD( nHeaderBytes );
// reicht der Header �ber das Ende hinaus?
bWasError |= !(nBytes >= nReadSoFar + nHeaderBytes);
READ_SOCKET_LEN( &nReceiveHeaderType, 2, nHeaderReadSoFar );
nReceiveHeaderType = NETWORD( nReceiveHeaderType );
switch ( nReceiveHeaderType )
{
case CH_SimpleMultiChannel:
{
READ_SOCKET_LEN( &nReceiveProtocol, 2, nHeaderReadSoFar );
nReceiveProtocol = NETWORD( nReceiveProtocol );
}
break;
case CH_Handshake:
{
}
break;
default:
{
OSL_FAIL("Unbekannter Headertyp in der Kommunikation");
bWasError = sal_True;
}
}
if ( bWasError )
return sal_False;
/// L�ngen anpassen und ggf restheader �berlesen.
while ( nHeaderBytes > nHeaderReadSoFar )
{
unsigned char nDummy;
READ_SOCKET_LEN( &nDummy, 1, nHeaderReadSoFar );
}
nReadSoFar += nHeaderReadSoFar;
nBytes -= nReadSoFar;
}
/* @@@ Notes @@@
*
* 1) a 'void*' allocated via 'new char[]' is always deallocated
* via plain 'delete()', not via array 'delete[]()'; it's just
* raw memory.
*
* 2) as the caller of this routine later-on changes ownership
* of 'pData' via 'SvMemoryStream::SetBuffer()' (in 'simplecm.cxx',
* 'SimpleCommunicationLinkViaSocket::DoReceiveDataStream()'),
* the allocator used here for 'void* pData' must match the
* deallocator used in 'SvMemoryStream::FreeMemory()', i.e.
* '::operator delete()'.
*/
pData = ::operator new(nBytes);
READ_SOCKET( pData, nBytes )
if ( bWasError )
{
::operator delete(pData), pData = 0;
return sal_False;
}
nLen = nBytes;
}
else
bWasError = sal_True;
return !bWasError;
}
/*#define WRITE_SOCKET( pBuffer, nLength )\
if ( !bWasError )\
bWasError |= !pStreamSocket || (pStreamSocket->write( pBuffer, nLength ) != nLength)*/
#define WRITE_SOCKET( pBuffer, nLength )\
if ( !bWasError )\
{bWasError |= pTransmitter->TransferBytes( pBuffer, nLength ) != C_ERROR_NONE;}
comm_BOOL PacketHandler::TransferData( const void* pData, comm_UINT32 nLen, CMProtocol nProtocol )
{
comm_UINT32 nBuffer = nLen;
comm_BOOL bWasError = sal_False;
#ifndef FORCE_MULTI_CHANNEL_HEADERS
if ( bMultiChannel )
#endif
nBuffer += 1+2+2+2; // f�r einen CH_SimpleMultiChannel
#ifdef FORCE_MULTI_CHANNEL_HEADERS
if ( !bMultiChannel )
{
comm_UINT32 n32;
n32 = 0xffffffff; // Umschalten auf MultiChannel
n32 = NETDWORD( n32 );
WRITE_SOCKET( &n32, 4 );
}
#endif
comm_UINT32 nNetworkBuffer = NETDWORD( nBuffer );
WRITE_SOCKET( &nNetworkBuffer, sizeof(nNetworkBuffer) );
#ifndef FORCE_MULTI_CHANNEL_HEADERS
if ( bMultiChannel )
#endif
{
comm_UINT16 n16;
unsigned char c;
c = CalcCheckByte( nBuffer );
WRITE_SOCKET( &c, 1 );
n16 = 4; // L�nge des Headers f�r einen CH_SimpleMultiChannel
n16 = NETWORD( n16 );
WRITE_SOCKET( &n16, 2 );
n16 = CH_SimpleMultiChannel; // Typ des Headers
n16 = NETWORD( n16 );
WRITE_SOCKET( &n16, 2 );
nProtocol = NETWORD( nProtocol );
WRITE_SOCKET( &nProtocol, 2 );
}
WRITE_SOCKET( pData, nLen );
return !bWasError;
}
comm_BOOL PacketHandler::SendHandshake( HandshakeType aHandshakeType, const void* pData, comm_UINT32 nLen )
{
comm_BOOL bWasError = sal_False;
comm_UINT32 nBuffer = 0;
// if ( pMyManager->IsMultiChannel() ) Wir senden immer FFFFFFFF vorweg -> immer MultiChannel (Oder GPF bei �lteren)
nBuffer += 1+2+2; // f�r einen CH_Handshake
nBuffer += 2; // f�r den Typ des Handshakes
switch ( aHandshakeType )
{
case CH_REQUEST_HandshakeAlive:
nBuffer += 0; // Keine extra Daten
break;
case CH_RESPONSE_HandshakeAlive:
nBuffer += 0; // Keine extra Daten
break;
case CH_REQUEST_ShutdownLink:
nBuffer += 0; // Keine extra Daten
break;
case CH_ShutdownLink:
nBuffer += 0; // Keine extra Daten
break;
case CH_SUPPORT_OPTIONS:
nBuffer += 2 ; // one word extradata for options
break;
case CH_SetApplication:
nBuffer += 0 ; // one word extradata for options
break;
default:
OSL_FAIL("Unknown HandshakeType");
}
if ( pData )
nBuffer += nLen; // Extra data in Buffer
comm_UINT32 n32;
n32 = 0xffffffff; // Umschalten auf MultiChannel
n32 = NETDWORD( n32 );
WRITE_SOCKET( &n32, 4 );
comm_UINT32 nNetworkBuffer = NETDWORD( nBuffer );
WRITE_SOCKET( &nNetworkBuffer, sizeof(nNetworkBuffer) );
comm_UINT16 n16;
unsigned char c;
c = CalcCheckByte( nBuffer );
WRITE_SOCKET( &c, 1 );
n16 = 2; // L�nge des Headers f�r einen CH_Handshake
n16 = NETWORD( n16 );
WRITE_SOCKET( &n16, 2 );
n16 = CH_Handshake; // Typ des Headers
n16 = NETWORD( n16 );
WRITE_SOCKET( &n16, 2 );
n16 = aHandshakeType; // Typ des Handshakes
n16 = NETWORD( n16 );
WRITE_SOCKET( &n16, 2 );
switch ( aHandshakeType )
{
case CH_SUPPORT_OPTIONS:
n16 = OPT_USE_SHUTDOWN_PROTOCOL;
n16 = NETWORD( n16 );
WRITE_SOCKET( &n16, 2 );
break;
}
if ( pData )
WRITE_SOCKET( pData, nLen );
return !bWasError;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|