Ejemplo n.º 1
0
        private void ClientConnectHandler(IPAddress ip, PerChannelID id)
        {
            ip = ip.MapToIPv6();

            if (!_pendingConnections.ContainsKey(ip)) // Start pending
            {
                Logger.Log("Starting pending connection on \"" + ip + "\"...");
                _pendingConnections.Add(ip, new Connection(ip));
            }
            var connection = _pendingConnections[ip];

            connection.AppendChannelId(id); // Append channel

            var complete = true;            // Pending connection is connected on all channels?

            for (int i = 0; i < _transportLayers.Length; i++)
            {
                complete &= connection.HasChannelId(i);
            }

            if (complete) // Successful connection on all channels
            {
                _pendingConnections.Remove(ip);
                _connectionsHistory.Add(connection);
                _activeConnections.Add(connection);

                Logger.Log("A client successfully connected on all channels.");
            }
        }
Ejemplo n.º 2
0
        public bool AppendChannelId(PerChannelID id)
        {
            if (HasChannelId(id.Channel))
            {
                Logger.Warn("This connection is already connected on channel#" + id.Channel + ".");
                return(false);
            }

            _perChannelId.Add(id.Channel, id.Id);
            return(true);
        }
Ejemplo n.º 3
0
        public Connection GetConnection(PerChannelID id, bool onlyConnected = true)
        {
            foreach (var connection in (onlyConnected ? _activeConnections : _connectionsHistory))
            {
                if (connection.Id(id.Channel) == id.Id)
                {
                    return(connection);
                }
            }

            return(null);
        }
        void IServerTransportLayer.Update()
        {
            while (_server.GetNextMessage(out var msg))
            {
                var id = new PerChannelID(_channelId, msg.connectionId);

                switch (msg.eventType)
                {
                case Telepathy.EventType.Connected:
                    _serverConnectionCallback.Invoke(_server.GetClientAddress(msg.connectionId), id);
                    break;

                case Telepathy.EventType.Data:
                    _serverMessageCallback.Invoke(msg.data, id);
                    break;

                case Telepathy.EventType.Disconnected:
                    _serverDisconnectCallback.Invoke(id);
                    break;
                }
            }
        }
Ejemplo n.º 5
0
        bool IServerTransportLayer.Init(int channelId, OnClientConnect connectCallback, OnReceiveMessage messageCallback, OnClientDisconnect disconnectCallback)
        {
            _connection = new MiniUDP.NetCore(ConnectionToken, true);
            _channelId  = channelId;

            _clients = new Dictionary <int, MiniUDP.NetPeer>();
            _nextId  = 0;

            _connection.PeerConnected += (peer, token) =>
            {
                _clients.Add(_nextId, peer);

                var id = new PerChannelID(channelId, _nextId++);

                connectCallback.Invoke(peer.EndPoint.Address, id);

                peer.PayloadReceived += (netPeer, data, length) =>
                {
                    if (data.Length != length)
                    {
                        Array.Resize(ref data, length);
                    }

                    messageCallback.Invoke(data, id);
                };
            };
            _connection.PeerClosed += (peer, reason, kickReason, error) =>
            {
                var id = _clients.FirstOrDefault(i => i.Value == peer).Key;
                disconnectCallback.Invoke(new PerChannelID(_channelId, id));

                _clients.Remove(id);
            };

            return(true);
        }
 /// <summary>
 /// Find a client based off its channel id
 /// </summary>
 public Connection FindClient(PerChannelID id, bool onlyConnected = true)
 {
     return(_server.GetConnection(id, onlyConnected));
 }
 /// <summary>
 /// Send packet on the channel to the given id
 /// </summary>
 public void Send(PerChannelID receiver, object packet)
 {
     _server.Send(FindClient(receiver, true), packet, receiver.Channel);
 }
Ejemplo n.º 8
0
 private void ClientDisconnectHandler(PerChannelID id)
 {
     // Disconnect client on all channels
     Disconnect(GetConnection(id, false));
 }
Ejemplo n.º 9
0
 private void ReceiveMessageHandler(byte[] data, PerChannelID id)
 {
     Logger.Log("Received packet!");
     PacketSubscribers?.Invoke(_serializer.DeserializeObject(data), GetConnection(id, false));
 }