Пример #1
0
 /// <summary>
 ///     Starts given peer instance.
 /// </summary>
 /// <param name="peer">Peer to start.</param>
 /// <param name="configuration">Configuration of peer.</param>
 /// <param name="onRegisterHandlers"></param>
 private static T InternalStartPeer <T>([NotNull] T peer, [NotNull] QNetConfiguration configuration,
                                        Action onRegisterHandlers)
     where T : QNetPeer
 {
     if (peer == null)
     {
         throw new ArgumentNullException(nameof(peer));
     }
     if (configuration == null)
     {
         throw new ArgumentNullException(nameof(configuration));
     }
     // first register peer handlers
     peer.RegisterPeerHandlers();
     // then register peer local handlers
     onRegisterHandlers.Invoke();
     // then start peer
     peer.Start(configuration);
     // and at the end, add new running peer
     InternalRunningPeers.Add(peer);
     // rebuild array
     RunningPeers = InternalRunningPeers.ToArray();
     // clear network time
     QNetTime.Frame = 0;
     QNetSimulation.EstimatedServerFrame = 0;
     return(peer);
 }
Пример #2
0
 /// <summary>
 ///     Stops given peer.
 /// </summary>
 private static void InternalStopPeer([NotNull] QNetPeer peer, string stopReason)
 {
     if (peer == null)
     {
         throw new ArgumentNullException(nameof(peer));
     }
     if (!InternalRunningPeers.Contains(peer))
     {
         throw new InvalidOperationException(
                   "Unable to stop given peer. Given peer can't be stopped while not started.");
     }
     // remove this peer
     InternalRunningPeers.Remove(peer);
     // rebuild array
     RunningPeers = InternalRunningPeers.ToArray();
     // and then stop
     peer.Stop(stopReason);
 }
Пример #3
0
        /// <summary>
        ///     Starts client connection.
        /// </summary>
        public static void StartClient([NotNull] string ipAddress, ushort port, string password)
        {
            if (IsClientActive)
            {
                throw new InvalidOperationException(
                          "QNet is unable to start client while there is already active instance of client.");
            }
            if (ipAddress == null)
            {
                throw new ArgumentNullException(nameof(ipAddress));
            }

            var configuration = new QNetConfiguration
            {
                IpAddress      = ipAddress,
                Port           = port,
                MaxConnections = 2
            };

            if (OnClientPrepare == null)
            {
                throw new NullReferenceException("QNet is unable to start client. OnClientPrepare event is not set.");
            }

            Client = new QNetClient();
            Client = InternalStartPeer(Client, configuration, () =>
            {
                QNetHandlerStack.RegisterClientHandlers(Client);
                OnClientRegisterHeaders?.Invoke();
            });
            OnClientPrepare.Invoke(out var nickname, out var token);

            Client.OnMessagePoll += reader =>
            {
                // as the server always send the server frame
                // we need to read that right here
                QNetSimulation.ReceivedServerFrame = reader.ReadUInt32();
                QNetSimulation.AdjustServerFrames  = QNetSimulation.ReceivedServerFrame > QNetTime.ServerFrame;
            };

            Client.OnConnectionReady += (reader, writer) =>
            {
                var tickRate    = reader.ReadInt32();
                var frameNumber = reader.ReadUInt32();

                // set TickRate
                QNetTime.TickRate = tickRate;

                // initialize server frame count
                QNetSimulation.ReceivedServerFrame  = frameNumber;
                QNetSimulation.EstimatedServerFrame = frameNumber;

                // write player data
                writer.WriteString(nickname);
                writer.WriteUInt32(token);
                writer.WriteString(JEMBuild.BuildVersion);
                OnClientReady?.Invoke(reader, writer);
            };

            Client.OnDisconnection += (lostConnection, reason) =>
            {
                // stop peer
                if (InternalRunningPeers.Contains(Client))
                {
                    InternalStopPeer(Client, reason);
                }

                // update active state
                IsClientActive = false;

                // and de-initialize game
                if (!IsHostActive && QNetGameInitializer.GameInitialized && !QNetGameInitializer.GameIsDeInitializing
                    ) // ignore if host, server will de-initialize it anyway
                {
                    QNetGameInitializer.DeInitialize(() =>
                    {
                        OnClientDisconnected?.Invoke(IsHostActive, lostConnection, reason);
                    });
                }
                else
                {
                    if (!QNetGameInitializer.GameInitialized && !QNetGameInitializer.GameIsInitializing)
                    {
                        OnClientDisconnected?.Invoke(IsHostActive, lostConnection, reason);
                    }
                }
            };

            IsClientActive = true;
        }