Пример #1
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);
        }
Пример #2
0
        bool IClientTransportLayer.Init(int channelId, OnConnected connectedCallback, Generic.Client.OnReceiveMessage messageCallback, OnDisconnected disconnectedCallback)
        {
            _connection = new MiniUDP.NetCore(ConnectionToken, false);
            _channelId  = channelId;

            _connection.PeerConnected += (peer, token) =>
            {
                // Peer will be the server
                connectedCallback.Invoke(_channelId);

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

                    messageCallback.Invoke(data);
                };
            };
            _connection.PeerClosed += (peer, reason, kickReason, error) => disconnectedCallback.Invoke(_channelId);

            return(true);
        }