示例#1
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);
        }
示例#2
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);
        }