Allow downstream and upstream messages to be compounded
A client can collect a bunch of messages into a CompoundUpstreamMessage, which are unpacked and dispatched in order by the server. The server collects any messages sent while processing that compound message into a CompoundDownstreamMessage, which is unpacked and dispatched by the client. This allows the client to indicate that it's going to send enough messages that it'd blow out the throttle, but that it's doing so intentionally. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@6659 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -20,7 +20,6 @@
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
package com.threerings.presents.client {
|
||||
|
||||
import flash.events.EventDispatcher;
|
||||
import flash.events.TimerEvent;
|
||||
|
||||
@@ -280,6 +279,21 @@ public class Client extends EventDispatcher
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the given function while collecting any generated messages in a CompoundEvent, which
|
||||
* will be sent when the function returns.
|
||||
*/
|
||||
public function inCompoundMessage (run :Function) :void
|
||||
{
|
||||
_comm.startCompoundMessage();
|
||||
try {
|
||||
run();
|
||||
} finally {
|
||||
_comm.finishCompoundMessage();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the client property object to pass to {@link ObjectInputStream}. Subclasses may
|
||||
* add references useful during e.g. deserialization.
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
package com.threerings.presents.client {
|
||||
import com.threerings.presents.net.CompoundDownstreamMessage;
|
||||
|
||||
import flash.events.Event;
|
||||
import flash.events.TimerEvent;
|
||||
@@ -175,6 +176,10 @@ public class ClientDObjectMgr
|
||||
|
||||
} else if (msg is UpdateThrottleMessage) {
|
||||
_client.setOutgoingMessageThrottle((msg as UpdateThrottleMessage).messagesPerSec);
|
||||
} else if (msg is CompoundDownstreamMessage) {
|
||||
for each (var submsg :DownstreamMessage in CompoundDownstreamMessage(msg).msgs) {
|
||||
processMessage(submsg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
package com.threerings.presents.client {
|
||||
import com.threerings.presents.net.CompoundUpstreamMessage;
|
||||
|
||||
import flash.events.Event;
|
||||
import flash.events.IOErrorEvent;
|
||||
@@ -75,6 +76,10 @@ public class Communicator
|
||||
|
||||
public function postMessage (msg :UpstreamMessage) :void
|
||||
{
|
||||
if (_compound != null) {
|
||||
_compound.msgs.push(msg);
|
||||
return;
|
||||
}
|
||||
_outq.push(msg);
|
||||
if (_writer != null) {
|
||||
sendPendingMessages(null);
|
||||
@@ -109,6 +114,23 @@ public class Communicator
|
||||
return false;
|
||||
}
|
||||
|
||||
public function startCompoundMessage () :void
|
||||
{
|
||||
if (_compound == null) {
|
||||
_compound = new CompoundUpstreamMessage();
|
||||
}
|
||||
_compoundDepth++;
|
||||
}
|
||||
|
||||
public function finishCompoundMessage () :void
|
||||
{
|
||||
if (--_compoundDepth == 0) {
|
||||
var toSend :CompoundUpstreamMessage = _compound;
|
||||
_compound = null;
|
||||
postMessage(toSend);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to logon on using the port at the specified index.
|
||||
*/
|
||||
@@ -383,6 +405,13 @@ public class Communicator
|
||||
protected var _writer :Timer;
|
||||
protected var _notedThrottle :Boolean = false;
|
||||
|
||||
protected var _compound :CompoundUpstreamMessage;
|
||||
|
||||
/**
|
||||
* The count of startCompoundMessage calls that have occurred without a finishCompoundMessage
|
||||
*/
|
||||
protected var _compoundDepth :int;
|
||||
|
||||
protected const log :Log = Log.getLog(this);
|
||||
|
||||
/** The current port we'll try to connect to. */
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Narya library - tools for developing networked games
|
||||
// Copyright (C) 2002-2011 Three Rings Design, Inc., All Rights Reserved
|
||||
// http://code.google.com/p/narya/
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or modify it
|
||||
// under the terms of the GNU Lesser General Public License as published
|
||||
// by the Free Software Foundation; either version 2.1 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
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
package com.threerings.presents.net {
|
||||
import com.threerings.io.streamers.ArrayStreamer;
|
||||
import com.threerings.io.TypedArray;
|
||||
import com.threerings.io.ObjectInputStream;
|
||||
|
||||
public class CompoundDownstreamMessage extends DownstreamMessage
|
||||
{
|
||||
public var msgs :TypedArray = TypedArray.create(DownstreamMessage);
|
||||
|
||||
override public function readObject (ins :ObjectInputStream) :void
|
||||
{
|
||||
super.readObject(ins);
|
||||
|
||||
msgs = ins.readField(ArrayStreamer.INSTANCE);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Narya library - tools for developing networked games
|
||||
// Copyright (C) 2002-2011 Three Rings Design, Inc., All Rights Reserved
|
||||
// http://code.google.com/p/narya/
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or modify it
|
||||
// under the terms of the GNU Lesser General Public License as published
|
||||
// by the Free Software Foundation; either version 2.1 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
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
package com.threerings.presents.net {
|
||||
|
||||
import com.threerings.io.ObjectInputStream;
|
||||
import com.threerings.io.ObjectOutputStream;
|
||||
import com.threerings.io.TypedArray;
|
||||
import com.threerings.io.streamers.ArrayStreamer;
|
||||
|
||||
import com.threerings.presents.net.UpstreamMessage;
|
||||
|
||||
public class CompoundUpstreamMessage extends UpstreamMessage
|
||||
{
|
||||
public var msgs :TypedArray = TypedArray.create(UpstreamMessage);
|
||||
|
||||
override public function writeObject (out :ObjectOutputStream) :void
|
||||
{
|
||||
super.writeObject(out);
|
||||
out.writeField(msgs, ArrayStreamer.INSTANCE);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -31,11 +31,6 @@ import com.threerings.io.Streamable;
|
||||
|
||||
public class PongResponse extends DownstreamMessage
|
||||
{
|
||||
public function PongResponse ()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
public function getPackStamp () :Long
|
||||
{
|
||||
return _packStamp;
|
||||
|
||||
Reference in New Issue
Block a user