Exemplo n.º 1
0
        //void ConnectionCompleted(IAsyncResult ar)
        //{
        //    Debug.Log("Client Connected");

        //    var client = (PrimeNetClient)ar.AsyncState;
        //    client.EndConnect(ar); // this will block until the TCP connection is completed


        //    // Once connection completes, this will setup this TCP connection to wait for data
        //    // on it's network stream, when data is received, the OnRead method will be called
        //    // this is a non-blocking call, so this method will end once BeginRead starts
        //    Debug.Log("Client connect ended");
        //    client.Read();

        //    _connectFinished.Set();
        //}

        void BeginServerConnection(PrimeNetTransportClient client, IPEndPoint endPoint = null)
        {
            // create a connection thread that tries to connect every 1.5 seconds untill the server becomes available
            Task t = endPoint == null ? new Task(() => ConnectToServer(client)) : new Task(() => ConnectToServer(client, endPoint));

            t.Start();
        }
Exemplo n.º 2
0
        void ConnectToServer(PrimeNetTransportClient client)
        {
            _isConnecting = true;
            ManualResetEvent tryConnect = new ManualResetEvent(false); // this is a wait timer essentially

            while (_isConnecting)
            {
                try
                {
                    client.GetClient().Connect(_conn.HosHostAddress, (int)_conn.Port);
                    client.Read();
                    _isConnecting = false;
                    // _connectFinished.Reset();
                }
                catch (ObjectDisposedException ex)
                {
                    Debug.Log(ex.Message);
                }
                catch (SocketException ex)
                {
                    Debug.Log(ex.Message);
                }

                if (_isConnecting == true)
                {
                    tryConnect.WaitOne(1000); // wait for 1 second
                }
            }
        }
Exemplo n.º 3
0
 public void OnClientDisconnected(PrimeNetTransportClient client)
 {
     Debug.Log("removing network client");
     StatusMessage("Client Diconnected");
     client.DataReceived -= OnDataReceived;
     _clientList.Remove(client);
 }
Exemplo n.º 4
0
        public void OnServerConnect(IAsyncResult ar)
        {
            Debug.Log("Client connecting");

            TcpClient client           = _listener.EndAcceptTcpClient(ar);
            PrimeNetTransportClient nc = new PrimeNetTransportClient(client)
            {
                ClientNumber = _clientList.Count + 1
            };

            _clientList.Add(nc);

            //PrimeNetService.Instance._Text.text = "The client connected: " + nc.ClientID;
            nc.DataReceived += OnDataReceived;

            Debug.Log("Listen for further connections");
            StatusMessage("The server is listening for new connections");

            var message = new PrimeNetMessage()
            {
                MessageBody = nc.ClientNumber.ToString(),
                NetMessage  = EPrimeNetMessage.ClientConnected
            };

            NetworkMessageEvent e = new NetworkMessageEvent(message);

            PublishNetworkMessage(e);

            _listener.BeginAcceptTcpClient(OnServerConnect, null);
        }
Exemplo n.º 5
0
        void ConnectToServer(PrimeNetTransportClient client, IPEndPoint endPoint)
        {
            Debug.Log("Connecting to the server...");
            _isConnecting = true;
            _quitAppEvent.Reset();

            while (_isConnecting)
            {
                try
                {
                    client.GetSocket().Connect(endPoint);
                    client.SocketRead();
                    _isConnecting = false;

                    client.StartHeartbeatTimer();

                    PrimeNetMessage m = new PrimeNetMessage()
                    {
                        NetMessage  = EPrimeNetMessage.ServerConnected,
                        MessageBody = "Server connected",
                    };

                    PublishNetworkMessage(new NetworkMessageEvent(m));
                }
                catch (ObjectDisposedException ex)
                {
                    Debug.Log(ex.Message);
                }
                catch (SocketException ex)
                {
                    Debug.Log(ex.Message);
                }


                if (_isConnecting == true)
                {
                    // this signal is from the outside, if the application is told to
                    // quit, stop this task.  If th task expires, and not connection is there, keep trying to connect
                    // when connected, set _isConnecting to false, which also stops this task
                    var isSignaled = _quitAppEvent.WaitOne(1000);

                    if (isSignaled)
                    {
                        _isConnecting = false;
                    }
                }
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Asyc method that is called whenever the listener accepts a socket connection from a client
        /// </summary>
        /// <param name="ar"></param>
        public void OnServerSocketConnect(IAsyncResult ar)
        {
            Debug.Log("Client connecting to this server: " + _listener);
            if (_listener == null)
            {
                return;
            }

            Debug.Log("The listener is still active");
            Socket socket = _listener.EndAcceptSocket(ar);

            Debug.Log("what is state of socket? " + socket);

            var addressBytes = _conn.HosHostAddress.GetAddressBytes();

            // Debug.Log("Addr1 " + _conn.HosHostAddress.Address);
            // Debug.Log("Addr2 " + _conn.HosHostAddress.GetAddressBytes().ToString());

            PrimeNetTransportClient nc = new PrimeNetTransportClient(socket, true, _conn)
            {
                ClientNumber = _clientList.Count + 1,

                RemoteEndPoint = new IPEndPoint(
                    PrimeNetUtils.StringIPToLong(_conn.HosHostAddress.ToString()),
                    (int)_conn.Port)
            };

            _clientList.Add(nc);

            nc.DataReceived += OnDataReceived;

            Debug.Log("Listen for further connections");
            StatusMessage("The server is listening for new connections");

            var message = new PrimeNetMessage()
            {
                MessageBody = nc.ClientNumber.ToString(),
                NetMessage  = EPrimeNetMessage.ClientConnected
            };

            NetworkMessageEvent e = new NetworkMessageEvent(message);

            PublishNetworkMessage(e);

            _listener.BeginAcceptSocket(OnServerSocketConnect, _listener);
        }
Exemplo n.º 7
0
        public void StartupClient()
        {
            StatusMessage("Is the client check working? " + _conn.IsServer);
            if (_conn.IsServer)
            {
                return;
            }

            StatusMessage("Starting up the network client");
            Debug.Log("Startup client");

            TcpClient client = new TcpClient();
            PrimeNetTransportClient connectedClient = new PrimeNetTransportClient(client, false);

            connectedClient.DataReceived += OnDataReceived;
            _clientList.Add(connectedClient);

            BeginServerConnection(connectedClient);
        }
Exemplo n.º 8
0
        public void StartSocketClient()
        {
            if (_conn.IsServer)
            {
                return;
            }

            StatusMessage("Starting up the network client");
            Debug.Log("Startup client");

            IPEndPoint localEndPoint = new IPEndPoint(PrimeNetUtils.StringIPToLong(_conn.HosHostAddress.ToString()), (int)_conn.Port);
            Socket     sender        = new Socket(_conn.HosHostAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            PrimeNetTransportClient client = new PrimeNetTransportClient(sender, false, _conn)
            {
                RemoteEndPoint = localEndPoint
            };

            client.DataReceived += OnDataReceived;
            _clientList.Add(client);

            BeginServerConnection(client, localEndPoint);
        }