Beispiel #1
0
        internal void PlayerConnecting(object connection, IPEndPoint endpoint, Action <Player> ctor, NetMessage msg)
        {
            var player = new Player(this)
            {
                Connection = connection, EndPoint = endpoint
            };

            ctor(player);

            player.Status = ConnectionStatus.Connecting;
            if (ConstructNetData != null)
            {
                player.NetUserData = ConstructNetData();
            }

            if (VerifyPlayer != null)
            {
                VerifyPlayer(player, msg);
            }
            else
            {
                player.AllowConnect();
            }
        }
Beispiel #2
0
        private async void HandleClientConnection(TcpClient client)
        {
            var endpoint = client.Client.RemoteEndPoint;

            Debug.Log("Player connection request from {0}", endpoint);
            client.ReceiveBufferSize = 1024;
            client.SendBufferSize    = 1024;
            client.NoDelay           = true;
            client.LingerState       = new LingerOption(true, 10);

            Player player = null;

            try
            {
                using (var networkStream = client.GetStream())
                {
                    var        buffer        = new byte[1024];
                    NetMessage dataBuffer    = null;
                    var        bufferSize    = 0;
                    var        lengthBuffer  = new byte[2];
                    var        bytesReceived = 0;
                    int        readBytes;

                    var authMessages = new Queue <NetMessage>();
                    while (_tcpRunning)
                    {
                        readBytes = await networkStream.ReadAsync(buffer, 0, buffer.Length, _shutdownTokenSource.Token);

                        if (readBytes > 0)
                        {
                            var readMessage = NetMessage.GetMessages(buffer, readBytes, ref bytesReceived,
                                                                     ref dataBuffer, ref lengthBuffer, ref bufferSize, authMessages.Enqueue);
                            if (readMessage >= 1)
                            {
                                break;
                            }
                        }
                        if (!client.Connected)
                        {
                            return;
                        }
                    }

                    player = new Player(this)
                    {
                        TcpClient     = client,
                        NetworkStream = networkStream,
                        Status        = ConnectionStatus.Connecting
                    };
                    if (ConstructNetData != null)
                    {
                        player.NetUserData = ConstructNetData();
                    }

                    if (VerifyPlayer != null)
                    {
                        if (authMessages.Count > 0)
                        {
                            VerifyPlayer(player, authMessages.Dequeue());
                        }
                        else
                        {
                            player.Disconnect("No authentication message sent");
                        }
                    }
                    else
                    {
                        player.AllowConnect();
                    }

                    var canConnect = await player.AllowConnectCompletion.Task;
                    if (!canConnect)
                    {
                        return;
                    }

                    player.Status = ConnectionStatus.Connected;
                    FinalizePlayerAdd(player);

                    Debug.Log("Client connected from {0}", client.Client.RemoteEndPoint);
                    //and drain the rest of messages that might have come after the auth
                    while (authMessages.Count > 0)
                    {
                        player.ConsumeData(authMessages.Dequeue());
                    }

                    while (_tcpRunning)
                    {
                        readBytes = await networkStream.ReadAsync(buffer, 0, buffer.Length, _shutdownTokenSource.Token);

                        if (readBytes > 0)
                        {
                            NetMessage.GetMessages(buffer, readBytes, ref bytesReceived, ref dataBuffer,
                                                   ref lengthBuffer, ref bufferSize, player.ConsumeData);
                        }
                        if (!client.Connected)
                        {
                            return;
                        }
                    }
                }
            }
            catch (OperationCanceledException o)
            {
                Debug.Log("Operation canceled");
            }
            catch (ObjectDisposedException ode)
            {
                if (_tcpRunning && ode.ObjectName != "System.Net.Sockets.NetworkStream")
                {
                    Debug.LogException(ode, "{0} disposed when it shouldn't have", ode.ObjectName);
                }
            }
            catch (SocketException se)
            {
            }
            catch (IOException ioe)
            {
                if (!(ioe.InnerException is SocketException))
                {
                    Debug.LogException(ioe);
                }
            }
            catch (Exception exp)
            {
                Debug.LogException(exp);
            }
            finally
            {
                player.Status = ConnectionStatus.Disconnecting;
                Debug.Log("Closing client connection to {0}", endpoint);
                client.Close();
                if (player != null)
                {
                    player.Status = ConnectionStatus.Disconnected;
                    RemovePlayer(player);
                    player.NetworkStream = null;
                }
            }
        }
        private void PlayerCallback(object peer)
        {
            var netPeer = peer as NetServer;

            if (netPeer == null)
            {
                return;
            }

            var lmsg = netPeer.ReadMessage();

            var msgType = lmsg.MessageType;
            var sender  = lmsg.SenderEndPoint;
            var sConn   = lmsg.SenderConnection;
            var seq     = lmsg.SequenceChannel;
            var msg     = NetMessage.GetMessage(lmsg.Data.Length);

            lmsg.Clone(msg);
            msg.Sender = lmsg.SenderConnection;
            netPeer.Recycle(lmsg);

            if (msgType == NetIncomingMessageType.Data)
            {
                var player = GetPlayer(sConn);
                if (player != null)
                {
                    player.ConsumeData(msg);
                }
                else
                {
                    Debug.LogError($"Unknown player {sender} sent {msg.LengthBytes} bytes of data");
                    sConn.Disconnect(DtoPMsgs.UnknownPlayer);
                }
            }
            else if (msgType == NetIncomingMessageType.DebugMessage)
            {
                Debug.Log(msg.ReadString());
            }
            else if (msgType == NetIncomingMessageType.ConnectionApproval)
            {
                var player = new Player(this)
                {
                    Connection = sConn
                };
                sConn.Tag     = player;
                player.Status = ConnectionStatus.Connecting;
                if (ConstructNetData != null)
                {
                    player.NetUserData = ConstructNetData();
                }

                if (VerifyPlayer != null)
                {
                    VerifyPlayer(player, msg);
                }
                else
                {
                    player.AllowConnect();
                }
            }
            else if (msgType == NetIncomingMessageType.StatusChanged)
            {
                var status       = (NetConnectionStatus)msg.ReadByte();
                var statusReason = msg.ReadString();
                var player       = GetPlayer(sConn);
                if (player != null)
                {
                    player.Status = status.ToPNet();
                    if (status == NetConnectionStatus.Disconnecting || status == NetConnectionStatus.Disconnected)
                    {
#if DEBUG
                        if (statusReason == "unequaldisconnect")
                        {
                            RemovePlayerNoNotify(player);
                        }
                        else
#endif
                        RemovePlayer(player);
                        sConn.Tag = null;
                    }
                    if (status == NetConnectionStatus.Connected)
                    {
                        FinalizePlayerAdd(player);
                    }
                    Debug.Log($"Player status: {player.Status}, {statusReason}");
                }
                else if (status == NetConnectionStatus.RespondedAwaitingApproval)
                {
                    //eh.
                }
                else
                {
                    Debug.Log($"Unknown player {sConn} status: {status}, {statusReason}");
                }
            }
            else if (msgType == NetIncomingMessageType.WarningMessage)
            {
                var str = msg.ReadString();
                if (!str.StartsWith("Received unhandled library message Acknowledge"))
                {
                    Debug.LogWarning(str);
                }
            }
            else if (msgType == NetIncomingMessageType.Error)
            {
                Debug.LogException(new Exception(msg.ReadString())
                {
                    Source = "Server.Lidgren.PlayerCallback [Errored Lidgren Message]"
                });                                                                                                                         //this should really never happen...
            }

            NetMessage.RecycleMessage(msg);
        }