From 8366c150f4b0d59628d6d398a5ba3642ac81fa89 Mon Sep 17 00:00:00 2001 From: Ray Greenwell Date: Mon, 13 Feb 2006 22:07:50 +0000 Subject: [PATCH] Progress on the presents client. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3848 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../threerings/presents/net/AuthRequest.as | 40 ++++++++++++++ .../presents/net/DownstreamMessage.as | 25 +++++++++ .../threerings/presents/net/PingRequest.as | 38 +++++++++++++ .../threerings/presents/net/PongResponse.as | 54 +++++++++++++++++++ .../presents/net/UpstreamMessage.as | 48 +++++++++++++++++ src/as/com/threerings/util/JavaConstants.as | 7 +++ src/as/com/threerings/util/long.as | 10 +++- 7 files changed, 220 insertions(+), 2 deletions(-) create mode 100644 src/as/com/threerings/presents/net/AuthRequest.as create mode 100644 src/as/com/threerings/presents/net/DownstreamMessage.as create mode 100644 src/as/com/threerings/presents/net/PingRequest.as create mode 100644 src/as/com/threerings/presents/net/PongResponse.as create mode 100644 src/as/com/threerings/presents/net/UpstreamMessage.as create mode 100644 src/as/com/threerings/util/JavaConstants.as diff --git a/src/as/com/threerings/presents/net/AuthRequest.as b/src/as/com/threerings/presents/net/AuthRequest.as new file mode 100644 index 000000000..74849856c --- /dev/null +++ b/src/as/com/threerings/presents/net/AuthRequest.as @@ -0,0 +1,40 @@ +package com.threerings.presents.net { + +import com.threerings.io.ObjectInputStream; +import com.threerings.io.ObjectOutputStream; + +public class AuthRequest extends UpstreamMessage +{ + public function AuthRequest () + { + super(); + } + + public function AuthRequest (creds :Credentials, version :String) + { + super(); + _creds = creds; + _version = version; + } + + // documentation inherited + public function writeObject (out :ObjectOutputStream) :void + { + super.writeObject(out); + + out.writeField(_creds); + out.writeField(_version); + } + + // documentation inherited + public function readObject (ins :ObjectInputStream) :void + { + super.readObject(ins); + _creds = ins.readField(Credentials); + _version = ins.readField(String); + } + + protected var _creds :Credentials; + protected var _version :String; +} +} diff --git a/src/as/com/threerings/presents/net/DownstreamMessage.as b/src/as/com/threerings/presents/net/DownstreamMessage.as new file mode 100644 index 000000000..7d810f2f8 --- /dev/null +++ b/src/as/com/threerings/presents/net/DownstreamMessage.as @@ -0,0 +1,25 @@ +package com.threerings.presents.net { + +import com.threerings.io.Streamable; + +public class DownstreamMessage + implements Streamable +{ + /** The message id of the upstream message with which this downstream + * message is associated (or -1 if it is not associated with any + * upstream message). */ + public var messageId :int = -1; + + // documentation inherited from interface Streamable + public function writeObject (out :ObjectOutputStream) :void + { + out.writeShort(messageId); + } + + // documentation inherited from interface Streamable + public function readObject (ins :ObjectInputStream) :void + { + messageId = ins.readShort(); + } +} +} diff --git a/src/as/com/threerings/presents/net/PingRequest.as b/src/as/com/threerings/presents/net/PingRequest.as new file mode 100644 index 000000000..05c9de5da --- /dev/null +++ b/src/as/com/threerings/presents/net/PingRequest.as @@ -0,0 +1,38 @@ +package com.threerings.presents.net { + +import com.threerings.io.ObjectOutputStream; + +public class PingRequest extends UpstreamMessage +{ + /** The number of milliseconds of idle upstream that are allowed to elapse + * before the client sends a ping message to the server to let it + * know that we're still alive. */ + public static const PING_INTERVAL :int = 60 * 1000; + + public function PingRequest () + { + super(); + } + + public function getPackStamp () :Number + { + return _packStamp; + } + + // documentation inherited + public function writeObject (out :ObjectOutputStream) + { + _packStamp = new Date().getTime(); + super.writeObject(out); + } + + // documentation inherited + public function readObject (ins :ObjectInputStream) + { + trace("read PingRequest on the client?"); + } + + /** A time stamp obtained when we serialize this object. */ + protected var _packStamp :Number; +} +} diff --git a/src/as/com/threerings/presents/net/PongResponse.as b/src/as/com/threerings/presents/net/PongResponse.as new file mode 100644 index 000000000..08648d5fc --- /dev/null +++ b/src/as/com/threerings/presents/net/PongResponse.as @@ -0,0 +1,54 @@ +package com.threerings.presents.net { + +import com.threerings.util.long; + +import com.threerings.io.ObjectInputStream; +import com.threerings.io.ObjectOutputStream; +import com.threerings.io.Streamable; + +public class PongResponse extends DownstreamMessage +{ + public function PongResponse () + { + super(); + } + + public function getPackStamp () :long + { + return _packStamp; + } + + public function getProcessDelay () :int + { + return _processDelay; + } + + public function getUnpackStamp () :Number + { + return _unpackStamp; + } + + public function writeObject (out :ObjectOutputStream) + { + trace("write a pong on the client??"); + } + + public function readObject (ins :ObjectInputStream) + { + _unpackStamp = new Date().getTime(); + super.readObject(ins); + + // TODO: Figure out how we're really going to cope with longs + _packStamp = new long(); + _packStamp.readObject(ins); + + _processDelay = ins.readInt(); + } + + protected var _packStamp :long; + + protected var _processDelay :int; + + protected var _unpackStamp :Number; +} +} diff --git a/src/as/com/threerings/presents/net/UpstreamMessage.as b/src/as/com/threerings/presents/net/UpstreamMessage.as new file mode 100644 index 000000000..f9c352861 --- /dev/null +++ b/src/as/com/threerings/presents/net/UpstreamMessage.as @@ -0,0 +1,48 @@ +package com.threerings.presents.net { + +import com.threerings.io.Streamable; + +public class UpstreamMessage + implements Streamable +{ + /** This is a unique (within the context of a reasonable period + * of time) identifier assigned to each upstream message. The message ids + * are used to correlate a downstream response message to the + * appropriate upstream request message. */ + public var messageId :int; + + /** + * Construct an upstream message. + */ + public function UpstreamMessage () + { + // automatically generate a valid message id + this.messageId = nextMessageId(); + } + + // documentation inherited from interface Streamable + public function writeObject (out :ObjectOutputStream) :void + { + out.writeShort(messageId); + } + + // documentation inherited from interface Streamable + public function readObject (ins :ObjectInputStream) :void + { + messageId = ins.readShort(); + } + + /** + * Returns the next message id suitable for use by an upstream message./ + */ + protected static function nextMessageId () :int + { + _nextMessageId = (_nextMessageId + 1) % JavaConstants.SHORT_MAX_VALUE; + return _nextMessageId; + } + + /** This is used to generate monotonically increasing message ids on the + * client as new messages are generated. */ + protected static var _nextMessageId :int; +} +} diff --git a/src/as/com/threerings/util/JavaConstants.as b/src/as/com/threerings/util/JavaConstants.as new file mode 100644 index 000000000..bfa2de140 --- /dev/null +++ b/src/as/com/threerings/util/JavaConstants.as @@ -0,0 +1,7 @@ +package com.threerings.util { + +public class JavaConstants +{ + public static const SHORT_MAX_VALUE :int = (Math.pow(2, 15) - 1); +} +} diff --git a/src/as/com/threerings/util/long.as b/src/as/com/threerings/util/long.as index e7c7da2fd..98501814f 100644 --- a/src/as/com/threerings/util/long.as +++ b/src/as/com/threerings/util/long.as @@ -1,5 +1,11 @@ package com.threerings.util { +import com.threerings.io.ObjectInputStream; +import com.threerings.io.ObjectOutputStream; + +/** + * TODO: stuff. + */ public class long extends Object implements Streamable { @@ -13,16 +19,16 @@ public class long extends Object public function writeObject (out :ObjectOutputStream) :void //throws IOError { - out.writeInt(_lowbits); out.writeInt(_highbits); + out.writeInt(_lowbits); } // documentation inherited from interface Streamable public function readObject (ins :ObjectInputStream) :void //throws IOError { - _lowbits = ins.readInt(); _highbits = ins.readInt(); + _lowbits = ins.readInt(); } /** Yon bits. */