Factored Narya into two Maven modules: core and tools.

This will allow us to properly ship narya-tools.jar to Maven Central which is
necessary for Nenya and Vilya (and any other Narya-using project) to themselves
be built.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@6776 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2012-02-03 21:25:13 +00:00
parent d73949838d
commit a7e0893191
781 changed files with 173 additions and 109 deletions
+39
View File
@@ -0,0 +1,39 @@
#include "stable.h"
#include "ArrayMask.h"
ArrayMask::ArrayMask (int length)
{
_length = length/8;
if (length % 8 != 0) {
_length++;
}
_mask = new uint8[_length];
}
ArrayMask::ArrayMask (ObjectInputStream& in)
{
_length = in.readShort();
_mask = new uint8[_length];
in.readBytes(_mask, _length);
}
ArrayMask::~ArrayMask()
{
delete _mask;
}
void ArrayMask::set (int index)
{
_mask[index/8] |= (1 << (index%8));
}
bool ArrayMask::isSet (int index)
{
return (_mask[index/8] & (1 << (index%8))) != 0;
}
void ArrayMask::writeTo (ObjectOutputStream& out)
{
out.writeShort((int16) _length);
out.writeBytes(_mask, _length);
}
+18
View File
@@ -0,0 +1,18 @@
#pragma once
#include "ObjectInputStream.h"
#include "ObjectOutputStream.h"
class ArrayMask
{
public:
ArrayMask (int length);
ArrayMask (ObjectInputStream& in);
~ArrayMask();
void set (int index);
bool isSet (int index);
void writeTo (ObjectOutputStream& out);
protected:
uint8* _mask;
int _length;
};
+12
View File
@@ -0,0 +1,12 @@
//
// $Id$
#pragma once
struct Streamer;
struct ClassMapping
{
int16 code;
Streamer* streamer;
};
+13
View File
@@ -0,0 +1,13 @@
#pragma once
namespace presents
{ enum DisconnectReason {
NETWORK_DISCONNECT,
SERVER_DISCONNECT,
CLIENT_CLOSED,
SERVER_UNREACHABLE,
UNKNOWN
};
typedef boost::signal<void (DisconnectReason)> DisconnectEvent;
}
@@ -0,0 +1,9 @@
#include "stable.h"
#include "InvocationDecoder.h"
using namespace presents;
InvocationDecoder::InvocationDecoder(utf8 rcode) :
receiverCode(rcode)
{
}
+18
View File
@@ -0,0 +1,18 @@
#pragma once
#include "Streamable.h"
namespace presents
{
class InvocationDecoder {
public:
const utf8 receiverCode;
InvocationDecoder(utf8 receiverCode);
virtual void dispatchNotification(int8 methodId, const std::vector< Shared<Streamable> >& args) = 0;
private:
// not implemented
InvocationDecoder (const InvocationDecoder&);
InvocationDecoder& operator= (const InvocationDecoder&);
};
}
+100
View File
@@ -0,0 +1,100 @@
#include "stable.h"
#include <boost/format.hpp>
#include "ObjectInputStream.h"
#include "PresentsError.h"
#include "presents/io/InputStream.h"
#include "Streamer.h"
#include "Util.h"
bool ObjectInputStream::readBoolean ()
{
return readByte() != 0;
}
namespace
{
template <class T>
T readInteger(presents::InputStream* in)
{
T value;
assert(in->read(&value, sizeof(T)) == sizeof(T));
return presents::presentsToHost(value);
}
}
int8 ObjectInputStream::readByte ()
{
return readInteger<int8>(_in);
}
int16 ObjectInputStream::readShort ()
{
return readInteger<int16>(_in);
}
int32 ObjectInputStream::readInt ()
{
return readInteger<int32>(_in);
}
int64 ObjectInputStream::readLong ()
{
return readInteger<int64>(_in);
}
void ObjectInputStream::readBytes (uint8* bytes, size_t length)
{
_in->read(bytes, length);
}
float ObjectInputStream::readFloat ()
{
float value;
readBytes((uint8*) &value, 4);
return value;
}
double ObjectInputStream::readDouble ()
{
double value;
readBytes((uint8*) &value, 8);
return value;
}
utf8 ObjectInputStream::readUTF ()
{
uint16 dataLength = readInteger<uint16>(_in);
boost::scoped_array<uint8> data(new uint8[dataLength]);
readBytes((uint8*)&data[0], dataLength);
return std::string((char*)&data[0], dataLength);
}
Shared<void> ObjectInputStream::readObject ()
{
int16 code = readShort();
if (code == 0) {
return Shared<void>();
}
Streamer* streamer;
if (code < 0) {
code *= -1;
utf8 name = readUTF();
streamer = getStreamer(name);
ClassMapping cmap = { code, streamer };
_classMap.insert(_classMap.begin() + code, cmap);
} else {
streamer = (&_classMap[code])->streamer;
if (streamer == NULL) {
throw PresentsError((boost::format("Unknown streamer code '%1%'") % code).str());
}
}
return streamer->createObject(*this);
}
+49
View File
@@ -0,0 +1,49 @@
//
// $Id$
#pragma once
#include "ClassMapping.h"
#include "Streamer.h"
namespace presents { class InputStream; }
using namespace presents;
class ObjectInputStream
{
public:
ObjectInputStream (presents::InputStream* stream) : _in(stream), _classMap(1) { }
bool readBoolean ();
int8 readByte ();
int16 readShort ();
int32 readInt ();
int64 readLong ();
void readBytes (uint8* bytes, size_t length);
float readFloat ();
double readDouble ();
utf8 readUTF ();
Shared<void> readObject ();
template <typename T>
Shared<T> readField ()
{
return boost::static_pointer_cast<T>(readField(getJavaName((const T*)NULL)));
}
Shared<void> readField (utf8 javaname)
{
if (readBoolean()) {
return getStreamer(javaname)->createObject(*this);
} else {
return Shared<void>();
}
}
protected:
presents::InputStream* _in;
typedef std::vector<ClassMapping> ClassMap;
ClassMap _classMap;
};
@@ -0,0 +1,81 @@
#include "stable.h"
#include "presents/io/OutputStream.h"
#include "ObjectOutputStream.h"
#include "Streamable.h"
#include "Streamer.h"
#include "Util.h"
#pragma warning (disable: 4100) // unreferenced parameter
using namespace presents;
namespace {
template <class T>
static void writeInteger(OutputStream* out, T value)
{
value = hostToPresents(value);
out->write((uint8*)&value, sizeof(value));
}
}
void ObjectOutputStream::writeBoolean (bool value)
{
writeInteger(_out, (uint8)(value ? 1 : 0));
}
void ObjectOutputStream::writeByte (int8 value)
{
writeInteger(_out, value);
}
void ObjectOutputStream::writeLong (int64 value)
{
writeInteger(_out, value);
}
void ObjectOutputStream::writeInt (int32 value)
{
writeInteger(_out, value);
}
void ObjectOutputStream::writeShort (int16 value)
{
writeInteger(_out, value);
}
void ObjectOutputStream::writeBytes (const uint8* bytes, size_t length)
{
_out->write(bytes, length);
}
void ObjectOutputStream::writeDouble (double value)
{
_out->write((uint8*) &value, 8);
}
void ObjectOutputStream::writeFloat (float value)
{
_out->write((uint8*) &value, 4);
}
void ObjectOutputStream::writeUTF (const utf8& value)
{
size_t bytesToWrite = value.size() * sizeof(uint8);
writeShort(bytesToWrite);
_out->write((uint8*)value.data(), value.size());
}
void ObjectOutputStream::writeBareObject (const Shared<void>& object, Streamer* streamer)
{
streamer->writeObject(object, *this);
}
void ObjectOutputStream::writeField (const Shared<void>& object, const utf8& javaName)
{
writeBoolean(object != NULL);
if (object != NULL) {
writeBareObject(object, getStreamer(javaName));
}
}
@@ -0,0 +1,89 @@
//
// $Id$
#pragma once
#include <string>
#include <map>
#include <vector>
#include <boost/format.hpp>
#include "ClassMapping.h"
#include "PresentsError.h"
#include "Streamable.h"
#include "Streamer.h"
//interface Streamable;
//interface Streamer;
namespace presents { class OutputStream; }
using namespace presents;
class ObjectOutputStream
{
public:
ObjectOutputStream (presents::OutputStream* stream) : _out(stream), _nextClassCode(1) { }
void writeBoolean (bool value);
void writeByte (int8 value);
void writeShort (int16 value);
void writeInt (int32 value);
void writeLong (int64 value);
void writeBytes (const uint8* bytes, size_t length);
void writeDouble (double value);
void writeFloat (float value);
void writeUTF (const utf8& value);
template <typename T>
void writeObject (const Shared<T>& object)
{
if (object.get() == NULL) {
writeShort(0);
return;
}
const utf8 className = getObjectName(object.get());
ClassMap::iterator it = _classMap.find(className);
Streamer* streamer;
if (it == _classMap.end()) {
streamer = getStreamer(className);
if (streamer == NULL) {
throw PresentsError((boost::format("No streamer for '%1%'") % className).str());
}
ClassMapping cmap = { (int16)_nextClassCode++, streamer };
assert(_nextClassCode < 32767); // Can't be more than Short.MAX_VALUE
_classMap[className] = cmap;
writeShort(-cmap.code);
writeUTF(className);
} else {
ClassMapping* cmap = &(it->second);
streamer = cmap->streamer;
writeShort(cmap->code);
}
writeBareObject((Shared<void>)object, streamer);
}
template <typename T>
void writeField (const Shared<T>& object)
{
writeField(object, getJavaName((T*)NULL));
}
void writeField (const Shared<void>& object, const utf8& javaName);
protected:
void writeBareObject (const Shared<void>& object, Streamer* streamer);
presents::OutputStream* _out;
typedef std::map<utf8, ClassMapping> ClassMap;
ClassMap _classMap;
int _nextClassCode;
};
+256
View File
@@ -0,0 +1,256 @@
//
// $Id$
#include "presents/stable.h"
#include "PresentsClient.h"
#include "SocketStream.h"
#include "ObjectInputStream.h"
#include "ObjectOutputStream.h"
#include "presents/box/BoxedBoolean.h"
#include "presents/box/BoxedByte.h"
#include "presents/box/BoxedDouble.h"
#include "presents/box/BoxedFloat.h"
#include "presents/box/BoxedInt.h"
#include "presents/box/BoxedShort.h"
#include "presents/client/Registration.h"
#include "presents/data/ClientObject.h"
#include "presents/data/InvocationMarshaller.h"
#include "presents/data/PermissionPolicy.h"
#include "presents/data/RegistrationMarshaller.h"
#include "presents/dobj/DEvent.h"
#include "presents/dobj/DObject.h"
#include "presents/dobj/DSet.h"
#include "presents/dobj/EntryAddedEvent.h"
#include "presents/dobj/EntryEvent.h"
#include "presents/dobj/EntryRemovedEvent.h"
#include "presents/dobj/EntryUpdatedEvent.h"
#include "presents/dobj/InvocationNotificationEvent.h"
#include "presents/dobj/InvocationRequestEvent.h"
#include "presents/dobj/NamedEvent.h"
#include "presents/io/FramedInputStream.h"
#include "presents/io/FramingOutputStream.h"
#include "presents/net/AuthRequest.h"
#include "presents/net/AuthResponse.h"
#include "presents/net/AuthResponseData.h"
#include "presents/net/BootstrapData.h"
#include "presents/net/BootstrapNotification.h"
#include "presents/net/Credentials.h"
#include "presents/net/DownstreamMessage.h"
#include "presents/net/EventNotification.h"
#include "presents/net/ForwardEventRequest.h"
#include "presents/net/ObjectResponse.h"
#include "presents/net/PingRequest.h"
#include "presents/net/PongResponse.h"
#include "presents/net/UpstreamMessage.h"
#include "presents/net/UsernamePasswordCreds.h"
#include "util/Name.h"
#include "presents/Util.h"
using namespace presents;
PresentsClient::PresentsClient ()
: _outMessageId(0)
, _nextReceiverId(1)
, _oid(-1)
, _socket(NULL)
, _idleTime(0)
, _connected(false)
, _active(false)
{
// ensure all Streamables are not stripped out of the library by clients
// (Some of these classes aren't directly referenced by PresentsClient)
box::BoxedBoolean::registerWithPresents();
box::BoxedByte::registerWithPresents();
box::BoxedDouble::registerWithPresents();
box::BoxedFloat::registerWithPresents();
box::BoxedInt::registerWithPresents();
box::BoxedShort::registerWithPresents();
client::Registration::registerWithPresents();
data::ClientObject::registerWithPresents();
data::InvocationMarshaller::registerWithPresents();
data::PermissionPolicy::registerWithPresents();
data::RegistrationMarshaller::registerWithPresents();
dobj::DEvent::registerWithPresents();
dobj::DObject::registerWithPresents();
dobj::DSet::registerWithPresents();
dobj::EntryEvent::registerWithPresents();
dobj::EntryAddedEvent::registerWithPresents();
dobj::EntryRemovedEvent::registerWithPresents();
dobj::EntryUpdatedEvent::registerWithPresents();
dobj::InvocationNotificationEvent::registerWithPresents();
dobj::InvocationRequestEvent::registerWithPresents();
dobj::NamedEvent::registerWithPresents();
net::AuthRequest::registerWithPresents();
net::AuthResponse::registerWithPresents();
net::AuthResponseData::registerWithPresents();
net::BootstrapData::registerWithPresents();
net::BootstrapNotification::registerWithPresents();
net::Credentials::registerWithPresents();
net::DownstreamMessage::registerWithPresents();
net::EventNotification::registerWithPresents();
net::ForwardEventRequest::registerWithPresents();
net::ObjectResponse::registerWithPresents();
net::PingRequest::registerWithPresents();
net::PongResponse::registerWithPresents();
net::UpstreamMessage::registerWithPresents();
net::UsernamePasswordCreds::registerWithPresents();
}
PresentsClient::~PresentsClient ()
{
}
bool PresentsClient::isConnected () const
{
return _connected;
}
void PresentsClient::connect (const std::string& host, int port, Shared<presents::net::Credentials> creds, Shared<utf8> version)
{
assert(!_active);
assert(!_onSocketDisconnect.connected());
_active = true;
_socket.reset(createSocketStream(host.c_str(), port));
_onSocketDisconnect = _socket->onDisconnect(boost::bind(&PresentsClient::handleDisconnect, this, _1));
_framedIn.reset(new FramedInputStream(_socket.get()));
_framingOut.reset(new FramingOutputStream(_socket.get()));
_in.reset(new ObjectInputStream(_framedIn.get()));
_out.reset(new ObjectOutputStream(_framingOut.get()));
Shared<net::AuthRequest> req(new net::AuthRequest);
req->version = version;
req->zone = Shared<utf8>(new utf8(""));
req->creds = creds;
req->bootGroups = Shared< std::vector< Shared<utf8> > >(new std::vector< Shared<utf8> >);
req->bootGroups->push_back(Shared<utf8>(new utf8("client")));
PLOG("Connecting to %s:%d", host.c_str(), port);
postMessage(req);
}
void PresentsClient::disconnect ()
{
if (_active) {
_socket->close();
}
}
void PresentsClient::update (float dt)
{
if (!_active) {
return;
}
_socket->update(dt);
if (!_active) {
// Update can inform of uf a disconnect through onDisconnect, which shuts us down.
return;
}
while (_framedIn->readFrame()) {
Shared<Streamable> obj = boost::static_pointer_cast<Streamable>(_in->readObject());
if (obj == NULL) {
PLOG("Got null message? What the dickens?");
continue;
}
const utf8& msgName = obj->getJavaClassName();
if (msgName == net::AuthResponse::javaName()) {
Shared<net::AuthResponse> resp = boost::static_pointer_cast<net::AuthResponse>(obj);
PLOG("Got auth response from corpseserver: %s", resp->data->code->c_str());
_onAuth(*resp->data);
} else if (msgName == net::BootstrapNotification::javaName()) {
PLOG("Got bootstrap");
Shared<net::BootstrapNotification> strap = boost::static_pointer_cast<net::BootstrapNotification>(obj);
_oid = strap->data->clientOid;
typedef std::vector< Shared<data::InvocationMarshaller> > MarshVec;
Shared<MarshVec> services(strap->data->services);
for (MarshVec::iterator iter = services->begin(); iter != services->end(); ++iter) {
_services.insert(MarshallerMap::value_type((*iter)->getJavaClassName(), *iter));
}
_connected = true;
_onConnected(this);
} else if (msgName == net::EventNotification::javaName()) {
Shared<dobj::DEvent> event = boost::static_pointer_cast<net::EventNotification>(obj)->event;
if (event->getJavaClassName() == dobj::InvocationNotificationEvent::javaName()) {
Shared<dobj::InvocationNotificationEvent> notification = boost::static_pointer_cast<dobj::InvocationNotificationEvent>(event);
DecoderMap::iterator it = _decoders.find(notification->receiverId);
it->second->dispatchNotification(notification->methodId, *notification->args);
} else {
PLOG("Ignoring event %s", event->getJavaClassName().c_str());
}
} else {
PLOG("Ignoring message %s", msgName.c_str());
}
if (!_active) {
// The client may decide to disconnect as a result of reading the message, in which case we need to stop reading.
return;
}
}
_idleTime += dt;
if (!_connected) {
if (_idleTime > 30) {
handleDisconnect(SERVER_UNREACHABLE);
}
} else if (_idleTime > 60) {
Shared<net::PingRequest> ping(new net::PingRequest());
postMessage(ping);
}
}
void PresentsClient::postMessage (Shared<net::UpstreamMessage> message)
{
if (!_active) {
PLOG("PresentsClient::postMessage called while not connected!");
} else {
message->messageId = _outMessageId++;
_out->writeObject(message);
_framingOut->writeFrame();
_idleTime = 0;
}
}
void PresentsClient::sendRequest (int32 invOid, int32 invCode, int8 methId, const Shared< std::vector< Shared<Streamable> > >& args)
{
Shared<dobj::InvocationRequestEvent> req(new dobj::InvocationRequestEvent);
req->toid = invOid;
req->invCode = invCode;
req->methodId = methId;
req->args = args;
Shared<net::ForwardEventRequest> forward(new net::ForwardEventRequest);
forward->event = req;
postMessage(forward);
}
void PresentsClient::handleDisconnect (DisconnectReason reason)
{
PLOG("PresentsClient::handleDisconnect");
_active = false;
_socket.reset();
_in.reset();
_out.reset();
_framedIn.reset();
_framingOut.reset();
_oid = -1;
_decoders.clear();
_services.clear();
_onSocketDisconnect.disconnect();
_connected = false;
_onDisconnected(reason);
}
Shared<PresentsClient> PresentsClient::getSharedThis()
{
return shared_from_this();
}
+116
View File
@@ -0,0 +1,116 @@
//
// $Id$
#pragma once
#include <boost/format.hpp>
#include "presents/InvocationDecoder.h"
#include "presents/DisconnectReason.h"
#include "presents/PresentsError.h"
#include "presents/client/Registration.h"
#include "presents/data/RegistrationMarshaller.h"
class ObjectInputStream;
class ObjectOutputStream;
namespace presents
{
namespace data { class InvocationMarshaller; }
namespace net {
class AuthResponseData;
class Credentials;
class UpstreamMessage;
}
class FramedInputStream;
class FramingOutputStream;
class SocketStream;
class PresentsClient : public boost::enable_shared_from_this<PresentsClient>
{
protected:
typedef std::map<utf8, Shared<data::InvocationMarshaller> > MarshallerMap;
typedef std::map<int16, Shared<InvocationDecoder> > DecoderMap;
public:
typedef boost::signal<void (PresentsClient*)> ClientEvent;
typedef boost::signal<void (const net::AuthResponseData&)> AuthFailureEvent;
PresentsClient ();
virtual ~PresentsClient ();
void update (float dt);
template <typename T>
Shared<T> requireService ()
{
MarshallerMap::iterator it = _services.find(T::javaName());
if (it->second == NULL) {
throw PresentsError((boost::format("No marshaller for %1%") % T::javaName()).str());
}
return boost::static_pointer_cast<T>(it->second);
}
template <typename T>
void registerReceiver (Shared<T> receiver)
{
assert(_socket != NULL);
Shared<presents::client::Registration> reg(new presents::client::Registration);
reg->receiverId = _nextReceiverId++;
Shared<InvocationDecoder> dec(new typename T::Decoder(receiver));
reg->receiverCode = Shared<utf8>(new utf8(dec->receiverCode));
_decoders.insert(DecoderMap::value_type(reg->receiverId, dec));
requireService<presents::data::RegistrationMarshaller>()->registerReceiver(getSharedThis(), reg);
}
bool isConnected () const;
void connect (const std::string& hostname, int port, Shared<presents::net::Credentials> creds, Shared<utf8> version);
void disconnect ();
void postMessage (Shared<presents::net::UpstreamMessage> msg);
void sendRequest (int32 invOid, int32 invCode, int8 methodId, const Shared< std::vector< Shared<Streamable> > >& args);
int getOid() const { assert(_oid != -1); return _oid; }
connection_t onConnected (const ClientEvent::slot_type& slot) {
return _onConnected.connect(slot);
}
connection_t onDisconnected (const DisconnectEvent::slot_type& slot) {
return _onDisconnected.connect(slot);
}
connection_t onAuth (const AuthFailureEvent::slot_type& slot) {
return _onAuth.connect(slot);
}
protected:
void handleDisconnect (DisconnectReason);
Shared<PresentsClient> getSharedThis();
boost::scoped_ptr<SocketStream> _socket;
boost::scoped_ptr<FramingOutputStream> _framingOut;
boost::scoped_ptr<FramedInputStream> _framedIn;
boost::scoped_ptr<ObjectInputStream> _in;
boost::scoped_ptr<ObjectOutputStream> _out;
int16 _outMessageId;
int16 _nextReceiverId;
int32 _oid;
DecoderMap _decoders;
MarshallerMap _services;
float _idleTime;
// If the connection to the presents server is authenticated and ready to go. _onConnected is
// signaled when this becomes true
bool _connected;
// If we're connected or are trying to connect. This is always true when _connected is true,
// but also before the connection is authed and before the bootstrap data arrives.
bool _active;
ClientEvent _onConnected;
DisconnectEvent _onDisconnected;
AuthFailureEvent _onAuth;
connection_t _onSocketDisconnect;
};
}
+14
View File
@@ -0,0 +1,14 @@
#pragma once
namespace presents
{
class PresentsError : public std::runtime_error
{
public:
PresentsError(const std::string& message) : std::runtime_error(message)
{}
virtual ~PresentsError() throw()
{}
};
}
+18
View File
@@ -0,0 +1,18 @@
#pragma once
#include "DisconnectReason.h"
#include "io/InputStream.h"
#include "io/OutputStream.h"
namespace presents {
class SocketStream : public InputStream, public OutputStream
{
public:
virtual ~SocketStream() {};
virtual void close () = 0;
virtual void update (float dt) = 0;
virtual connection_t onDisconnect (const DisconnectEvent::slot_type& slot) = 0;
};
SocketStream* createSocketStream (const char* hostname, int port);
}
+15
View File
@@ -0,0 +1,15 @@
//
// $Id$
#pragma once
class ObjectOutputStream;
class ObjectInputStream;
struct Streamable
{
virtual void writeObject (ObjectOutputStream& out) const = 0;
virtual void readObject (ObjectInputStream& in) = 0;
virtual const utf8& getJavaClassName () const = 0;
};
+98
View File
@@ -0,0 +1,98 @@
#include "stable.h"
#include "Streamer.h"
#include <map>
#include <string>
#include <boost/format.hpp>
#include "ObjectInputStream.h"
#include "ObjectOutputStream.h"
#include "PresentsError.h"
#include "streamers/StringStreamer.h"
#include "streamers/StreamableStreamer.h"
#include "streamers/VectorStreamer.h"
#include "Util.h"
class StreamerMap
{
private:
typedef std::map<utf8, Streamer*> Map;
public:
void insert (const utf8& javaName, Streamer* streamer)
{
_map[javaName] = streamer;
}
Streamer* get (const utf8& javaName)
{
Map::iterator it = _map.find(javaName);
if (it == _map.end()) {
throw PresentsError((boost::format("Unable to find Streamer for %1%") % javaName).str());
}
return it->second;
}
~StreamerMap ()
{
for (Map::iterator iter = _map.begin(); iter != _map.end(); ++iter) {
delete iter->second;
}
}
private:
Map _map;
};
namespace
{
template <typename T>
void registerPrimitiveArrayStreamer (StreamerMap& map)
{
map.insert(getJavaName((const std::vector<T>*)NULL), new VectorStreamer<T>());
}
template <typename T>
void registerObjectArrayStreamer (StreamerMap& map)
{
map.insert(getJavaName<>((std::vector< Shared<T> >*)NULL), new VectorStreamer<T>());
}
}
StreamerMap* getMap ()
{
static bool gInited = false;
static StreamerMap gMap;
if (!gInited) {
gInited = true;
gMap.insert(getJavaName((utf8*)NULL), new StringStreamer());
gMap.insert(JAVA_LIST_NAME(), new VectorStreamer< Streamable >());
registerObjectArrayStreamer<Streamable>(gMap);
registerObjectArrayStreamer<utf8>(gMap);
registerPrimitiveArrayStreamer<int8>(gMap);
registerPrimitiveArrayStreamer<int16>(gMap);
registerPrimitiveArrayStreamer<int32>(gMap);
registerPrimitiveArrayStreamer<int64>(gMap);
registerPrimitiveArrayStreamer<float>(gMap);
registerPrimitiveArrayStreamer<double>(gMap);
registerPrimitiveArrayStreamer<bool>(gMap);
}
return &gMap;
}
void registerStreamer (const utf8& javaName, Streamer* streamer)
{
getMap()->insert(javaName, streamer);
}
Streamer* getStreamer (const utf8& javaName)
{
return getMap()->get(javaName);
}
+103
View File
@@ -0,0 +1,103 @@
//
// $Id$
#pragma once
#include <boost/type_traits.hpp>
#include "Streamable.h"
static inline utf8 JAVA_LIST_NAME () { return "java.util.ArrayList"; }
class ObjectOutputStream;
class ObjectInputStream;
struct Streamer
{
virtual Shared<void> createObject (ObjectInputStream& in) = 0;
virtual void writeObject (const Shared<void>& object, ObjectOutputStream& out) = 0;
};
Streamer* getStreamer (const utf8& javaName);
void registerStreamer (const utf8& javaName, Streamer* streamer);
inline const utf8 getJavaName (const utf8*)
{
return "java.lang.String";
}
inline const utf8 getJavaName (const int8*)
{
return "B";
}
inline const utf8 getJavaName (const int16*)
{
return "S";
}
inline const utf8 getJavaName (const int32*)
{
return "I";
}
inline const utf8 getJavaName (const int64*)
{
return "J";
}
inline const utf8 getJavaName (const bool*)
{
return "Z";
}
inline const utf8 getJavaName (const float*)
{
return "F";
}
inline const utf8 getJavaName (const double*)
{
return "D";
}
template <typename T>
inline const utf8 getJavaName (const Shared<T>*)
{
return getJavaName((const T*)NULL);
}
template <typename T>
inline const utf8 getJavaName (const std::vector<T>*)
{
return "[" + getJavaName((const T*)NULL);
}
template <typename T>
inline const utf8 getJavaName (const std::vector< Shared<T> >*)
{
return "[L" + getJavaName((const T*)NULL) + ";";
}
template <typename T>
inline const utf8 getJavaName (const T*)
{
return T::javaName();
}
template <>
inline const utf8 getJavaName<Streamable> (const Streamable*)
{
return "java.lang.Object";
}
template <typename T>
inline const utf8 getObjectName (T* object)
{
if (boost::is_base_of<Streamable, T>::value) {
// Look up the object's class virtually
return ((Streamable*)object)->getJavaClassName();
} else {
// Everything else is assumed to be a final class
return getJavaName(object);
}
}
+18
View File
@@ -0,0 +1,18 @@
#include "Util.h"
void presents::internal::log(const char* format, ...)
{
va_list argumentList;
va_start(argumentList, format);
char buffer[1024];
#ifdef PRESENTS_COMPILER_MSVC
if (_vsnprintf(buffer, 1024, format, argumentList) > 0) {
#else
if (vsprintf(buffer, format, argumentList) > 0) {
#endif
presents::log(buffer);
}
va_end(argumentList);
}
+37
View File
@@ -0,0 +1,37 @@
#pragma once
namespace presents {
void log (const char* message);
namespace internal {
void log (const char* format, ...);
}
template <class T>
inline T reverseBytes (T source)
{
uint8* pData = reinterpret_cast<uint8*>(&source);
std::reverse(pData, pData + sizeof(T));
return source;
}
template <class T>
T presentsToHost (T source)
{
#ifdef PRESENTS_HOST_LITTLE_ENDIAN
return reverseBytes(source);
#else
return source;
#endif
}
template <class T>
T hostToPresents (T source)
{
#ifdef PRESENTS_HOST_LITTLE_ENDIAN
return reverseBytes(source);
#else
return source;
#endif
}
}
@@ -0,0 +1,19 @@
#include "presents/stable.h"
#include "BoxedBoolean.h"
#include "presents/ObjectInputStream.h"
#include "presents/ObjectOutputStream.h"
// Generated by running python boxer.py in this directory
using namespace presents::box;
DEFINE_STREAMABLE("java.lang.Boolean", BoxedBoolean);
void BoxedBoolean::readObject (ObjectInputStream& in)
{
value = in.readBoolean();
}
void BoxedBoolean::writeObject (ObjectOutputStream& out) const
{
out.writeBoolean(value);
}
+24
View File
@@ -0,0 +1,24 @@
#pragma once
// Generated by running python boxer.py in this directory
#include "presents/Streamable.h"
#include "presents/streamers/StreamableStreamer.h"
namespace presents { namespace box {
class BoxedBoolean : public Streamable{
public:
DECLARE_STREAMABLE();
bool value;
static Shared<BoxedBoolean> createShared (bool value)
{
Shared<BoxedBoolean> shared(new BoxedBoolean);
shared->value = value;
return shared;
}
virtual void readObject(ObjectInputStream& in);
virtual void writeObject(ObjectOutputStream& out) const;
};
}}
+19
View File
@@ -0,0 +1,19 @@
#include "presents/stable.h"
#include "BoxedByte.h"
#include "presents/ObjectInputStream.h"
#include "presents/ObjectOutputStream.h"
// Generated by running python boxer.py in this directory
using namespace presents::box;
DEFINE_STREAMABLE("java.lang.Byte", BoxedByte);
void BoxedByte::readObject (ObjectInputStream& in)
{
value = in.readByte();
}
void BoxedByte::writeObject (ObjectOutputStream& out) const
{
out.writeByte(value);
}
+24
View File
@@ -0,0 +1,24 @@
#pragma once
// Generated by running python boxer.py in this directory
#include "presents/Streamable.h"
#include "presents/streamers/StreamableStreamer.h"
namespace presents { namespace box {
class BoxedByte : public Streamable{
public:
DECLARE_STREAMABLE();
int8 value;
static Shared<BoxedByte> createShared (int8 value)
{
Shared<BoxedByte> shared(new BoxedByte);
shared->value = value;
return shared;
}
virtual void readObject(ObjectInputStream& in);
virtual void writeObject(ObjectOutputStream& out) const;
};
}}
+19
View File
@@ -0,0 +1,19 @@
#include "presents/stable.h"
#include "BoxedDouble.h"
#include "presents/ObjectInputStream.h"
#include "presents/ObjectOutputStream.h"
// Generated by running python boxer.py in this directory
using namespace presents::box;
DEFINE_STREAMABLE("java.lang.Double", BoxedDouble);
void BoxedDouble::readObject (ObjectInputStream& in)
{
value = in.readDouble();
}
void BoxedDouble::writeObject (ObjectOutputStream& out) const
{
out.writeDouble(value);
}
+24
View File
@@ -0,0 +1,24 @@
#pragma once
// Generated by running python boxer.py in this directory
#include "presents/Streamable.h"
#include "presents/streamers/StreamableStreamer.h"
namespace presents { namespace box {
class BoxedDouble : public Streamable{
public:
DECLARE_STREAMABLE();
double value;
static Shared<BoxedDouble> createShared (double value)
{
Shared<BoxedDouble> shared(new BoxedDouble);
shared->value = value;
return shared;
}
virtual void readObject(ObjectInputStream& in);
virtual void writeObject(ObjectOutputStream& out) const;
};
}}
+19
View File
@@ -0,0 +1,19 @@
#include "presents/stable.h"
#include "BoxedFloat.h"
#include "presents/ObjectInputStream.h"
#include "presents/ObjectOutputStream.h"
// Generated by running python boxer.py in this directory
using namespace presents::box;
DEFINE_STREAMABLE("java.lang.Float", BoxedFloat);
void BoxedFloat::readObject (ObjectInputStream& in)
{
value = in.readFloat();
}
void BoxedFloat::writeObject (ObjectOutputStream& out) const
{
out.writeFloat(value);
}
+24
View File
@@ -0,0 +1,24 @@
#pragma once
// Generated by running python boxer.py in this directory
#include "presents/Streamable.h"
#include "presents/streamers/StreamableStreamer.h"
namespace presents { namespace box {
class BoxedFloat : public Streamable{
public:
DECLARE_STREAMABLE();
float value;
static Shared<BoxedFloat> createShared (float value)
{
Shared<BoxedFloat> shared(new BoxedFloat);
shared->value = value;
return shared;
}
virtual void readObject(ObjectInputStream& in);
virtual void writeObject(ObjectOutputStream& out) const;
};
}}
+19
View File
@@ -0,0 +1,19 @@
#include "presents/stable.h"
#include "BoxedInt.h"
#include "presents/ObjectInputStream.h"
#include "presents/ObjectOutputStream.h"
// Generated by running python boxer.py in this directory
using namespace presents::box;
DEFINE_STREAMABLE("java.lang.Integer", BoxedInt);
void BoxedInt::readObject (ObjectInputStream& in)
{
value = in.readInt();
}
void BoxedInt::writeObject (ObjectOutputStream& out) const
{
out.writeInt(value);
}
+24
View File
@@ -0,0 +1,24 @@
#pragma once
// Generated by running python boxer.py in this directory
#include "presents/Streamable.h"
#include "presents/streamers/StreamableStreamer.h"
namespace presents { namespace box {
class BoxedInt : public Streamable{
public:
DECLARE_STREAMABLE();
int32 value;
static Shared<BoxedInt> createShared (int32 value)
{
Shared<BoxedInt> shared(new BoxedInt);
shared->value = value;
return shared;
}
virtual void readObject(ObjectInputStream& in);
virtual void writeObject(ObjectOutputStream& out) const;
};
}}
+19
View File
@@ -0,0 +1,19 @@
#include "presents/stable.h"
#include "BoxedShort.h"
#include "presents/ObjectInputStream.h"
#include "presents/ObjectOutputStream.h"
// Generated by running python boxer.py in this directory
using namespace presents::box;
DEFINE_STREAMABLE("java.lang.Short", BoxedShort);
void BoxedShort::readObject (ObjectInputStream& in)
{
value = in.readShort();
}
void BoxedShort::writeObject (ObjectOutputStream& out) const
{
out.writeShort(value);
}
+24
View File
@@ -0,0 +1,24 @@
#pragma once
// Generated by running python boxer.py in this directory
#include "presents/Streamable.h"
#include "presents/streamers/StreamableStreamer.h"
namespace presents { namespace box {
class BoxedShort : public Streamable{
public:
DECLARE_STREAMABLE();
int16 value;
static Shared<BoxedShort> createShared (int16 value)
{
Shared<BoxedShort> shared(new BoxedShort);
shared->value = value;
return shared;
}
virtual void readObject(ObjectInputStream& in);
virtual void writeObject(ObjectOutputStream& out) const;
};
}}
+55
View File
@@ -0,0 +1,55 @@
primitives = [("Int", "int32", "Integer"),
("Short", "int16", "Short"),
("Byte", "int8", "Byte"),
("Boolean", "bool", "Boolean"),
("Double", "double", "Double"),
("Float", "float", "Float")]
for interpreter, cpptype, javatype in primitives:
header = open("Boxed" + interpreter + ".h", 'w')
header.write('''#pragma once
// Generated by running python boxer.py in this directory
#include "presents/Streamable.h"
#include "presents/streamers/StreamableStreamer.h"
namespace presents { namespace box {
class Boxed%(interpreter)s : public Streamable{
public:
DECLARE_STREAMABLE();
%(cpptype)s value;
static Shared<Boxed%(interpreter)s> createShared (%(cpptype)s value)
{
Shared<Boxed%(interpreter)s> shared(new Boxed%(interpreter)s);
shared->value = value;
return shared;
}
virtual void readObject(ObjectInputStream& in);
virtual void writeObject(ObjectOutputStream& out) const;
};
}}''' % locals())
header.close()
impl = open("Boxed" + interpreter + ".cpp", "w")
impl.write('''#include "presents/stable.h"
#include "Boxed%(interpreter)s.h"
#include "presents/ObjectInputStream.h"
#include "presents/ObjectOutputStream.h"
// Generated by running python boxer.py in this directory
using namespace presents::box;
DEFINE_STREAMABLE("java.lang.%(javatype)s", Boxed%(interpreter)s);
void Boxed%(interpreter)s::readObject (ObjectInputStream& in)
{
value = in.read%(interpreter)s();
}
void Boxed%(interpreter)s::writeObject (ObjectOutputStream& out) const
{
out.write%(interpreter)s(value);
}
''' % locals())
impl.close()
@@ -0,0 +1,18 @@
#include "presents/stable.h"
#include "Registration.h"
using namespace presents::client;
DEFINE_STREAMABLE("com.threerings.presents.client.InvocationReceiver$Registration", Registration);
void Registration::readObject (ObjectInputStream& in)
{
receiverCode = in.readField< utf8 >();
receiverId = int16(in.readShort());
}
void Registration::writeObject (ObjectOutputStream& out) const
{
out.writeField(receiverCode);
out.writeShort(receiverId);
}
@@ -0,0 +1,23 @@
#pragma once
#include "presents/Streamable.h"
#include "presents/ObjectInputStream.h"
#include "presents/ObjectOutputStream.h"
#include "presents/streamers/StreamableStreamer.h"
#include "presents/Streamable.h"
namespace presents { namespace client {
class Registration : public Streamable {
public:
DECLARE_STREAMABLE();
Shared<utf8> receiverCode;
int16 receiverId;
virtual void readObject(ObjectInputStream& in);
virtual void writeObject(ObjectOutputStream& out) const;
};
}}
@@ -0,0 +1,20 @@
#include "presents/stable.h"
#include "ClientObject.h"
using namespace presents::data;
DEFINE_STREAMABLE("com.threerings.presents.data.ClientObject", ClientObject);
void ClientObject::readObject (ObjectInputStream& in)
{
presents::dobj::DObject::readObject(in);
username = boost::static_pointer_cast<util::Name>(in.readObject());
receivers = boost::static_pointer_cast<presents::dobj::DSet>(in.readObject());
}
void ClientObject::writeObject (ObjectOutputStream& out) const
{
presents::dobj::DObject::writeObject(out);
out.writeObject(username);
out.writeObject(receivers);
}
+25
View File
@@ -0,0 +1,25 @@
#pragma once
#include "presents/Streamable.h"
#include "presents/ObjectInputStream.h"
#include "presents/ObjectOutputStream.h"
#include "presents/streamers/StreamableStreamer.h"
#include "presents/dobj/DObject.h"
#include "presents/dobj/DSet.h"
#include "util/Name.h"
namespace presents { namespace data {
class ClientObject : public presents::dobj::DObject {
public:
DECLARE_STREAMABLE();
Shared<util::Name> username;
Shared<presents::dobj::DSet> receivers;
virtual void readObject(ObjectInputStream& in);
virtual void writeObject(ObjectOutputStream& out) const;
};
}}
@@ -0,0 +1,18 @@
#include "presents/stable.h"
#include "InvocationMarshaller.h"
using namespace presents::data;
DEFINE_STREAMABLE("com.threerings.presents.data.InvocationMarshaller", InvocationMarshaller);
void InvocationMarshaller::readObject (ObjectInputStream& in)
{
invOid = int32(in.readInt());
invCode = int32(in.readInt());
}
void InvocationMarshaller::writeObject (ObjectOutputStream& out) const
{
out.writeInt(invOid);
out.writeInt(invCode);
}
@@ -0,0 +1,27 @@
#pragma once
#include "presents/Streamable.h"
#include "presents/ObjectInputStream.h"
#include "presents/ObjectOutputStream.h"
#include "presents/streamers/StreamableStreamer.h"
#include "presents/Streamable.h"
namespace presents {
class PresentsClient;
namespace data {
class InvocationMarshaller : public Streamable {
public:
DECLARE_STREAMABLE();
int32 invOid;
int32 invCode;
virtual void readObject(ObjectInputStream& in);
virtual void writeObject(ObjectOutputStream& out) const;
};
}}
@@ -0,0 +1,14 @@
#include "presents/stable.h"
#include "PermissionPolicy.h"
using namespace presents::data;
DEFINE_STREAMABLE("com.threerings.presents.data.PermissionPolicy", PermissionPolicy);
void PermissionPolicy::readObject (ObjectInputStream& /*in*/)
{
}
void PermissionPolicy::writeObject (ObjectOutputStream& /*out*/) const
{
}
@@ -0,0 +1,21 @@
#pragma once
#include "presents/Streamable.h"
#include "presents/ObjectInputStream.h"
#include "presents/ObjectOutputStream.h"
#include "presents/streamers/StreamableStreamer.h"
#include "presents/Streamable.h"
namespace presents { namespace data {
class PermissionPolicy : public Streamable {
public:
DECLARE_STREAMABLE();
virtual void readObject(ObjectInputStream& in);
virtual void writeObject(ObjectOutputStream& out) const;
};
}}
@@ -0,0 +1,42 @@
//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2012 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
#include "presents/stable.h"
#include "RegistrationMarshaller.h"
#include "presents/PresentsClient.h"
using namespace presents::data;
DEFINE_STREAMABLE("com.threerings.presents.data.RegistrationMarshaller", RegistrationMarshaller);
void RegistrationMarshaller::registerReceiver (Shared<presents::PresentsClient> client, Shared<presents::client::Registration> arg1)
{
typedef std::vector< Shared<Streamable> > StreamableList;
Shared<StreamableList> args(new StreamableList);
args->push_back(arg1);
args->push_back(getSharedThis());
client->sendRequest(invOid, invCode, 1, args);
}
Shared<RegistrationMarshaller> RegistrationMarshaller::getSharedThis()
{
return shared_from_this();
}
@@ -0,0 +1,45 @@
//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2012 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
#pragma once
#include "presents/Streamable.h"
#include "presents/ObjectInputStream.h"
#include "presents/ObjectOutputStream.h"
#include "presents/data/InvocationMarshaller.h"
#include "presents/streamers/StreamableStreamer.h"
#include <boost/enable_shared_from_this.hpp>
#include "presents/client/Registration.h"
namespace presents { namespace data {
class RegistrationMarshaller : public presents::data::InvocationMarshaller, public boost::enable_shared_from_this<RegistrationMarshaller> {
public:
DECLARE_STREAMABLE();
virtual ~RegistrationMarshaller () {}
void registerReceiver (Shared<presents::PresentsClient> client, Shared<presents::client::Registration> arg1);
protected:
Shared<RegistrationMarshaller> getSharedThis();
};
}}
+16
View File
@@ -0,0 +1,16 @@
#include "presents/stable.h"
#include "DEvent.h"
using namespace presents::dobj;
DEFINE_STREAMABLE("com.threerings.presents.dobj.DEvent", DEvent);
void DEvent::readObject (ObjectInputStream& in)
{
toid = int32(in.readInt());
}
void DEvent::writeObject (ObjectOutputStream& out) const
{
out.writeInt(toid);
}
+22
View File
@@ -0,0 +1,22 @@
#pragma once
#include "presents/Streamable.h"
#include "presents/ObjectInputStream.h"
#include "presents/ObjectOutputStream.h"
#include "presents/streamers/StreamableStreamer.h"
#include "presents/Streamable.h"
namespace presents { namespace dobj {
class DEvent : public Streamable {
public:
DECLARE_STREAMABLE();
int32 toid;
virtual void readObject(ObjectInputStream& in);
virtual void writeObject(ObjectOutputStream& out) const;
};
}}
+16
View File
@@ -0,0 +1,16 @@
#include "presents/stable.h"
#include "DObject.h"
using namespace presents::dobj;
DEFINE_STREAMABLE("com.threerings.presents.dobj.DObject", DObject);
void DObject::readObject (ObjectInputStream& in)
{
oid = int32(in.readInt());
}
void DObject::writeObject (ObjectOutputStream& out) const
{
out.writeInt(oid);
}
+22
View File
@@ -0,0 +1,22 @@
#pragma once
#include "presents/Streamable.h"
#include "presents/ObjectInputStream.h"
#include "presents/ObjectOutputStream.h"
#include "presents/streamers/StreamableStreamer.h"
#include "presents/Streamable.h"
namespace presents { namespace dobj {
class DObject : public Streamable {
public:
DECLARE_STREAMABLE();
int32 oid;
virtual void readObject(ObjectInputStream& in);
virtual void writeObject(ObjectOutputStream& out) const;
};
}}
+21
View File
@@ -0,0 +1,21 @@
#include "presents/stable.h"
#include "DSet.h"
using namespace presents::dobj;
DEFINE_STREAMABLE("com.threerings.presents.dobj.DSet", DSet);
void DSet::readObject (ObjectInputStream& in)
{
size = in.readInt();
entries = Shared<std::vector< Shared <Streamable> > >(new std::vector< Shared <Streamable> >(size));
for (int ii = 0; ii < size; ii++) {
(*entries)[ii] = boost::static_pointer_cast<Streamable>(in.readObject());
}
}
void DSet::writeObject (ObjectOutputStream& out) const
{
out.writeInt(size);
out.writeField(entries);
}
+21
View File
@@ -0,0 +1,21 @@
#pragma once
#include "presents/Streamable.h"
#include "presents/ObjectInputStream.h"
#include "presents/ObjectOutputStream.h"
#include "presents/streamers/StreamableStreamer.h"
namespace presents { namespace dobj {
class DSet : public Streamable {
public:
DECLARE_STREAMABLE();
Shared< std::vector< Shared<Streamable> > > entries;
int32 size;
virtual void readObject(ObjectInputStream& in);
virtual void writeObject(ObjectOutputStream& out) const;
};
}}
@@ -0,0 +1,18 @@
#include "presents/stable.h"
#include "EntryAddedEvent.h"
using namespace presents::dobj;
DEFINE_STREAMABLE("com.threerings.presents.dobj.EntryAddedEvent", EntryAddedEvent);
void EntryAddedEvent::readObject (ObjectInputStream& in)
{
presents::dobj::EntryEvent::readObject(in);
entry = boost::static_pointer_cast<Streamable>(in.readObject());
}
void EntryAddedEvent::writeObject (ObjectOutputStream& out) const
{
presents::dobj::EntryEvent::writeObject(out);
out.writeObject(entry);
}
@@ -0,0 +1,23 @@
#pragma once
#include "presents/Streamable.h"
#include "presents/ObjectInputStream.h"
#include "presents/ObjectOutputStream.h"
#include "presents/streamers/StreamableStreamer.h"
#include "presents/Streamable.h"
#include "presents/dobj/EntryEvent.h"
namespace presents { namespace dobj {
class EntryAddedEvent : public presents::dobj::EntryEvent {
public:
DECLARE_STREAMABLE();
Shared<Streamable> entry;
virtual void readObject(ObjectInputStream& in);
virtual void writeObject(ObjectOutputStream& out) const;
};
}}
+16
View File
@@ -0,0 +1,16 @@
#include "presents/stable.h"
#include "EntryEvent.h"
using namespace presents::dobj;
DEFINE_STREAMABLE("com.threerings.presents.dobj.EntryEvent", EntryEvent);
void EntryEvent::readObject (ObjectInputStream& in)
{
presents::dobj::NamedEvent::readObject(in);
}
void EntryEvent::writeObject (ObjectOutputStream& out) const
{
presents::dobj::NamedEvent::writeObject(out);
}
+21
View File
@@ -0,0 +1,21 @@
#pragma once
#include "presents/Streamable.h"
#include "presents/ObjectInputStream.h"
#include "presents/ObjectOutputStream.h"
#include "presents/streamers/StreamableStreamer.h"
#include "presents/dobj/NamedEvent.h"
namespace presents { namespace dobj {
class EntryEvent : public presents::dobj::NamedEvent {
public:
DECLARE_STREAMABLE();
virtual void readObject(ObjectInputStream& in);
virtual void writeObject(ObjectOutputStream& out) const;
};
}}
@@ -0,0 +1,18 @@
#include "presents/stable.h"
#include "EntryRemovedEvent.h"
using namespace presents::dobj;
DEFINE_STREAMABLE("com.threerings.presents.dobj.EntryRemovedEvent", EntryRemovedEvent);
void EntryRemovedEvent::readObject (ObjectInputStream& in)
{
presents::dobj::EntryEvent::readObject(in);
key = boost::static_pointer_cast<Streamable>(in.readObject());
}
void EntryRemovedEvent::writeObject (ObjectOutputStream& out) const
{
presents::dobj::EntryEvent::writeObject(out);
out.writeObject(key);
}
@@ -0,0 +1,23 @@
#pragma once
#include "presents/Streamable.h"
#include "presents/ObjectInputStream.h"
#include "presents/ObjectOutputStream.h"
#include "presents/streamers/StreamableStreamer.h"
#include "presents/Streamable.h"
#include "presents/dobj/EntryEvent.h"
namespace presents { namespace dobj {
class EntryRemovedEvent : public presents::dobj::EntryEvent {
public:
DECLARE_STREAMABLE();
Shared<Streamable> key;
virtual void readObject(ObjectInputStream& in);
virtual void writeObject(ObjectOutputStream& out) const;
};
}}
@@ -0,0 +1,18 @@
#include "presents/stable.h"
#include "EntryUpdatedEvent.h"
using namespace presents::dobj;
DEFINE_STREAMABLE("com.threerings.presents.dobj.EntryUpdatedEvent", EntryUpdatedEvent);
void EntryUpdatedEvent::readObject (ObjectInputStream& in)
{
presents::dobj::EntryEvent::readObject(in);
entry = boost::static_pointer_cast<Streamable>(in.readObject());
}
void EntryUpdatedEvent::writeObject (ObjectOutputStream& out) const
{
presents::dobj::EntryEvent::writeObject(out);
out.writeObject(entry);
}
@@ -0,0 +1,23 @@
#pragma once
#include "presents/Streamable.h"
#include "presents/ObjectInputStream.h"
#include "presents/ObjectOutputStream.h"
#include "presents/streamers/StreamableStreamer.h"
#include "presents/Streamable.h"
#include "presents/dobj/EntryEvent.h"
namespace presents { namespace dobj {
class EntryUpdatedEvent : public presents::dobj::EntryEvent {
public:
DECLARE_STREAMABLE();
Shared<Streamable> entry;
virtual void readObject(ObjectInputStream& in);
virtual void writeObject(ObjectOutputStream& out) const;
};
}}
@@ -0,0 +1,22 @@
#include "presents/stable.h"
#include "InvocationNotificationEvent.h"
using namespace presents::dobj;
DEFINE_STREAMABLE("com.threerings.presents.dobj.InvocationNotificationEvent", InvocationNotificationEvent);
void InvocationNotificationEvent::readObject (ObjectInputStream& in)
{
presents::dobj::DEvent::readObject(in);
receiverId = int16(in.readShort());
methodId = int8(in.readByte());
args = in.readField< std::vector< Shared<Streamable> > >();
}
void InvocationNotificationEvent::writeObject (ObjectOutputStream& out) const
{
presents::dobj::DEvent::writeObject(out);
out.writeShort(receiverId);
out.writeByte(methodId);
out.writeField(args);
}
@@ -0,0 +1,24 @@
#pragma once
#include "presents/Streamable.h"
#include "presents/ObjectInputStream.h"
#include "presents/ObjectOutputStream.h"
#include "presents/streamers/StreamableStreamer.h"
#include "presents/dobj/DEvent.h"
namespace presents { namespace dobj {
class InvocationNotificationEvent : public presents::dobj::DEvent {
public:
DECLARE_STREAMABLE();
int16 receiverId;
int8 methodId;
Shared< std::vector< Shared<Streamable> > > args;
virtual void readObject(ObjectInputStream& in);
virtual void writeObject(ObjectOutputStream& out) const;
};
}}
@@ -0,0 +1,22 @@
#include "presents/stable.h"
#include "InvocationRequestEvent.h"
using namespace presents::dobj;
DEFINE_STREAMABLE("com.threerings.presents.dobj.InvocationRequestEvent", InvocationRequestEvent);
void InvocationRequestEvent::readObject (ObjectInputStream& in)
{
presents::dobj::DEvent::readObject(in);
invCode = int32(in.readInt());
methodId = int8(in.readByte());
args = in.readField< std::vector< Shared<Streamable> > >();
}
void InvocationRequestEvent::writeObject (ObjectOutputStream& out) const
{
presents::dobj::DEvent::writeObject(out);
out.writeInt(invCode);
out.writeByte(methodId);
out.writeField(args);
}
@@ -0,0 +1,24 @@
#pragma once
#include "presents/Streamable.h"
#include "presents/ObjectInputStream.h"
#include "presents/ObjectOutputStream.h"
#include "presents/streamers/StreamableStreamer.h"
#include "presents/dobj/DEvent.h"
namespace presents { namespace dobj {
class InvocationRequestEvent : public presents::dobj::DEvent {
public:
DECLARE_STREAMABLE();
int32 invCode;
int8 methodId;
Shared< std::vector< Shared<Streamable> > > args;
virtual void readObject(ObjectInputStream& in);
virtual void writeObject(ObjectOutputStream& out) const;
};
}}
+18
View File
@@ -0,0 +1,18 @@
#include "presents/stable.h"
#include "NamedEvent.h"
using namespace presents::dobj;
DEFINE_STREAMABLE("com.threerings.presents.dobj.NamedEvent", NamedEvent);
void NamedEvent::readObject (ObjectInputStream& in)
{
presents::dobj::DEvent::readObject(in);
name = in.readField< utf8 >();
}
void NamedEvent::writeObject (ObjectOutputStream& out) const
{
presents::dobj::DEvent::writeObject(out);
out.writeField(name);
}
+22
View File
@@ -0,0 +1,22 @@
#pragma once
#include "presents/Streamable.h"
#include "presents/ObjectInputStream.h"
#include "presents/ObjectOutputStream.h"
#include "presents/streamers/StreamableStreamer.h"
#include "presents/dobj/DEvent.h"
namespace presents { namespace dobj {
class NamedEvent : public presents::dobj::DEvent {
public:
DECLARE_STREAMABLE();
Shared<utf8> name;
virtual void readObject(ObjectInputStream& in);
virtual void writeObject(ObjectOutputStream& out) const;
};
}}
@@ -0,0 +1,63 @@
#include "FramedInputStream.h"
#include "presents/stable.h"
#include "presents/PresentsError.h"
#include "presents/Util.h"
#include "FramedInputStream.h"
#include "InputStream.h"
using namespace presents;
static const int READ_BUFFER_LENGTH = 4096;
FramedInputStream::FramedInputStream (InputStream* base)
: _base(base)
, _length(-1)
, _fillPosition(0)
, _readPosition(0)
, _readBufferLength(READ_BUFFER_LENGTH)
, _readBuffer(new uint8[READ_BUFFER_LENGTH])
{
}
// Returns bytes read
size_t FramedInputStream::read (void* pData, size_t bytesToRead)
{
if (_fillPosition != _length) {
throw PresentsError("Attempted to read with no frame!");
}
if ((int32) (_readPosition + bytesToRead) > _length) {
throw PresentsError("Attempted to read past frame!");
}
memcpy(pData, &_readBuffer[_readPosition], bytesToRead);
_readPosition += bytesToRead;
return bytesToRead;
}
bool FramedInputStream::readFrame ()
{
if (_length == _fillPosition) {
if (_fillPosition != _readPosition) {
throw PresentsError("Attempted to read new frame without consuming old one");
}
_length = -1;
_fillPosition = _readPosition = 0;
}
if (_length == -1) {
_fillPosition += _base->read(&_readBuffer[_fillPosition], 4 - _fillPosition);
if (_fillPosition == 4) {
_length = ((_readBuffer[0] << 24) | (_readBuffer[1] << 16) | (_readBuffer[2] << 8) | _readBuffer[3]) - 4;
if (_length > _readBufferLength) {
_readBufferLength *= 2;
_readBuffer.reset(new uint8[_readBufferLength]);
PLOG("Thas a mighty big frame. Musta been feedin' him corn. Growing buffer to %d", _readBufferLength);
}
_fillPosition = 0;
} else {
return false;
}
}
_fillPosition += _base->read(&_readBuffer[_fillPosition], _length - _fillPosition);
return _fillPosition == _length;
}
@@ -0,0 +1,19 @@
#pragma once
#include "InputStream.h"
namespace presents {
class FramedInputStream : public InputStream
{
public:
FramedInputStream (InputStream* base);
virtual size_t read(void* pData, size_t bytesToRead);
bool readFrame ();
protected:
int32 _length;
InputStream* _base;
boost::scoped_array<uint8> _readBuffer;
int32 _fillPosition, _readPosition, _readBufferLength;
};
}
@@ -0,0 +1,30 @@
#include "presents/stable.h"
#include "FramingOutputStream.h"
#include "presents/Util.h"
using namespace presents;
FramingOutputStream::FramingOutputStream (OutputStream* base)
: _base(base)
{}
// Returns bytes written
size_t FramingOutputStream::write(const uint8* pData, size_t bytesToWrite)
{
uint8* bytes = (uint8*)pData;
for (int ii = 0; ii < bytesToWrite; ii++) {
_frameBuffer.push_back(bytes[ii]);
}
return bytesToWrite;
}
void FramingOutputStream::writeFrame()
{
int32 length = hostToPresents((int32)(_frameBuffer.size() + sizeof(int32)));
_base->write((uint8*)&length, sizeof(int32));
_base->write(&_frameBuffer[0], _frameBuffer.size());
_frameBuffer.clear();
}
@@ -0,0 +1,19 @@
#pragma once
#include "OutputStream.h"
namespace presents {
class FramingOutputStream : public OutputStream {
public:
FramingOutputStream (OutputStream* base);
// Returns bytes written
virtual size_t write (const uint8* pData, size_t bytesToWrite);
void writeFrame ();
protected:
OutputStream* _base;
std::vector<uint8> _frameBuffer;
};
}
+9
View File
@@ -0,0 +1,9 @@
#pragma once
namespace presents {
class InputStream {
public:
virtual ~InputStream() {};
virtual size_t read(void* pData, size_t bytesToRead) = 0;
};
}
+9
View File
@@ -0,0 +1,9 @@
#pragma once
namespace presents {
class OutputStream {
public:
virtual ~OutputStream() {};
virtual size_t write(const uint8* pData, size_t bytesToWrite) = 0;
};
}
+24
View File
@@ -0,0 +1,24 @@
#include "presents/stable.h"
#include "AuthRequest.h"
using namespace presents::net;
DEFINE_STREAMABLE("com.threerings.presents.net.AuthRequest", AuthRequest);
void AuthRequest::readObject (ObjectInputStream& in)
{
presents::net::UpstreamMessage::readObject(in);
creds = boost::static_pointer_cast<presents::net::Credentials>(in.readObject());
version = in.readField< utf8 >();
zone = in.readField< utf8 >();
bootGroups = boost::static_pointer_cast< std::vector< Shared<utf8> > >(in.readObject());
}
void AuthRequest::writeObject (ObjectOutputStream& out) const
{
presents::net::UpstreamMessage::writeObject(out);
out.writeObject(creds);
out.writeField(version);
out.writeField(zone);
out.writeObject(bootGroups);
}
+26
View File
@@ -0,0 +1,26 @@
#pragma once
#include "presents/Streamable.h"
#include "presents/ObjectInputStream.h"
#include "presents/ObjectOutputStream.h"
#include "presents/streamers/StreamableStreamer.h"
#include "presents/net/Credentials.h"
#include "presents/net/UpstreamMessage.h"
namespace presents { namespace net {
class AuthRequest : public presents::net::UpstreamMessage {
public:
DECLARE_STREAMABLE();
Shared<presents::net::Credentials> creds;
Shared<utf8> version;
Shared<utf8> zone;
Shared< std::vector< Shared<utf8> > > bootGroups;
virtual void readObject(ObjectInputStream& in);
virtual void writeObject(ObjectOutputStream& out) const;
};
}}
@@ -0,0 +1,18 @@
#include "presents/stable.h"
#include "AuthResponse.h"
using namespace presents::net;
DEFINE_STREAMABLE("com.threerings.presents.net.AuthResponse", AuthResponse);
void AuthResponse::readObject (ObjectInputStream& in)
{
presents::net::DownstreamMessage::readObject(in);
data = boost::static_pointer_cast<presents::net::AuthResponseData>(in.readObject());
}
void AuthResponse::writeObject (ObjectOutputStream& out) const
{
presents::net::DownstreamMessage::writeObject(out);
out.writeObject(data);
}
+23
View File
@@ -0,0 +1,23 @@
#pragma once
#include "presents/Streamable.h"
#include "presents/ObjectInputStream.h"
#include "presents/ObjectOutputStream.h"
#include "presents/streamers/StreamableStreamer.h"
#include "presents/net/AuthResponseData.h"
#include "presents/net/DownstreamMessage.h"
namespace presents { namespace net {
class AuthResponse : public presents::net::DownstreamMessage {
public:
DECLARE_STREAMABLE();
Shared<presents::net::AuthResponseData> data;
virtual void readObject(ObjectInputStream& in);
virtual void writeObject(ObjectOutputStream& out) const;
};
}}
@@ -0,0 +1,18 @@
#include "presents/stable.h"
#include "AuthResponseData.h"
using namespace presents::net;
DEFINE_STREAMABLE("com.threerings.presents.net.AuthResponseData", AuthResponseData);
void AuthResponseData::readObject (ObjectInputStream& in)
{
presents::dobj::DObject::readObject(in);
code = in.readField< utf8 >();
}
void AuthResponseData::writeObject (ObjectOutputStream& out) const
{
presents::dobj::DObject::writeObject(out);
out.writeField(code);
}
@@ -0,0 +1,22 @@
#pragma once
#include "presents/Streamable.h"
#include "presents/ObjectInputStream.h"
#include "presents/ObjectOutputStream.h"
#include "presents/streamers/StreamableStreamer.h"
#include "presents/dobj/DObject.h"
namespace presents { namespace net {
class AuthResponseData : public presents::dobj::DObject {
public:
DECLARE_STREAMABLE();
Shared<utf8> code;
virtual void readObject(ObjectInputStream& in);
virtual void writeObject(ObjectOutputStream& out) const;
};
}}
@@ -0,0 +1,21 @@
#include "presents/stable.h"
#include "BootstrapData.h"
#include "presents/Streamer.h"
using namespace presents::net;
DEFINE_STREAMABLE("com.threerings.presents.net.BootstrapData", BootstrapData);
void BootstrapData::readObject (ObjectInputStream& in)
{
connectionId = int32(in.readInt());
clientOid = int32(in.readInt());
services = boost::static_pointer_cast< std::vector< Shared<presents::data::InvocationMarshaller> > >(in.readField(JAVA_LIST_NAME()));
}
void BootstrapData::writeObject (ObjectOutputStream& out) const
{
out.writeInt(connectionId);
out.writeInt(clientOid);
out.writeField(services, JAVA_LIST_NAME());
}
+25
View File
@@ -0,0 +1,25 @@
#pragma once
#include "presents/Streamable.h"
#include "presents/ObjectInputStream.h"
#include "presents/ObjectOutputStream.h"
#include "presents/streamers/StreamableStreamer.h"
#include "presents/Streamable.h"
#include "presents/data/InvocationMarshaller.h"
namespace presents { namespace net {
class BootstrapData : public Streamable {
public:
DECLARE_STREAMABLE();
int32 connectionId;
int32 clientOid;
Shared< std::vector< Shared<presents::data::InvocationMarshaller> > > services;
virtual void readObject(ObjectInputStream& in);
virtual void writeObject(ObjectOutputStream& out) const;
};
}}
@@ -0,0 +1,18 @@
#include "presents/stable.h"
#include "BootstrapNotification.h"
using namespace presents::net;
DEFINE_STREAMABLE("com.threerings.presents.net.BootstrapNotification", BootstrapNotification);
void BootstrapNotification::readObject (ObjectInputStream& in)
{
presents::net::DownstreamMessage::readObject(in);
data = boost::static_pointer_cast<presents::net::BootstrapData>(in.readObject());
}
void BootstrapNotification::writeObject (ObjectOutputStream& out) const
{
presents::net::DownstreamMessage::writeObject(out);
out.writeObject(data);
}
@@ -0,0 +1,23 @@
#pragma once
#include "presents/Streamable.h"
#include "presents/ObjectInputStream.h"
#include "presents/ObjectOutputStream.h"
#include "presents/streamers/StreamableStreamer.h"
#include "presents/net/BootstrapData.h"
#include "presents/net/DownstreamMessage.h"
namespace presents { namespace net {
class BootstrapNotification : public presents::net::DownstreamMessage {
public:
DECLARE_STREAMABLE();
Shared<presents::net::BootstrapData> data;
virtual void readObject(ObjectInputStream& in);
virtual void writeObject(ObjectOutputStream& out) const;
};
}}
+14
View File
@@ -0,0 +1,14 @@
#include "presents/stable.h"
#include "Credentials.h"
using namespace presents::net;
DEFINE_STREAMABLE("com.threerings.presents.net.Credentials", Credentials);
void Credentials::readObject (ObjectInputStream& /*in*/)
{
}
void Credentials::writeObject (ObjectOutputStream& /*out*/) const
{
}
+21
View File
@@ -0,0 +1,21 @@
#pragma once
#include "presents/Streamable.h"
#include "presents/ObjectInputStream.h"
#include "presents/ObjectOutputStream.h"
#include "presents/streamers/StreamableStreamer.h"
#include "presents/Streamable.h"
namespace presents { namespace net {
class Credentials : public Streamable {
public:
DECLARE_STREAMABLE();
virtual void readObject(ObjectInputStream& in);
virtual void writeObject(ObjectOutputStream& out) const;
};
}}
@@ -0,0 +1,16 @@
#include "presents/stable.h"
#include "DownstreamMessage.h"
using namespace presents::net;
DEFINE_STREAMABLE("com.threerings.presents.net.DownstreamMessage", DownstreamMessage);
void DownstreamMessage::readObject (ObjectInputStream& in)
{
messageId = int16(in.readShort());
}
void DownstreamMessage::writeObject (ObjectOutputStream& out) const
{
out.writeShort(messageId);
}
@@ -0,0 +1,22 @@
#pragma once
#include "presents/Streamable.h"
#include "presents/ObjectInputStream.h"
#include "presents/ObjectOutputStream.h"
#include "presents/streamers/StreamableStreamer.h"
#include "presents/Streamable.h"
namespace presents { namespace net {
class DownstreamMessage : public Streamable {
public:
DECLARE_STREAMABLE();
int16 messageId;
virtual void readObject(ObjectInputStream& in);
virtual void writeObject(ObjectOutputStream& out) const;
};
}}
@@ -0,0 +1,18 @@
#include "presents/stable.h"
#include "EventNotification.h"
using namespace presents::net;
DEFINE_STREAMABLE("com.threerings.presents.net.EventNotification", EventNotification);
void EventNotification::readObject (ObjectInputStream& in)
{
presents::net::DownstreamMessage::readObject(in);
event = boost::static_pointer_cast<presents::dobj::DEvent>(in.readObject());
}
void EventNotification::writeObject (ObjectOutputStream& out) const
{
presents::net::DownstreamMessage::writeObject(out);
out.writeObject(event);
}
@@ -0,0 +1,23 @@
#pragma once
#include "presents/Streamable.h"
#include "presents/ObjectInputStream.h"
#include "presents/ObjectOutputStream.h"
#include "presents/streamers/StreamableStreamer.h"
#include "presents/dobj/DEvent.h"
#include "presents/net/DownstreamMessage.h"
namespace presents { namespace net {
class EventNotification : public presents::net::DownstreamMessage {
public:
DECLARE_STREAMABLE();
Shared<presents::dobj::DEvent> event;
virtual void readObject(ObjectInputStream& in);
virtual void writeObject(ObjectOutputStream& out) const;
};
}}
@@ -0,0 +1,18 @@
#include "presents/stable.h"
#include "ForwardEventRequest.h"
using namespace presents::net;
DEFINE_STREAMABLE("com.threerings.presents.net.ForwardEventRequest", ForwardEventRequest);
void ForwardEventRequest::readObject (ObjectInputStream& in)
{
presents::net::UpstreamMessage::readObject(in);
event = boost::static_pointer_cast<presents::dobj::DEvent>(in.readObject());
}
void ForwardEventRequest::writeObject (ObjectOutputStream& out) const
{
presents::net::UpstreamMessage::writeObject(out);
out.writeObject(event);
}
@@ -0,0 +1,23 @@
#pragma once
#include "presents/Streamable.h"
#include "presents/ObjectInputStream.h"
#include "presents/ObjectOutputStream.h"
#include "presents/streamers/StreamableStreamer.h"
#include "presents/dobj/DEvent.h"
#include "presents/net/UpstreamMessage.h"
namespace presents { namespace net {
class ForwardEventRequest : public presents::net::UpstreamMessage {
public:
DECLARE_STREAMABLE();
Shared<presents::dobj::DEvent> event;
virtual void readObject(ObjectInputStream& in);
virtual void writeObject(ObjectOutputStream& out) const;
};
}}
@@ -0,0 +1,18 @@
#include "presents/stable.h"
#include "ObjectResponse.h"
using namespace presents::net;
DEFINE_STREAMABLE("com.threerings.presents.net.ObjectResponse", ObjectResponse);
void ObjectResponse::readObject (ObjectInputStream& in)
{
presents::net::DownstreamMessage::readObject(in);
dobj = boost::static_pointer_cast<Streamable>(in.readObject());
}
void ObjectResponse::writeObject (ObjectOutputStream& out) const
{
presents::net::DownstreamMessage::writeObject(out);
out.writeObject(dobj);
}
@@ -0,0 +1,23 @@
#pragma once
#include "presents/Streamable.h"
#include "presents/ObjectInputStream.h"
#include "presents/ObjectOutputStream.h"
#include "presents/streamers/StreamableStreamer.h"
#include "presents/Streamable.h"
#include "presents/net/DownstreamMessage.h"
namespace presents { namespace net {
class ObjectResponse : public presents::net::DownstreamMessage {
public:
DECLARE_STREAMABLE();
Shared<Streamable> dobj;
virtual void readObject(ObjectInputStream& in);
virtual void writeObject(ObjectOutputStream& out) const;
};
}}
+16
View File
@@ -0,0 +1,16 @@
#include "presents/stable.h"
#include "PingRequest.h"
using namespace presents::net;
DEFINE_STREAMABLE("com.threerings.presents.net.PingRequest", PingRequest);
void PingRequest::readObject (ObjectInputStream& in)
{
presents::net::UpstreamMessage::readObject(in);
}
void PingRequest::writeObject (ObjectOutputStream& out) const
{
presents::net::UpstreamMessage::writeObject(out);
}
+21
View File
@@ -0,0 +1,21 @@
#pragma once
#include "presents/Streamable.h"
#include "presents/ObjectInputStream.h"
#include "presents/ObjectOutputStream.h"
#include "presents/streamers/StreamableStreamer.h"
#include "presents/net/UpstreamMessage.h"
namespace presents { namespace net {
class PingRequest : public presents::net::UpstreamMessage {
public:
DECLARE_STREAMABLE();
virtual void readObject(ObjectInputStream& in);
virtual void writeObject(ObjectOutputStream& out) const;
};
}}
@@ -0,0 +1,20 @@
#include "presents/stable.h"
#include "PongResponse.h"
using namespace presents::net;
DEFINE_STREAMABLE("com.threerings.presents.net.PongResponse", PongResponse);
void PongResponse::readObject (ObjectInputStream& in)
{
presents::net::DownstreamMessage::readObject(in);
packStamp = int64(in.readLong());
processDelay = int32(in.readInt());
}
void PongResponse::writeObject (ObjectOutputStream& out) const
{
presents::net::DownstreamMessage::writeObject(out);
out.writeLong(packStamp);
out.writeInt(processDelay);
}
+23
View File
@@ -0,0 +1,23 @@
#pragma once
#include "presents/Streamable.h"
#include "presents/ObjectInputStream.h"
#include "presents/ObjectOutputStream.h"
#include "presents/streamers/StreamableStreamer.h"
#include "presents/net/DownstreamMessage.h"
namespace presents { namespace net {
class PongResponse : public presents::net::DownstreamMessage {
public:
DECLARE_STREAMABLE();
int64 packStamp;
int32 processDelay;
virtual void readObject(ObjectInputStream& in);
virtual void writeObject(ObjectOutputStream& out) const;
};
}}
@@ -0,0 +1,16 @@
#include "presents/stable.h"
#include "UpstreamMessage.h"
using namespace presents::net;
DEFINE_STREAMABLE("com.threerings.presents.net.UpstreamMessage", UpstreamMessage);
void UpstreamMessage::readObject (ObjectInputStream& in)
{
messageId = int16(in.readShort());
}
void UpstreamMessage::writeObject (ObjectOutputStream& out) const
{
out.writeShort(messageId);
}
@@ -0,0 +1,22 @@
#pragma once
#include "presents/Streamable.h"
#include "presents/ObjectInputStream.h"
#include "presents/ObjectOutputStream.h"
#include "presents/streamers/StreamableStreamer.h"
#include "presents/Streamable.h"
namespace presents { namespace net {
class UpstreamMessage : public Streamable {
public:
DECLARE_STREAMABLE();
int16 messageId;
virtual void readObject(ObjectInputStream& in);
virtual void writeObject(ObjectOutputStream& out) const;
};
}}
@@ -0,0 +1,20 @@
#include "presents/stable.h"
#include "UsernamePasswordCreds.h"
using namespace presents::net;
DEFINE_STREAMABLE("com.threerings.presents.net.UsernamePasswordCreds", UsernamePasswordCreds);
void UsernamePasswordCreds::readObject (ObjectInputStream& in)
{
presents::net::Credentials::readObject(in);
username = boost::static_pointer_cast<util::Name>(in.readObject());
password = in.readField< utf8 >();
}
void UsernamePasswordCreds::writeObject (ObjectOutputStream& out) const
{
presents::net::Credentials::writeObject(out);
out.writeObject(username);
out.writeField(password);
}
@@ -0,0 +1,24 @@
#pragma once
#include "presents/Streamable.h"
#include "presents/ObjectInputStream.h"
#include "presents/ObjectOutputStream.h"
#include "presents/streamers/StreamableStreamer.h"
#include "presents/net/Credentials.h"
#include "util/Name.h"
namespace presents { namespace net {
class UsernamePasswordCreds : public presents::net::Credentials {
public:
DECLARE_STREAMABLE();
Shared<util::Name> username;
Shared<utf8> password;
virtual void readObject(ObjectInputStream& in);
virtual void writeObject(ObjectOutputStream& out) const;
};
}}
@@ -0,0 +1,187 @@
//
// $Id$
#include "presents/stable.h"
#include "presents/SocketStream.h"
#include "presents/Util.h"
#include <CoreFoundation/CoreFoundation.h>
#include <CFNetwork/CFNetwork.h>
namespace presents
{
class CFSocketStream : public SocketStream
{
public:
CFSocketStream (const CFStringRef hostname, int port)
: _reader(NULL)
, _writer(NULL)
{
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, hostname, port, &_reader, &_writer);
CFReadStreamOpen(_reader);
CFWriteStreamOpen(_writer);
}
virtual ~CFSocketStream ()
{
CloseWithReason(CLIENT_CLOSED, "Destroyed!");
}
// Returns bytes read
virtual size_t read (void* pData, size_t bytesToRead)
{
if (_reader == NULL) {
PLOG("Attempted to read from closed stream!");
return 0;
}
switch (CFReadStreamGetStatus(_reader)) {
case kCFStreamStatusClosed:
CloseWithReason(SERVER_DISCONNECT, "Server closed our read connection");
return 0;
case kCFStreamStatusError:
HandleStreamError(CFReadStreamCopyError(_reader));
return 0;
}
if (CFReadStreamHasBytesAvailable(_reader)) {
int read = CFReadStreamRead(_reader, (UInt8*)pData, bytesToRead);
if (read < 0) {
PLOG("Got error while reading!");
HandleStreamError(CFReadStreamCopyError(_reader));
return 0;
} else if (read == 0 && CFReadStreamGetStatus(_reader) == kCFStreamStatusAtEnd) {
// Only disconnect due to end-of-stream if the stream thinks there should be data
CloseWithReason(SERVER_DISCONNECT, "Hit read stream end");
}
return read;
}
return 0;
}
// Returns bytes written
virtual size_t write (const uint8* pData, size_t bytesToWrite)
{
if (_writer == NULL) {
PLOG("Attempted to write to closed stream!");
return 0;
}
uint8* bytes = (uint8*)pData;
for (int ii = 0; ii < bytesToWrite; ii++) {
_writerBuffer.push_back(bytes[ii]);
}
return bytesToWrite;
}
virtual void update (float dt)
{
if (_writer == NULL) {
PLOG("Attempted to write to closed stream!");
return;
}
switch (CFWriteStreamGetStatus(_writer)) {
case kCFStreamStatusClosed:
CloseWithReason(SERVER_DISCONNECT, "Server closed our write connection");
return;
case kCFStreamStatusAtEnd:
CloseWithReason(SERVER_DISCONNECT, "Hit write stream end");
return;
case kCFStreamStatusError:
HandleStreamError(CFWriteStreamCopyError(_writer));
return;
}
while (!_writerBuffer.empty() && CFWriteStreamCanAcceptBytes(_writer)) {
int written = CFWriteStreamWrite(_writer, (UInt8*)&_writerBuffer[0], _writerBuffer.size());
if (written < 0) {
PLOG("Hit error in write!");
HandleStreamError(CFWriteStreamCopyError(_writer));
} else {
_writerBuffer.erase(_writerBuffer.begin(), _writerBuffer.begin() + written);
}
}
}
virtual void close ()
{
CloseWithReason(CLIENT_CLOSED, "Client requested close");
}
virtual connection_t onDisconnect (const DisconnectEvent::slot_type& slot)
{
return _onDisconnected.connect(slot);
}
protected:
void CloseWithReason (presents::DisconnectReason why, const std::string& detail)
{
if (_reader == NULL) {
return;// Multiple closes don't hurt nobody
}
PLOG("Closing stream: %s", detail.c_str());
CFReadStreamClose(_reader);
CFRelease(_reader);
_reader = NULL;
CFWriteStreamClose(_writer);
CFRelease(_writer);
_writer = NULL;
PLOG("Signaling disconnected");
_onDisconnected(why);
}
void HandleStreamError (CFErrorRef error)
{
if (CFErrorGetDomain(error) == kCFErrorDomainPOSIX) {
switch (CFErrorGetCode(error)) {
case ENETDOWN:
CloseWithReason(NETWORK_DISCONNECT, "Net down!");
break;
case ENETUNREACH:
CloseWithReason(NETWORK_DISCONNECT, "Net unreachable");
break;
case ENETRESET:
CloseWithReason(NETWORK_DISCONNECT, "Net reset");
break;
case ECONNRESET:
CloseWithReason(NETWORK_DISCONNECT, "Net connection reset");
break;
case EHOSTDOWN:
CloseWithReason(SERVER_UNREACHABLE, "host down!");
break;
case EHOSTUNREACH:
CloseWithReason(SERVER_UNREACHABLE, "host unreachable!");
break;
case ECONNREFUSED:
CloseWithReason(SERVER_UNREACHABLE, "connection refused");
break;
case EPIPE:
CloseWithReason(SERVER_DISCONNECT, "broken pipe");
break;
case ENOTCONN:
CloseWithReason(SERVER_DISCONNECT, "not connected");
break;
default:
PLOG("Unknown posix net error: %d", CFErrorGetCode(error));
CloseWithReason(NETWORK_DISCONNECT, "Unknown failure!");
break;
}
} else {
PLOG("Unknown CFStreamError domain: domain: %s error: %d", CFErrorGetDomain(error), CFErrorGetCode(error));
CloseWithReason(UNKNOWN, "CFStream error");
}
CFRelease(error);
}
CFWriteStreamRef _writer;
std::vector<uint8> _writerBuffer;
CFReadStreamRef _reader;
DisconnectEvent _onDisconnected;
};
SocketStream* createSocketStream (const char* hostname, int port)
{
CFStringRef cfHostname = CFStringCreateWithCString(kCFAllocatorDefault, hostname,
kCFStringEncodingMacRoman);
CFSocketStream* stream = new CFSocketStream(cfHostname, port);
CFRelease(cfHostname);
return stream;
}
}
+9
View File
@@ -0,0 +1,9 @@
#include "presents/Util.h"
#undef interface
#include <Foundation/Foundation.h>
void presents::log(const char* msg)
{
NSLog(@"%s", msg);
}
+148
View File
@@ -0,0 +1,148 @@
//
// C Library Headers
//
#include <math.h>
#include <time.h>
#include <float.h>
#include <wchar.h>
//
// Standard C++ Headers
//
#include <string>
#include <vector>
#include <map>
#include <stdexcept>
//
// Boost Headers
//
#pragma warning(push, 3)
#define BOOST_ALL_NO_LIB
#define BOOST_REGEX_NO_LIB
#define BOOST_DATE_TIME_NO_LIB
#include <boost/smart_ptr.hpp>
#include <boost/static_assert.hpp>
#include <boost/bind.hpp>
//
// boost::signals
#pragma warning(push)
#pragma warning(disable: 4512) // assignment operator could not be generated
#include <boost/signals.hpp>
#pragma warning(pop)
// typedefs
typedef boost::signals::connection connection_t;
typedef boost::signals::scoped_connection scoped_connection_t;
// GCC
#if defined(__GNUC__)
# define PRESENTS_COMPILER_GCC (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
// Visual Studio
#elif defined(_MSC_VER)
# define PRESENTS_COMPILER_MSVC _MSC_VER
// Unknown Compiler
#else
# error The current compiler is not supported.
#endif
#ifdef __APPLE__
#include "TargetConditionals.h"
#endif
#if defined(_WIN32)
# define PRESENTS_HOST_LITTLE_ENDIAN
typedef signed __int8 int8;
typedef unsigned __int8 uint8;
typedef signed __int16 int16;
typedef unsigned __int16 uint16;
typedef signed __int32 int32;
typedef unsigned __int32 uint32;
typedef signed __int64 int64;
typedef unsigned __int64 uint64;
typedef unsigned int uint;
typedef unsigned long ulong;
#elif defined(TARGET_OS_IPHONE)
# define PRESENTS_IOS
#include <MacTypes.h>
typedef int8_t int8;
typedef int16_t int16;
typedef int32_t int32;
typedef int64_t int64;
typedef uint8_t uint8;
typedef uint16_t uint16;
typedef uint32_t uint32;
typedef uint64_t uint64;
#define _INT32
typedef unsigned int uint;
typedef unsigned long ulong;
#elif defined(__APPLE__)
# define PRESENTS_MACOSX
#include <libkern/OSTypes.h>
#ifndef __i386__
typedef SInt64 int64;
typedef UInt64 uint64;
typedef SInt32 int32;
typedef SInt16 int16;
typedef SInt8 int8;
typedef UInt32 uint32;
typedef UInt16 uint16;
typedef UInt8 uint8;
#else
typedef int8_t int8;
typedef int16_t int16;
typedef int32_t int32;
typedef int64_t int64;
typedef uint8_t uint8;
typedef uint16_t uint16;
typedef uint32_t uint32;
typedef uint64_t uint64;
#define _INT32
#endif
typedef unsigned int uint;
typedef unsigned long ulong;
#elif defined(__linux__)
# define PRESENTS_HOST_LITTLE_ENDIAN
typedef signed char int8;
typedef signed short int16;
typedef signed int int32;
typedef signed long long int64;
typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned int uint32;
typedef unsigned long long uint64;
typedef unsigned int uint;
typedef unsigned long ulong;
// Unknown Platform
#else
# error The current platform is not supported.
#endif
#if defined(__APPLE__)
// Apple defines __BIG_ENDIAN__ and __LITTLE_ENDIAN__
# if defined(__BIG_ENDIAN__)
# define PRESENTS_HOST_BIG_ENDIAN
# else
# define PRESENTS_HOST_LITTLE_ENDIAN
# endif
#endif
#define PLOG presents::internal::log
#define Shared boost::shared_ptr
#define utf8 std::string
@@ -0,0 +1,34 @@
#pragma once
#include "presents/Streamer.h"
#include "presents/Streamable.h"
template <typename T>
class StreamableStreamer : public Streamer
{
public:
Shared<void> createObject (ObjectInputStream& in)
{
Shared<Streamable> streamable(new T());
streamable->readObject(in);
return streamable;
}
void writeObject (const Shared<void>& object, ObjectOutputStream& out)
{
((Shared<T>&)object)->writeObject(out);
}
};
#define DECLARE_STREAMABLE() \
static const utf8& javaName (); \
static void registerWithPresents () { javaName(); } \
virtual const utf8& getJavaClassName () const { return javaName(); }
#define DEFINE_STREAMABLE(nameString, StreamableClass) \
static const utf8& gStreamableClassRegistration = StreamableClass::javaName(); \
const utf8& StreamableClass::javaName () {\
static const utf8 JAVA_NAME = nameString; \
static bool gRegistered = false; \
if (!gRegistered) { registerStreamer(JAVA_NAME, new StreamableStreamer<StreamableClass>()); gRegistered = true; } \
return JAVA_NAME; }

Some files were not shown because too many files have changed in this diff Show More