StartClient() private method

Starts the client. Asks the server for permission to join. Then runs in a loop, constantly looking for new instructions, such as Leave, Joined, and Command. It deserializes the instruction and passes it on to ReceiveInstruction method.
private StartClient ( ) : void
return void
Exemplo n.º 1
0
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            base.Initialize();
            _networkClient = new NetworkClient("127.0.0.1", 7000);
            _networkClient.StartClient();
            var packet = new LoginPacket()
            {
                MsgType  = (byte)Packets.LoginPacket,
                Username = Encoding.UTF8.GetBytes("test"),
                Password = Encoding.UTF8.GetBytes("test")
            };

            _networkClient.EnqueuePacket(packet);
            _networkClient.OnLoginResponsePacket += OnLoginResponsePacket;
            //_networkClient.OnNewPlayerPacket += OnNewPlayerPacket;
            //_networkClient.OnEntitySpawnPacket += OnEntitySpawnPacket;
            //// Send a login packet to server
            //var packet = new LoginPacket()
            //{
            //    MsgType = (byte)Packets.LOGIN_PACKET,
            //    Username = "******".AsSpan(),
            //    PlayerId = _networkClient.PlayerId
            //};
            //_networkClient.EnqueuePacket(packet);
        }
Exemplo n.º 2
0
    public void Button()
    {
        string    portString = portInput.text;
        string    ipString   = ipInput.text;
        int       port;
        IPAddress ip;

        if (portString == string.Empty && ipString == string.Empty)
        {
            port = 7777;
            ip   = NetworkComponent.GetLocalIPAddress();
        }
        else
        {
            if (!int.TryParse(portString, out port))
            {
                Debug.LogWarning("portti meni vituix");
                return;
            }

            if (!IPAddress.TryParse(ipString, out ip))
            {
                Debug.LogWarning("ip meni vituix");
                return;
            }
        }

        client.StartClient(ip, port);
    }
Exemplo n.º 3
0
    void StartClient(string name, string address, int port)
    {
        MainMenu.instance.ToggleThisMenu(false);
        NetworkClient client = Instantiate(clientPrefab, Vector3.zero, Quaternion.identity).GetComponent <NetworkClient>();

        client.name = clientPrefab.name;
        client.StartClient(name, port);
    }
Exemplo n.º 4
0
        private void InitializeNetwork()
        {
            var serverCfg = LauncherConfig.GetInstance.ServerConfig;

            networkClient               = new NetworkClient(serverCfg.LogginAddress.Split(':')[0], int.Parse(serverCfg.LogginAddress.Split(':')[1]));
            networkClient.OnError      += NetworkClient_OnError;
            networkClient.OnConnected  += NetworkClient_OnConnected;
            networkClient.ClientEvents += NetworkClient_ClientEvents;
            (new Thread(() => {
                networkClient.StartClient();
            })).Start();
        }
Exemplo n.º 5
0
        public void CanDisconnect_DisconnectedWithoutCrashingServer()
        {
            #region Server Initialization

            //server
            AsyncTcp      serverTransport = new AsyncTcp();
            NetworkServer server          = new NetworkServer(2402, 5, 4095, serverTransport);
            server.StartServer();

            #endregion Server Initialization

            Thread.Sleep(1000);

            #region Clients Initialization

            //client 1
            AsyncTcp      client1Transport = new AsyncTcp();
            NetworkClient client1          = new NetworkClient("127.0.0.1", 2402, 4095, client1Transport);

            //client 2
            AsyncTcp      client2Transport = new AsyncTcp();
            NetworkClient client2          = new NetworkClient("127.0.0.1", 2402, 4095, client2Transport);

            //client3
            AsyncTcp      client3Transport = new AsyncTcp();
            NetworkClient client3          = new NetworkClient("127.0.0.1", 2402, 4095, client3Transport);

            client1.StartClient();
            client2.StartClient();
            client3.StartClient();

            #endregion Clients Initialization

            Thread.Sleep(1000);

            client1.StopClient();

            Thread.Sleep(1000);

            Assert.IsTrue(!client1.IsActive() && server.IsServerActive());
        }
Exemplo n.º 6
0
        public bool StartConnection(IPAddress inIpAddress, int inPort = 11000)
        {
            EndConnection();

            if (_connectionLinkInProgress == false && _connectionEstablished == false)
            {
                _connectionLinkInProgress = true;
                try
                {
                    _client.StartClient(inIpAddress, inPort);
                    _connectionEstablished = true;
                }
                catch (Exception e)
                {
                    // Log message
                }
                finally
                {
                    _connectionLinkInProgress = false;
                }
            }
            return(false);
        }
Exemplo n.º 7
0
        public void CanConnect_MultipleClientsConnecting_ConnectionEstablishes()
        {
            #region Server Initialization

            //server
            AsyncTcp      serverTransport = new AsyncTcp();
            NetworkServer server          = new NetworkServer(2401, 5, 4095, serverTransport);
            server.StartServer();

            #endregion Server Initialization

            Thread.Sleep(1000);

            #region Clients Initialization

            //client 1
            AsyncTcp      client1Transport = new AsyncTcp();
            NetworkClient client1          = new NetworkClient("127.0.0.1", 2401, 4095, client1Transport);

            //client 2
            AsyncTcp      client2Transport = new AsyncTcp();
            NetworkClient client2          = new NetworkClient("127.0.0.1", 2401, 4095, client2Transport);

            //client3
            AsyncTcp      client3Transport = new AsyncTcp();
            NetworkClient client3          = new NetworkClient("127.0.0.1", 2401, 4095, client3Transport);

            client1.StartClient();
            client2.StartClient();
            client3.StartClient();

            #endregion Clients Initialization

            Thread.Sleep(1000);

            Assert.IsTrue(client1.IsActive() && client2.IsActive() && client3.IsActive());
        }
Exemplo n.º 8
0
        public void SendPacket_BothSidesReceives()
        {
            bool serverReceivedPacket = false;
            bool clientReceivedPacket = false;

            #region Server Initialization

            //server
            AsyncTcp      serverTransport = new AsyncTcp();
            NetworkServer server          = new NetworkServer(2403, 5, 4095, serverTransport);
            server.StartServer();

            server.RegisterHandler(0, (connection, packet) =>
            {
                string msg = packet.ReadString();
                int msg2   = packet.ReadInteger();
                float msg3 = packet.ReadFloat();

                if (msg == "I Am Alive!" && msg2 == 12345 && msg3 == 123.456f)
                {
                    serverReceivedPacket = true;
                }
            });

            #endregion Server Initialization

            Thread.Sleep(1000);

            #region Clients Initialization

            //client 1
            AsyncTcp      client1Transport = new AsyncTcp();
            NetworkClient client1          = new NetworkClient("127.0.0.1", 2403, 4095, client1Transport);

            //client 2
            AsyncTcp      client2Transport = new AsyncTcp();
            NetworkClient client2          = new NetworkClient("127.0.0.1", 2403, 4095, client2Transport);

            //client3
            AsyncTcp      client3Transport = new AsyncTcp();
            NetworkClient client3          = new NetworkClient("127.0.0.1", 2403, 4095, client3Transport);

            client1.StartClient();
            client2.StartClient();
            client3.StartClient();

            client1.RegisterHandler(0, packet =>
            {
                string msg = packet.ReadString();
                int msg2   = packet.ReadInteger();
                float msg3 = packet.ReadFloat();

                if (msg == "I Am Alive!" && msg2 == 12345 && msg3 == 123.456f)
                {
                    clientReceivedPacket = true;
                }
            });

            #endregion Clients Initialization

            Thread.Sleep(1000);

            #region Server Sending Data

            Packet serverPacket = new Packet();
            serverPacket.WriteString("I Am Alive!");
            serverPacket.WriteInteger(12345);
            serverPacket.WriteFloat(123.456f);

            server.NetworkSendEveryone(0, serverPacket);

            #endregion Server Sending Data

            #region Client Sending Data

            Packet clientPacket = new Packet();
            clientPacket.WriteString("I Am Alive!");
            clientPacket.WriteInteger(12345);
            clientPacket.WriteFloat(123.456f);

            client1.NetworkSend(0, clientPacket);

            #endregion Client Sending Data

            Thread.Sleep(2000);

            Assert.IsTrue(serverReceivedPacket && clientReceivedPacket);
        }
Exemplo n.º 9
0
 // Use this for initialization
 void Start()
 {
     client.StartClient();
 }