More progress.
git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3849 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -30,16 +30,51 @@ public class Client extends EventDispatcher
|
||||
_port = port;
|
||||
}
|
||||
|
||||
public function getHostname () :String
|
||||
{
|
||||
return _hostname;
|
||||
}
|
||||
|
||||
public function getPort () :int
|
||||
{
|
||||
return _port;
|
||||
}
|
||||
|
||||
public function getCredentials () :Credentials
|
||||
{
|
||||
return _creds;
|
||||
}
|
||||
|
||||
public function setCredentials (creds :Credentials) :void
|
||||
{
|
||||
_creds = creds;
|
||||
}
|
||||
|
||||
public function getVersion () :String
|
||||
{
|
||||
return _version;
|
||||
}
|
||||
|
||||
public function setVersion (version :String)
|
||||
{
|
||||
_version = version;
|
||||
}
|
||||
|
||||
public function getAuthResponseData () :AuthResponseData
|
||||
{
|
||||
return _authData;
|
||||
}
|
||||
|
||||
public function getDObjectManager () :DObjectManager
|
||||
{
|
||||
return _omgr;
|
||||
}
|
||||
|
||||
public function getClientOid () :int
|
||||
{
|
||||
return _cloid;
|
||||
}
|
||||
|
||||
public function getClientObject () :ClientObject
|
||||
{
|
||||
return _clobj;
|
||||
@@ -50,6 +85,24 @@ public class Client extends EventDispatcher
|
||||
return _invdir;
|
||||
}
|
||||
|
||||
public function getService (clazz :Class) :InvocationService
|
||||
{
|
||||
if (_bstrap == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// TODO
|
||||
}
|
||||
|
||||
public function requireService (clazz :Class) :InvocationService
|
||||
{
|
||||
var isvc :InvocationService = getService(clazz);
|
||||
if (isvc == null) {
|
||||
throw new Error(clazz + " isn't available. I can't bear to go on.");
|
||||
}
|
||||
return isvc;
|
||||
}
|
||||
|
||||
public function getBootstrapData () :BootstrapData
|
||||
{
|
||||
return _bstrap;
|
||||
@@ -76,9 +129,11 @@ public class Client extends EventDispatcher
|
||||
_comm = new Communicator(this);
|
||||
_comm.logon();
|
||||
|
||||
_tickInterval = new Timer(5000);
|
||||
_tickInterval.addEventListener(TimerEvent.TIMER, tick);
|
||||
_tickInterval.start();
|
||||
if (_tickInterval == null) {
|
||||
_tickInterval = new Timer(5000);
|
||||
_tickInterval.addEventListener(TimerEvent.TIMER, tick);
|
||||
_tickInterval.start();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -126,6 +181,22 @@ public class Client extends EventDispatcher
|
||||
_invdir.init(omgr, _cloid, this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called every five seconds; ensures that we ping the server if we
|
||||
* haven't communicated in a long while.
|
||||
*/
|
||||
protected function tick (event :TimerEvent) :void
|
||||
{
|
||||
if (_comm == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
var now :Number = new Date().getTime();
|
||||
if (now - _comm.getLastWrite() > PingRequest.PING_INTERVAL) {
|
||||
_comm.postMessage(new PingRequest());
|
||||
}
|
||||
}
|
||||
|
||||
protected function gotClientObject (clobj :ClientObject) :void
|
||||
{
|
||||
_clobj = clobj;
|
||||
@@ -145,22 +216,33 @@ public class Client extends EventDispatcher
|
||||
notifyObservers(ClientEvent.CLIENT_OBJECT_CHANGED);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called every five seconds; ensures that we ping the server if we
|
||||
* haven't communicated in a long while.
|
||||
*/
|
||||
protected function tick (event :TimerEvent) :void
|
||||
protected function cleanup (logonError :Error) :void
|
||||
{
|
||||
if (_comm == null) {
|
||||
return;
|
||||
}
|
||||
// clear out our references
|
||||
_comm = null;
|
||||
_omgr = null;
|
||||
_clobj = null;
|
||||
_cloid = -1;
|
||||
|
||||
var now :Number = new Date().getTime();
|
||||
if (now - _comm.getLastWrite() > PingRequest.PING_INTERVAL) {
|
||||
_comm.postMessage(new PingRequest());
|
||||
// and let our invocation director know we're logged off
|
||||
_invdir.cleanup();
|
||||
|
||||
// if this was due to a logon error, we can notify our listeners
|
||||
// now that we're cleaned up: they may want to retry logon on
|
||||
// another port, or something
|
||||
if (logonError != null) {
|
||||
notifyObservers(ClientEvent.CLIENT_FAILED_TO_LOGON, logonError);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by the omgr when we receive a pong packet.
|
||||
*/
|
||||
protected function gotPong (pong :PongResponse) :void
|
||||
{
|
||||
// TODO: compute time delta bowl-shit
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience method to dispatch a client event to any listeners
|
||||
* and return the result of dispatchEvent.
|
||||
|
||||
@@ -1,5 +1,15 @@
|
||||
package com.threerings.presents.client {
|
||||
|
||||
import flash.net.Socket;
|
||||
|
||||
import flash.util.ByteArray;
|
||||
import flash.util.Endian;
|
||||
|
||||
import com.threerings.io.FrameAvailableEvent;
|
||||
import com.threerings.io.FrameReader;
|
||||
import com.threerings.io.ObjectInputStream;
|
||||
import com.threerings.io.ObjectOutputStream;
|
||||
|
||||
public class Communicator
|
||||
{
|
||||
public function Communicator (client :Client)
|
||||
@@ -9,9 +19,156 @@ public class Communicator
|
||||
|
||||
public function logon () :void
|
||||
{
|
||||
// create the socket and set up listeners
|
||||
_socket = new Socket();
|
||||
_socket.addEventListener(Event.CONNECT, socketOpened);
|
||||
_socket.addEventListener(IOErrorEvent.IO_ERROR, socketError);
|
||||
_socket.addEventListener(Event.CLOSE, socketClosed);
|
||||
|
||||
// create our input/output business
|
||||
_outBuffer = new ByteArray();
|
||||
_outBuffer.endian = Endian.BIG_ENDIAN;
|
||||
_outStream = new ObjectOutputStream(_outBuffer);
|
||||
|
||||
_frameReader = new FrameReader(_socket);
|
||||
_frameReader.addEventListener(FrameAvailableEvent.FRAME_AVAILABLE,
|
||||
inputFrameReceived);
|
||||
_inStream = new ObjectInputStream();
|
||||
|
||||
_socket.connect(_client.getHostname(), _client.getPort());
|
||||
}
|
||||
|
||||
public function logoff () :void
|
||||
{
|
||||
if (_socket == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
sendMessage(new LogoffRequest());
|
||||
|
||||
shutdown(null);
|
||||
}
|
||||
|
||||
protected function shutdown (logonError :Error) :void
|
||||
{
|
||||
if (_socket != null) {
|
||||
try {
|
||||
_socket.close();
|
||||
} catch (err :Error) {
|
||||
trace("Error closing failed socket: " + err);
|
||||
}
|
||||
_socket = null;
|
||||
_outStream = null;
|
||||
_inStream = null;
|
||||
_frameReader = null;
|
||||
_outBuffer = null;
|
||||
}
|
||||
|
||||
_client.cleanup(logonError);
|
||||
}
|
||||
|
||||
protected function sendMessage (msg :UpstreamMessage) :void
|
||||
{
|
||||
// write the message (ends up in _outBuffer)
|
||||
_outStream.writeObject(msg);
|
||||
|
||||
// frame it by writing the length, then the bytes
|
||||
_socket.writeInt(_outBuffer.length);
|
||||
_socket.writeBytes(_outBuffer);
|
||||
_socket.flush();
|
||||
|
||||
// clean up the output buffer
|
||||
_outBuffer.length = 0;
|
||||
_outBuffer.position = 0;
|
||||
|
||||
// make a note of our most recent write time
|
||||
updateWriteStamp();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the time at which we last sent a packet to the server.
|
||||
*/
|
||||
protected function getLastWrite () :Number
|
||||
{
|
||||
return _lastWrite;
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes a note of the time at which we last communicated with the server.
|
||||
*/
|
||||
protected function updateWriteStamp () :void
|
||||
{
|
||||
_lastWrite = new Date().getTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when a frame of data from the server is ready to be
|
||||
* decoded into a DownstreamMessage.
|
||||
*/
|
||||
protected function inputFrameReceived (event :FrameAvailableEvent) :void
|
||||
{
|
||||
// convert the frame data into a message from the server
|
||||
_inStream.setSource(event.getFrameData());
|
||||
var msg :DownstreamMessage = _inStream.readObject();
|
||||
|
||||
if (_omgr != null) {
|
||||
// if we're logged on, then just do the normal thing
|
||||
_omgr.processMessage(msg);
|
||||
return;
|
||||
}
|
||||
|
||||
// Otherwise, this would be the AuthResponse to our logon attempt.
|
||||
var rsp :AuthResponse = (msg as AuthResponse); // TODO: as correct?
|
||||
var data :AuthResponseData = rsp.getData();
|
||||
if (data.code !== AuthResponseData.SUCCESS) {
|
||||
shutdown(new Error(data.code));
|
||||
return;
|
||||
}
|
||||
|
||||
// logon success
|
||||
_omgr = new ClientDObjectMgr(this, _client);
|
||||
_client._authData = data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when the connection to the server was successfully opened.
|
||||
*/
|
||||
protected function socketOpened (event :Event) :void
|
||||
{
|
||||
// well that's great! let's logon
|
||||
var req :AuthRequest = new AuthRequest(_client.getCredentials(),
|
||||
_client.getVersion());
|
||||
sendMessage(req);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when there is an io error with the socket.
|
||||
*/
|
||||
protected function socketError (event :IOErrorEvent) :void
|
||||
{
|
||||
trace("socketError: " + event);
|
||||
shutdown(new Error("socket closed unexpectedly."));
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when the connection to the server was closed.
|
||||
*/
|
||||
protected function socketClosed (event :Event) :void
|
||||
{
|
||||
_client.notifyObserver(ClientEvent.CLIENT_CONNECTION_FAILED);
|
||||
shutdown(null);
|
||||
}
|
||||
|
||||
protected var _client :Client;
|
||||
protected var _omgr :ClientDObjectManager;
|
||||
|
||||
protected var _outBuffer :ByteArray;
|
||||
protected var _outStream :ObjectOutputStream;
|
||||
|
||||
protected var _inStream :ObjectInputStream;
|
||||
|
||||
protected var _socket :Socket;
|
||||
|
||||
protected var _lastWrite :Number;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user