1994-11-04 16:29:24 +00:00
|
|
|
|
/* Implementation of coder object for remote messaging
|
1996-01-24 03:15:23 +00:00
|
|
|
|
Copyright (C) 1994, 1995, 1996 Free Software Foundation, Inc.
|
1994-11-04 16:29:24 +00:00
|
|
|
|
|
1996-04-17 20:17:45 +00:00
|
|
|
|
Written by: Andrew Kachites McCallum <mccallum@gnu.ai.mit.edu>
|
1996-03-01 15:54:57 +00:00
|
|
|
|
Created: July 1994
|
1994-11-04 16:29:24 +00:00
|
|
|
|
|
1996-05-12 00:56:10 +00:00
|
|
|
|
This file is part of the GNUstep Base Library.
|
1994-11-04 16:29:24 +00:00
|
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This library 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
|
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
|
License along with this library; if not, write to the Free
|
|
|
|
|
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
*/
|
|
|
|
|
|
1996-04-17 15:34:35 +00:00
|
|
|
|
#include <gnustep/base/preface.h>
|
1996-04-17 15:23:00 +00:00
|
|
|
|
#include <gnustep/base/ConnectedCoder.h>
|
|
|
|
|
#include <gnustep/base/CStream.h>
|
|
|
|
|
#include <gnustep/base/Port.h>
|
|
|
|
|
#include <gnustep/base/MemoryStream.h>
|
|
|
|
|
#include <gnustep/base/Connection.h>
|
|
|
|
|
#include <gnustep/base/Proxy.h>
|
1994-11-04 16:29:24 +00:00
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
|
|
#define PTR2LONG(P) (((char*)(P))-(char*)0)
|
|
|
|
|
#define LONG2PTR(L) (((char*)0)+(L))
|
|
|
|
|
|
1996-03-01 15:54:57 +00:00
|
|
|
|
#define DEFAULT_SIZE 256
|
1994-11-04 16:29:24 +00:00
|
|
|
|
|
1996-03-06 13:54:21 +00:00
|
|
|
|
#define CONNECTED_CODER_FORMAT_VERSION 0
|
|
|
|
|
|
1994-11-04 16:29:24 +00:00
|
|
|
|
static BOOL debug_connected_coder = NO;
|
|
|
|
|
|
1996-03-01 15:54:57 +00:00
|
|
|
|
@implementation ConnectedEncoder
|
1994-11-04 16:29:24 +00:00
|
|
|
|
|
1996-03-01 15:54:57 +00:00
|
|
|
|
- (void) writeSignature
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- _initForWritingWithConnection: (Connection*)c
|
1994-11-04 16:29:24 +00:00
|
|
|
|
sequenceNumber: (int)n
|
|
|
|
|
identifier: (int)i
|
|
|
|
|
{
|
1996-03-13 02:26:24 +00:00
|
|
|
|
OutPacket* packet = [[[[c outPort] outPacketClass] alloc]
|
|
|
|
|
initForSendingWithCapacity: DEFAULT_SIZE
|
|
|
|
|
replyInPort: [c inPort]];
|
1996-03-01 15:54:57 +00:00
|
|
|
|
[super initForWritingToStream: packet];
|
|
|
|
|
connection = c;
|
|
|
|
|
sequence_number = n;
|
|
|
|
|
identifier = i;
|
|
|
|
|
[self encodeValueOfCType: @encode(typeof(sequence_number))
|
|
|
|
|
at: &sequence_number
|
|
|
|
|
withName: @"ConnectedCoder sequence number"];
|
|
|
|
|
[self encodeValueOfCType: @encode(typeof(identifier))
|
|
|
|
|
at: &identifier
|
|
|
|
|
withName: @"ConnectedCoder sequence number"];
|
1996-03-06 13:54:21 +00:00
|
|
|
|
return self;
|
1994-11-04 16:29:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
1996-03-01 15:54:57 +00:00
|
|
|
|
+ newForWritingWithConnection: (Connection*)c
|
|
|
|
|
sequenceNumber: (int)n
|
|
|
|
|
identifier: (int)i
|
1994-11-04 16:29:24 +00:00
|
|
|
|
{
|
1996-03-01 15:54:57 +00:00
|
|
|
|
/* Export this method and not the -init... method because eventually
|
|
|
|
|
we may do some caching of old ConnectedEncoder's to speed things up. */
|
|
|
|
|
return [[self alloc] _initForWritingWithConnection: c
|
|
|
|
|
sequenceNumber: n
|
|
|
|
|
identifier: i];
|
1994-11-04 16:29:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
1996-03-01 15:54:57 +00:00
|
|
|
|
- (void) dismiss
|
1994-11-04 16:29:24 +00:00
|
|
|
|
{
|
1996-03-01 15:54:57 +00:00
|
|
|
|
id packet = [cstream stream];
|
1996-03-13 02:26:24 +00:00
|
|
|
|
[[connection outPort] sendPacket: packet];
|
1996-03-01 15:54:57 +00:00
|
|
|
|
if (debug_connected_coder)
|
|
|
|
|
fprintf(stderr, "dismiss 0x%x: #=%d i=%d %d\n",
|
|
|
|
|
(unsigned)self, sequence_number, identifier,
|
|
|
|
|
[packet streamEofPosition]);
|
1995-03-12 19:58:48 +00:00
|
|
|
|
[self release];
|
1994-11-04 16:29:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
1996-03-01 15:54:57 +00:00
|
|
|
|
|
|
|
|
|
/* Access to ivars. */
|
|
|
|
|
|
|
|
|
|
- (int) identifier
|
|
|
|
|
{
|
|
|
|
|
return identifier;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- connection
|
1994-11-04 16:29:24 +00:00
|
|
|
|
{
|
1996-03-01 15:54:57 +00:00
|
|
|
|
return connection;
|
1994-11-04 16:29:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
1996-03-01 15:54:57 +00:00
|
|
|
|
- (unsigned) sequenceNumber
|
1994-11-04 16:29:24 +00:00
|
|
|
|
{
|
1996-03-01 15:54:57 +00:00
|
|
|
|
return sequence_number;
|
1994-11-04 16:29:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
1996-03-01 15:54:57 +00:00
|
|
|
|
|
|
|
|
|
/* Cache the const ptr's in the Connection, not separately for each
|
|
|
|
|
created ConnectedCoder. */
|
|
|
|
|
|
|
|
|
|
- (unsigned) _coderReferenceForConstPtr: (const void*)ptr
|
1994-11-04 16:29:24 +00:00
|
|
|
|
{
|
1996-03-01 15:54:57 +00:00
|
|
|
|
return [connection _encoderReferenceForConstPtr: ptr];
|
1994-11-04 16:29:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
1996-03-01 15:54:57 +00:00
|
|
|
|
- (unsigned) _coderCreateReferenceForConstPtr: (const void*)ptr
|
|
|
|
|
{
|
|
|
|
|
return [connection _encoderCreateReferenceForConstPtr: ptr];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* This is called by Coder's designated object encoder */
|
|
|
|
|
- (void) _doEncodeObject: anObj
|
|
|
|
|
{
|
|
|
|
|
id c = [anObj classForConnectedCoder: self];
|
|
|
|
|
/* xxx Should I also do classname substition here? */
|
|
|
|
|
[self encodeClass: c];
|
|
|
|
|
[c encodeObject: anObj withConnectedCoder: self];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@implementation ConnectedDecoder
|
|
|
|
|
|
|
|
|
|
+ (void) readSignatureFromCStream: (id <CStreaming>) cs
|
|
|
|
|
getClassname: (char *) name
|
|
|
|
|
formatVersion: (int*) version
|
1994-11-04 16:29:24 +00:00
|
|
|
|
{
|
1996-03-06 13:54:21 +00:00
|
|
|
|
const char *classname = class_get_class_name (self);
|
|
|
|
|
strcpy (name, classname);
|
|
|
|
|
*version = CONNECTED_CODER_FORMAT_VERSION;
|
1994-11-04 16:29:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
1996-03-06 13:54:21 +00:00
|
|
|
|
|
1996-03-01 15:54:57 +00:00
|
|
|
|
+ newDecodingWithConnection: (Connection*)c
|
|
|
|
|
timeout: (int) timeout
|
|
|
|
|
{
|
|
|
|
|
ConnectedDecoder *cd;
|
|
|
|
|
id in_port;
|
|
|
|
|
id packet;
|
|
|
|
|
id reply_port;
|
|
|
|
|
|
|
|
|
|
/* Try to get a packet. */
|
|
|
|
|
in_port = [c inPort];
|
|
|
|
|
packet = [in_port receivePacketWithTimeout: timeout];
|
|
|
|
|
if (!packet)
|
|
|
|
|
return nil; /* timeout */
|
|
|
|
|
|
|
|
|
|
/* Create the new ConnectedDecoder */
|
|
|
|
|
cd = [self newReadingFromStream: packet];
|
|
|
|
|
reply_port = [packet replyPort];
|
|
|
|
|
cd->connection = [Connection newForInPort: in_port
|
|
|
|
|
outPort: reply_port
|
|
|
|
|
ancestorConnection: c];
|
|
|
|
|
|
|
|
|
|
/* Decode the ConnectedDecoder's ivars. */
|
|
|
|
|
[cd decodeValueOfCType: @encode(typeof(cd->sequence_number))
|
|
|
|
|
at: &(cd->sequence_number)
|
|
|
|
|
withName: NULL];
|
|
|
|
|
[cd decodeValueOfCType: @encode(typeof(cd->identifier))
|
|
|
|
|
at: &(cd->identifier)
|
|
|
|
|
withName: NULL];
|
|
|
|
|
|
|
|
|
|
if (debug_connected_coder)
|
1996-03-12 19:36:13 +00:00
|
|
|
|
fprintf(stderr, "newDecoding #=%d id=%d\n",
|
|
|
|
|
cd->sequence_number, cd->identifier);
|
|
|
|
|
return cd;
|
|
|
|
|
}
|
|
|
|
|
|
1996-03-13 02:26:24 +00:00
|
|
|
|
+ newDecodingWithPacket: (InPacket*)packet
|
1996-03-12 19:36:13 +00:00
|
|
|
|
connection: (Connection*)c
|
|
|
|
|
{
|
|
|
|
|
ConnectedDecoder *cd;
|
|
|
|
|
id in_port;
|
|
|
|
|
id reply_port;
|
|
|
|
|
|
|
|
|
|
in_port = [c inPort];
|
|
|
|
|
|
|
|
|
|
/* Create the new ConnectedDecoder */
|
|
|
|
|
cd = [self newReadingFromStream: packet];
|
|
|
|
|
reply_port = [packet replyOutPort];
|
|
|
|
|
cd->connection = [Connection newForInPort: in_port
|
|
|
|
|
outPort: reply_port
|
|
|
|
|
ancestorConnection: c];
|
|
|
|
|
|
|
|
|
|
/* Decode the ConnectedDecoder's ivars. */
|
|
|
|
|
[cd decodeValueOfCType: @encode(typeof(cd->sequence_number))
|
|
|
|
|
at: &(cd->sequence_number)
|
|
|
|
|
withName: NULL];
|
|
|
|
|
[cd decodeValueOfCType: @encode(typeof(cd->identifier))
|
|
|
|
|
at: &(cd->identifier)
|
|
|
|
|
withName: NULL];
|
|
|
|
|
|
|
|
|
|
if (debug_connected_coder)
|
1996-03-01 15:54:57 +00:00
|
|
|
|
fprintf(stderr, "newDecoding #=%d id=%d\n",
|
|
|
|
|
cd->sequence_number, cd->identifier);
|
|
|
|
|
return cd;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Cache the const ptr's in the Connection, not separately for each
|
|
|
|
|
created ConnectedCoder. */
|
|
|
|
|
|
|
|
|
|
- (unsigned) _coderCreateReferenceForConstPtr: (const void*)ptr
|
|
|
|
|
{
|
|
|
|
|
return [connection _decoderCreateReferenceForConstPtr: ptr];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (const void*) _coderConstPtrAtReference: (unsigned)xref
|
|
|
|
|
{
|
|
|
|
|
return [connection _decoderConstPtrAtReference: xref];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
1994-11-04 16:29:24 +00:00
|
|
|
|
#if CONNECTION_WIDE_OBJECT_REFERENCES
|
|
|
|
|
|
1996-04-13 19:54:19 +00:00
|
|
|
|
/* xxx We need to think carefully about reference counts, bycopy and
|
1994-11-04 16:29:24 +00:00
|
|
|
|
remote objects before we do this. */
|
|
|
|
|
|
|
|
|
|
/* Some notes:
|
|
|
|
|
|
|
|
|
|
Is it really more efficient to send "retain" messages across the
|
|
|
|
|
wire than to resend the object?
|
|
|
|
|
|
|
|
|
|
How is this related to bycopy objects? Yipes.
|
|
|
|
|
|
|
|
|
|
Never let a Proxy be free'd completely until the connection does
|
|
|
|
|
down? The other connection is assuming we'll keep track of it and
|
|
|
|
|
be able to access it simply by the reference number.
|
|
|
|
|
|
|
|
|
|
Even if this is unacceptable, and we always have to sent enough info
|
|
|
|
|
to be able to recreate the proxy, we still win with the choice of
|
|
|
|
|
+encodeObject:withConnectedCoder because we avoid having
|
|
|
|
|
to keep around the local proxies. */
|
|
|
|
|
|
1996-04-13 19:54:19 +00:00
|
|
|
|
#warning These names need to be updated for the new xref scheme.
|
|
|
|
|
|
|
|
|
|
- (BOOL) _coderReferenceForObject: xref
|
1994-11-04 16:29:24 +00:00
|
|
|
|
{
|
|
|
|
|
if (is_decoding)
|
|
|
|
|
return [connection includesProxyForTarget:xref];
|
|
|
|
|
else
|
|
|
|
|
return [connection includesLocalObject:(id)LONG2PTR(xref)];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- _coderObjectAtReference: (unsigned)xref;
|
|
|
|
|
{
|
|
|
|
|
if (is_decoding)
|
|
|
|
|
return [connection proxyForTarget:xref];
|
|
|
|
|
else
|
|
|
|
|
return (id)LONG2PTR(xref);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) _coderPutObject: anObj atReference: (unsigned)xref
|
|
|
|
|
{
|
|
|
|
|
/* xxx But we need to deal with bycopy's too!!! Not all of the
|
|
|
|
|
"anObj"s are Proxies! */
|
|
|
|
|
if (is_decoding)
|
|
|
|
|
{
|
|
|
|
|
assert([anObj isProxy]);
|
|
|
|
|
assert([anObj targetForProxy] == xref);
|
|
|
|
|
/* This gets done in Proxy +newForRemote:connection:
|
|
|
|
|
[connection addProxy:anObj]; */
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
assert(PTR2LONG(anObj) == xref);
|
|
|
|
|
[connection addLocalObject:anObj];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif /* CONNECTION_WIDE_REFERENCES */
|
|
|
|
|
|
1996-03-01 15:54:57 +00:00
|
|
|
|
|
|
|
|
|
/* Access to ivars. */
|
1994-11-04 16:29:24 +00:00
|
|
|
|
|
|
|
|
|
- (int) identifier
|
|
|
|
|
{
|
|
|
|
|
return identifier;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- connection
|
|
|
|
|
{
|
|
|
|
|
return connection;
|
|
|
|
|
}
|
|
|
|
|
|
1996-03-01 15:54:57 +00:00
|
|
|
|
- replyPort
|
1994-11-04 16:29:24 +00:00
|
|
|
|
{
|
1996-03-01 15:54:57 +00:00
|
|
|
|
return [(id)[cstream stream] replyPort];
|
1994-11-04 16:29:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (unsigned) sequenceNumber
|
|
|
|
|
{
|
|
|
|
|
return sequence_number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) resetConnectedCoder /* xxx rename resetCoder */
|
|
|
|
|
{
|
|
|
|
|
[self notImplemented:_cmd];
|
|
|
|
|
/* prepare the receiver to do it's stuff again,
|
|
|
|
|
save time by doing this instead of free/malloc for each message */
|
|
|
|
|
}
|
|
|
|
|
|
1996-03-01 15:54:57 +00:00
|
|
|
|
- (void) dismiss
|
|
|
|
|
{
|
|
|
|
|
[self release];
|
|
|
|
|
}
|
|
|
|
|
|
1994-11-04 16:29:24 +00:00
|
|
|
|
@end
|
1996-01-26 19:49:48 +00:00
|
|
|
|
|
|
|
|
|
|
1996-01-28 02:07:50 +00:00
|
|
|
|
@implementation NSObject (ConnectedCoderCallbacks)
|
1996-01-26 19:49:48 +00:00
|
|
|
|
|
|
|
|
|
/* By default, Object's encode themselves as proxies across Connection's */
|
1996-03-01 15:54:57 +00:00
|
|
|
|
- classForConnectedCoder: aRmc
|
1996-01-26 19:49:48 +00:00
|
|
|
|
{
|
|
|
|
|
return [[aRmc connection] proxyClass];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* But if any object overrides the above method to return [Object class]
|
|
|
|
|
instead, the Object implementation of the coding method will actually
|
|
|
|
|
encode the object itself, not a proxy */
|
|
|
|
|
+ (void) encodeObject: anObject withConnectedCoder: aRmc
|
|
|
|
|
{
|
1996-03-01 15:54:57 +00:00
|
|
|
|
[anObject encodeWithCoder: aRmc];
|
1996-01-26 19:49:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|