public AlloClient() { if (!_AlloClient.allo_initialize(false)) { throw new Exception("Unable to initialize Allonet"); } unsafe { client = _AlloClient.alloclient_create(false); if (client == null) { throw new Exception("Failed to create Allonet instance"); } interactionCallback = new _AlloClient.InteractionCallbackFun(AlloClient._interaction); interactionCallbackHandle = GCHandle.Alloc(interactionCallback); IntPtr icp = Marshal.GetFunctionPointerForDelegate(interactionCallback); client->interaction_callback = icp; disconnectedCallback = new _AlloClient.DisconnectedCallbackFun(AlloClient._disconnected); disconnectedCallbackHandle = GCHandle.Alloc(disconnectedCallback); IntPtr icp2 = Marshal.GetFunctionPointerForDelegate(disconnectedCallback); client->disconnected_callback = icp2; assetRequestBytesCallback = new _AlloClient.AssetRequestBytesCallbackFun(AlloClient._assetRequestBytes); assetRequestBytesCallbackHandle = GCHandle.Alloc(assetRequestBytesCallback); IntPtr icp3 = Marshal.GetFunctionPointerForDelegate(assetRequestBytesCallback); client->asset_request_bytes_callback = icp3; thisHandle = GCHandle.Alloc(this); client->_backref = (IntPtr)thisHandle; } }
static unsafe private void _assetRequestBytes(_AlloClient *_client, IntPtr _assetId, UIntPtr offset, UIntPtr length) { GCHandle backref = (GCHandle)_client->_backref; AlloClient self = backref.Target as AlloClient; string assetId = Marshal.PtrToStringAnsi(_assetId); Debug.WriteLine($"Got request for asset {assetId}, length {length} at offset {offset}"); self.onAssetBytesRequested?.Invoke(assetId, (long)offset, (long)length); }
static unsafe private void _disconnected(_AlloClient *_client) { GCHandle backref = (GCHandle)_client->_backref; AlloClient self = backref.Target as AlloClient; Debug.WriteLine("_disconnected: calling delegate " + self.onDisconnected.ToString()); self.onDisconnected?.Invoke(); Debug.WriteLine("_disconnected: deallocating"); self.Disconnect(-1); }
public void Disconnect(int reason) { unsafe { if (client != null) { _AlloClient.alloclient_disconnect(client, reason); client = null; thisHandle.Free(); } } }
static unsafe private void _interaction(_AlloClient *_client, _AlloInteraction *inter) { string type = Marshal.PtrToStringAnsi(inter->type); string from = Marshal.PtrToStringAnsi(inter->senderEntityId); string to = Marshal.PtrToStringAnsi(inter->receiverEntityId); string cmd = Marshal.PtrToStringAnsi(inter->body); string requestId = Marshal.PtrToStringAnsi(inter->requestId); GCHandle backref = (GCHandle)_client->_backref; AlloClient self = backref.Target as AlloClient; List <object> data = Deserialize <List <object> >(cmd); if (from == "place" && data[0].ToString() == "announce") { self.avatarId = data[1].ToString(); self.placeName = data[2].ToString(); self.connected = true; self.onConnected?.Invoke(); } AlloEntity fromEntity = null; if (!string.IsNullOrEmpty(from)) { self.entities.TryGetValue(from, out fromEntity); } AlloEntity toEntity = null; if (!string.IsNullOrEmpty(to)) { self.entities.TryGetValue(to, out toEntity); } ResponseCallback callback = null; if (type == "response" && !string.IsNullOrEmpty(requestId)) { self.responseCallbacks.TryGetValue(requestId, out callback); } if (callback != null) { callback(cmd); self.responseCallbacks.Remove(requestId); } else { self.onInteraction?.Invoke(type, fromEntity, toEntity, data); } }
public unsafe static extern void alloclient_asset_send(_AlloClient *client, IntPtr asset_id, IntPtr data, UIntPtr offset, UIntPtr length, UIntPtr total_size);
public unsafe static extern void alloclient_asset_request(_AlloClient *client, IntPtr asset_id, IntPtr entity_id);
public unsafe static extern void alloclient_set_intent(_AlloClient *client, AlloIntent intent);
public unsafe static extern void alloclient_send_interaction(_AlloClient *client, _AlloInteraction *interaction);
public unsafe static extern void alloclient_poll(_AlloClient *client, int timeout_ms);
public unsafe static extern void alloclient_disconnect(_AlloClient *client, int reason);
public unsafe static extern bool alloclient_connect(_AlloClient *client, IntPtr urlString, IntPtr identity, IntPtr avatarDesc);