Exemple #1
0
        public void GetRooms()
        {
            try
            {
                Task.Run(() =>
                {
                    NetworkClient.Start();

                    Plugin.log.Info($"Creating message...");
                    NetOutgoingMessage outMsg = NetworkClient.CreateMessage();

                    Version assemblyVersion = Assembly.GetExecutingAssembly().GetName().Version;
                    byte[] version          = new byte[4] {
                        (byte)assemblyVersion.Major, (byte)assemblyVersion.Minor, (byte)assemblyVersion.Build, (byte)assemblyVersion.Revision
                    };

                    outMsg.Write(version);
                    new PlayerInfo(GetUserInfo.GetUserName(), GetUserInfo.GetUserID()).AddToMessage(outMsg);

                    Plugin.log.Info($"Connecting to {ip}:{port}...");

                    NetworkClient.Connect(ip, port, outMsg);
                }).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                ServerHubException?.Invoke(this, e);
                Abort();
            }
        }
        public void GetRooms()
        {
            try
            {
                NetworkClient.Start();

                Misc.Logger.Info($"Creating message...");
                NetOutgoingMessage outMsg = NetworkClient.CreateMessage();
                outMsg.Write(Plugin.pluginVersion);
                new PlayerInfo(GetUserInfo.GetUserName(), GetUserInfo.GetUserID()).AddToMessage(outMsg);

                Misc.Logger.Info($"Connecting to {ip}:{port}...");

                NetworkClient.Connect(ip, port, outMsg);
            }
            catch (Exception e)
            {
                ServerHubException?.Invoke(this, e);
                Abort();
            }
        }
Exemple #3
0
        private IEnumerator RequestPage(int page)
        {
            Connect();
            yield return(new WaitForSecondsRealtime(0.05f));

            ClientDataPacket packet = new ClientDataPacket()
            {
                ConnectionType = ConnectionType.Client, Offset = page
            };

            serverHubConnection.GetStream().Write(packet.ToBytes(), 0, packet.ToBytes().Length);

            yield return(WaitForDoneProcess(15f));

            if (serverHubConnection.Available <= 0)
            {
                try
                {
                    serverHubConnection.Close();
                    ServerHubException.Invoke(this, new Exception("ServerHub is offline"));
                }
                catch (Exception e)
                {
                    Console.WriteLine($"ServerHub Exception: {e}");
                    ServerHubException.Invoke(this, e);
                }
                yield break;
            }

            byte[] bytes = new byte[Packet.MAX_BYTE_LENGTH];

            packet = null;
            if (serverHubConnection.GetStream().Read(bytes, 0, bytes.Length) != 0)
            {
                packet = Packet.ToClientDataPacket(bytes);
            }
            Disconnect();
            ReceivedOnePage.Invoke(packet.Servers, page);
        }
Exemple #4
0
        public void Update()
        {
            if (NetworkClient != null && NetworkClient.Status == NetPeerStatus.Running)
            {
                NetIncomingMessage msg;
                while ((msg = NetworkClient.ReadMessage()) != null)
                {
                    if (NetworkClient.Connections.FirstOrDefault() != null)
                    {
                        ping = NetworkClient.Connections.First().AverageRoundtripTime;
                    }
                    switch (msg.MessageType)
                    {
                    case NetIncomingMessageType.StatusChanged:
                    {
                        NetConnectionStatus status = (NetConnectionStatus)msg.ReadByte();

                        if (status == NetConnectionStatus.Connected)
                        {
                            serverHubCompatible = true;
                            serverHubAvailable  = true;
                            NetOutgoingMessage outMsg = NetworkClient.CreateMessage();
                            outMsg.Write((byte)CommandType.GetRooms);

                            NetworkClient.SendMessage(outMsg, NetDeliveryMethod.ReliableOrdered, 0);
                        }
                        else if (status == NetConnectionStatus.Disconnected)
                        {
                            try
                            {
                                string reason = msg.ReadString();
                                if (reason.Contains("Version mismatch"))
                                {
                                    serverHubCompatible = false;
                                    serverHubAvailable  = true;
                                }
                                else
                                {
                                    serverHubCompatible = false;
                                    serverHubAvailable  = false;
                                }
                                ServerHubException?.Invoke(this, new Exception("ServerHub refused connection! Reason: " + reason));
                            }
                            catch (Exception e)
                            {
                                ServerHubException?.Invoke(this, new Exception("ServerHub refused connection! Exception: " + e));
                            }
                            Abort();
                        }
                    };
                        break;

                    case NetIncomingMessageType.Data:
                    {
                        if ((CommandType)msg.ReadByte() == CommandType.GetRooms)
                        {
                            try
                            {
                                int roomsCount = msg.ReadInt32();

                                availableRooms.Clear();

                                for (int i = 0; i < roomsCount; i++)
                                {
                                    availableRooms.Add(new RoomInfo(msg));
                                }

                                availableRoomsCount = availableRooms.Count;
                                playersCount        = availableRooms.Sum(x => x.players);
                                ReceivedRoomsList?.Invoke(this, availableRooms);
                            }catch (Exception e)
                            {
                                ServerHubException?.Invoke(this, new Exception("Unable to parse rooms list! Exception: " + e));
                            }
                            Abort();
                        }
                    };
                        break;

                    case NetIncomingMessageType.WarningMessage:
                        Plugin.log.Warn(msg.ReadString());
                        break;

                    case NetIncomingMessageType.ErrorMessage:
                        Plugin.log.Error(msg.ReadString());
                        break;

#if DEBUG
                    case NetIncomingMessageType.VerboseDebugMessage:
                    case NetIncomingMessageType.DebugMessage:
                        Plugin.log.Info(msg.ReadString());
                        break;

                    default:
                        Console.WriteLine("Unhandled type: " + msg.MessageType);
                        break;
#endif
                    }
                }
            }
        }
        public void Update()
        {
            if (NetworkClient != null && NetworkClient.Status == NetPeerStatus.Running)
            {
                NetIncomingMessage msg;
                while ((msg = NetworkClient.ReadMessage()) != null)
                {
                    switch (msg.MessageType)
                    {
                    case NetIncomingMessageType.StatusChanged:
                    {
                        NetConnectionStatus status = (NetConnectionStatus)msg.ReadByte();

                        if (status == NetConnectionStatus.Connected)
                        {
                            NetOutgoingMessage outMsg = NetworkClient.CreateMessage();
                            outMsg.Write((byte)CommandType.GetRooms);

                            NetworkClient.SendMessage(outMsg, NetDeliveryMethod.ReliableOrdered, 0);
                        }
                        else if (status == NetConnectionStatus.Disconnected)
                        {
                            ServerHubException?.Invoke(this, new Exception("ServerHub refused connection!"));
                            Abort();
                        }
                    };
                        break;

                    case NetIncomingMessageType.Data:
                    {
                        if ((CommandType)msg.ReadByte() == CommandType.GetRooms)
                        {
                            serverHubAvailable = true;

                            int roomsCount = msg.ReadInt32();

                            availableRooms.Clear();

                            for (int i = 0; i < roomsCount; i++)
                            {
                                availableRooms.Add(new RoomInfo(msg));
                            }

                            ReceivedRoomsList?.Invoke(this, availableRooms);
                            Abort();
                        }
                    };
                        break;

                    case NetIncomingMessageType.VerboseDebugMessage:
                    case NetIncomingMessageType.DebugMessage:
                        Misc.Logger.Info(msg.ReadString());
                        break;

                    case NetIncomingMessageType.WarningMessage:
                        Misc.Logger.Warning(msg.ReadString());
                        break;

                    case NetIncomingMessageType.ErrorMessage:
                        Misc.Logger.Error(msg.ReadString());
                        break;

                    default:
                        Console.WriteLine("Unhandled type: " + msg.MessageType);
                        break;
                    }
                }
            }
        }