Some more progress.
I still need to work on the Streaming, but I've figured out how to do a bit of introspection which will be necessary when streaming an array. Started working on the client classes, did ClientObserver and SessionObserver stuff using flash's event notification system. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3846 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -33,6 +33,12 @@ public class ObjectOutputStream
|
||||
// create a class mapping if we've not got one
|
||||
if (cmap === undefined) {
|
||||
var streamer :Streamer = Streamer.getStreamer(cname);
|
||||
if (streamer === undefined) {
|
||||
// TODO
|
||||
trace("OMG, cannot stream ", cname);
|
||||
return;
|
||||
}
|
||||
|
||||
cmap = new ClassMapping(_nextCode++, cname, streamer);
|
||||
_classMap[cname] = cmap;
|
||||
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.threerings.io {
|
||||
|
||||
public dynamic class StreamableArray extends Array
|
||||
implements Streamable
|
||||
{
|
||||
public function StreamableArray (ctype :Class = undefined, length :int = 0)
|
||||
{
|
||||
super(length);
|
||||
_ctype = ctype;
|
||||
}
|
||||
|
||||
public function StreamableArray (ctype :Class, ... values)
|
||||
{
|
||||
super(values);
|
||||
_ctype = ctype;
|
||||
}
|
||||
|
||||
/** The type of all the elements in the array. */
|
||||
protected var _ctype :Class;
|
||||
}
|
||||
@@ -1,9 +1,18 @@
|
||||
package com.threerings.io {
|
||||
|
||||
import flash.util.ByteArray;
|
||||
|
||||
import com.threerings.util.SimpleMap;
|
||||
|
||||
import com.threerings.io.streamers.ArrayStreamer;
|
||||
import com.threerings.io.streamers.ByteArrayStreamer;
|
||||
import com.threerings.io.streamers.IntStreamer;
|
||||
import com.threerings.io.streamers.NumberStreamer;
|
||||
import com.threerings.io.streamers.StringStreamer;
|
||||
|
||||
public class Streamer
|
||||
{
|
||||
/*
|
||||
public static function getStreamer (className :String) :Streamer
|
||||
//throws IOError
|
||||
{
|
||||
@@ -19,6 +28,58 @@ public class Streamer
|
||||
|
||||
return streamer;
|
||||
}
|
||||
*/
|
||||
|
||||
public static function getStreamer (obj :*) :Streamer
|
||||
{
|
||||
if (_streamerMap == null) {
|
||||
createStreamers();
|
||||
}
|
||||
|
||||
if (obj is Streamable) {
|
||||
return null;
|
||||
|
||||
} else if (obj is String) {
|
||||
return STRING_STREAMER;
|
||||
|
||||
} else if (obj is int) {
|
||||
return INT_STREAMER;
|
||||
|
||||
} else if (obj is Number) {
|
||||
return NUMBER_STREAMER;
|
||||
|
||||
} else if (obj is Array) {
|
||||
return ARRAY_STREAMER;
|
||||
|
||||
} else if (obj is ByteArray) {
|
||||
return BYTE_ARRAY_STREAMER;
|
||||
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
public static function getStreamerByJavaName (jname :String) :Streamer
|
||||
{
|
||||
if (jname === "java.lang.String") {
|
||||
return STRING_STREAMER;
|
||||
|
||||
} else if (jname === "java.lang.Integer") {
|
||||
return INT_STREAMER;
|
||||
|
||||
} else if (jname === "java.lang.Double") {
|
||||
return NUMBER_STREAMER;
|
||||
|
||||
} else if (jname === "[Ljava.lang.Object") {
|
||||
return ARRAY_STREAMER;
|
||||
|
||||
} else if (jname === "[B") {
|
||||
return BYTE_ARRAY_STREAMER;
|
||||
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** This should be a protected constructor. */
|
||||
public function Streamer (targ :Class)
|
||||
@@ -86,12 +147,18 @@ public class Streamer
|
||||
protected static function createStreamers () :void
|
||||
{
|
||||
_streamerMap = new SimpleMap();
|
||||
|
||||
// TODO: fill in with the basic streamers
|
||||
}
|
||||
|
||||
protected var _targ :Class;
|
||||
|
||||
protected static var _streamerMap :SimpleMap;
|
||||
|
||||
|
||||
protected static const STRING_STREAMER :Streamer = new StringStreamer();
|
||||
protected static const BYTE_ARRAY_STREAMER :Streamer =
|
||||
new ByteArrayStreamer();
|
||||
protected static const ARRAY_STREAMER :Streamer = new ArrayStreamer();
|
||||
protected static const INT_STREAMER :Streamer = new IntStreamer();
|
||||
protected static const NUMBER_STREAMER :Streamer = new NumberStreamer();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.threerings.io.streamers {
|
||||
|
||||
import com.threerings.io.ObjectInputStream;
|
||||
import com.threerings.io.ObjectOutputStream;
|
||||
import com.threerings.io.Streamer;
|
||||
|
||||
/**
|
||||
* A Streamer for Array objects.
|
||||
*/
|
||||
public class ArrayStreamer extends Streamer
|
||||
{
|
||||
public function ArrayStreamer ()
|
||||
{
|
||||
super(Array);
|
||||
}
|
||||
|
||||
public override function createObject (ins :ObjectInputStream) :*
|
||||
{
|
||||
return new Array(ins.readInt());
|
||||
}
|
||||
|
||||
public override function writeObject (obj :*, out :ObjectOutputStream,
|
||||
useWriter :Boolean) :void
|
||||
{
|
||||
var arr :Array = (obj as Array);
|
||||
out.writeInt(arr.length);
|
||||
for (var ii :int = 0; ii < arr.length; ii++) {
|
||||
out.writeObject(arr[ii]);
|
||||
}
|
||||
}
|
||||
|
||||
public override function readObject (obj :*, ins :ObjectInputStream,
|
||||
useReader :Boolean) :void
|
||||
{
|
||||
var arr :Array = (obj as Array);
|
||||
for (var ii :int = 0; ii < arr.length; ii++) {
|
||||
arr[ii] = ins.readObject();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.threerings.io.streamers {
|
||||
|
||||
import flash.util.ByteArray;
|
||||
|
||||
import com.threerings.io.ObjectInputStream;
|
||||
import com.threerings.io.ObjectOutputStream;
|
||||
import com.threerings.io.Streamer;
|
||||
|
||||
/**
|
||||
* A Streamer for ByteArray objects.
|
||||
*/
|
||||
public class ByteArrayStreamer extends Streamer
|
||||
{
|
||||
public function ByteArrayStreamer ()
|
||||
{
|
||||
super(ByteArray);
|
||||
}
|
||||
|
||||
public override function createObject (ins :ObjectInputStream) :*
|
||||
{
|
||||
var bytes :ByteArray = new ByteArray();
|
||||
bytes.length = ins.readInt();
|
||||
return bytes;
|
||||
}
|
||||
|
||||
public override function writeObject (obj :*, out :ObjectOutputStream,
|
||||
useWriter :Boolean) :void
|
||||
{
|
||||
var bytes :ByteArray = (obj as ByteArray);
|
||||
out.writeInt(bytes.length);
|
||||
out.writeBytes(bytes);
|
||||
}
|
||||
|
||||
public override function readObject (obj :*, ins :ObjectInputStream,
|
||||
useReader :Boolean) :void
|
||||
{
|
||||
var bytes :ByteArray = (obj as ByteArray);
|
||||
ins.readBytes(bytes, 0, bytes.length);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.threerings.io.streamers {
|
||||
|
||||
import com.threerings.io.ObjectInputStream;
|
||||
import com.threerings.io.ObjectOutputStream;
|
||||
import com.threerings.io.Streamer;
|
||||
|
||||
/**
|
||||
* A Streamer for int objects.
|
||||
*/
|
||||
public class IntStreamer extends Streamer
|
||||
{
|
||||
public function IntStreamer ()
|
||||
{
|
||||
super(int);
|
||||
}
|
||||
|
||||
public override function createObject (ins :ObjectInputStream) :*
|
||||
{
|
||||
return ins.readInt();
|
||||
}
|
||||
|
||||
public override function writeObject (obj :*, out :ObjectOutputStream,
|
||||
useWriter :Boolean) :void
|
||||
{
|
||||
var i :int = (obj as int);
|
||||
out.writeInt(i);
|
||||
}
|
||||
|
||||
public override function readObject (obj :*, ins :ObjectInputStream,
|
||||
useReader :Boolean) :void
|
||||
{
|
||||
// nothing here, the int is fully read in createObject()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.threerings.io.streamers {
|
||||
|
||||
import com.threerings.io.ObjectInputStream;
|
||||
import com.threerings.io.ObjectOutputStream;
|
||||
import com.threerings.io.Streamer;
|
||||
|
||||
/**
|
||||
* A Streamer for Number objects.
|
||||
*/
|
||||
public class NumberStreamer extends Streamer
|
||||
{
|
||||
public function NumberStreamer ()
|
||||
{
|
||||
super(Number);
|
||||
}
|
||||
|
||||
public override function createObject (ins :ObjectInputStream) :*
|
||||
{
|
||||
return ins.readDouble();
|
||||
}
|
||||
|
||||
public override function writeObject (obj :*, out :ObjectOutputStream,
|
||||
useWriter :Boolean) :void
|
||||
{
|
||||
var n :Number = (obj as Number);
|
||||
out.writeDouble(n);
|
||||
}
|
||||
|
||||
public override function readObject (obj :*, ins :ObjectInputStream,
|
||||
useReader :Boolean) :void
|
||||
{
|
||||
// nothing here, the Number is fully read in createObject()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.threerings.io.streamers {
|
||||
|
||||
import com.threerings.io.ObjectInputStream;
|
||||
import com.threerings.io.ObjectOutputStream;
|
||||
import com.threerings.io.Streamer;
|
||||
|
||||
/**
|
||||
* A Streamer for String objects.
|
||||
*/
|
||||
public class StringStreamer extends Streamer
|
||||
{
|
||||
public function StringStreamer ()
|
||||
{
|
||||
super(String);
|
||||
}
|
||||
|
||||
public override function createObject (ins :ObjectInputStream) :*
|
||||
{
|
||||
return ins.readUTF();
|
||||
}
|
||||
|
||||
public override function writeObject (obj :*, out :ObjectOutputStream,
|
||||
useWriter :Boolean) :void
|
||||
{
|
||||
var s :String = (obj as String);
|
||||
out.writeUTF(s);
|
||||
}
|
||||
|
||||
public override function readObject (obj :*, ins :ObjectInputStream,
|
||||
useReader :Boolean) :void
|
||||
{
|
||||
// nothing here, the String is fully read in createObject()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user