Beispiel #1
0
        public override Task Send(string messageId, object message)
        {
            return(Task.Factory.StartNew(() =>
            {
                if (_channel == null)
                {
                    throw new InvalidOperationException("Game has not joined a private channel!");
                }

                if (_game == null || !_game.Started)
                {
                    throw new InvalidOperationException("The game has not been started!");
                }


                _channel.SendData(_game.OpponentId, messageId, message.ToPayload());
            }));
        }
Beispiel #2
0
        public void OnDataReceived(string fromNode, string fromChannel, string payloadType, IntPtr payload)
        {
            var array = JNIEnv.GetArray <byte[]>(payload);

            //Public channel
            if (fromChannel == ChordManager.PublicChannel)
            {
                var message = array.ToMessage <PublicMessage>();
                //var message = payload.ToMessage<PublicMessage>();

                switch (payloadType)
                {
                case MessageType.ListGames:
                    if (_game != null && !_game.Started)
                    {
                        _publicChannel.SendData(fromNode, MessageType.Game, new PublicMessage
                        {
                            Type = MessageType.Game,
                            Game = new MultiplayerGame
                            {
                                Id         = _game.Id,
                                Name       = _game.Name,
                                OpponentId = _manager.Name,
                            },
                        }.ToPayload());
                    }
                    break;

                case MessageType.Game:
                    lock (_lock)
                        _games.Add(message.Game);
                    break;

                default:
                    break;
                }
            }
            //Our private channel
            else if (_channel != null && _channel.Name == fromChannel)
            {
                if (MessageTypes == null)
                {
                    Console.WriteLine("MessageTypes is null!");
                }
                else
                {
                    Type messageType;
                    if (MessageTypes.TryGetValue(payloadType, out messageType))
                    {
                        //var message = payload.ToMessage(messageType);
                        var message = array.ToMessage(messageType);

                        _activity.RunOnUiThread(() => OnReceived(message));
                    }
                    else
                    {
                        Console.WriteLine("No message type found for: " + messageType);
                    }
                }
            }

            /*
             * finally
             * {
             * JNIEnv.DeleteLocalRef(payload);
             * }*/
        }