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); }
void PublishNetworkMessage(NetworkMessageEvent e) { if (NetworkMessageReceived != null) { NetworkMessageReceived?.Invoke(this, e); } }
/// <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); }
/// <summary> /// This handler is /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void HandleMessageReceived(object sender, NetworkMessageEvent e) { Debug.Log("A new network message event is received, add it to the message queue"); ProcessIncommingMessages(e.Data); }