public void OnDataReceived(object sender, DataReceivedEvent e) { Debug.Log("Got a message from a network client "); Debug.Log("what message ? " + e); Debug.Log("what message ? " + e.Data); var netMsg = PrimeNetMessage.Deserialize(e.Data); Debug.Log("message desrialized Body of message is {" + netMsg.MessageBody + "}"); if (netMsg.NetMessage == EPrimeNetMessage.ClientDisconnected || netMsg.NetMessage == EPrimeNetMessage.ServerDisconnected) { Debug.Log("Disconnected"); var id = int.Parse(netMsg.MessageBody); var client = _clientList.Find(i => i.ClientNumber == id); client.DataReceived -= OnDataReceived; _clientList.Remove(client); if (netMsg.NetMessage == EPrimeNetMessage.ServerDisconnected) { StartSocketClient(); // go back into connecting to server } } PublishNetworkMessage(new NetworkMessageEvent(netMsg)); }
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); }
public void ListenForConnections() { // Security.PrefetchSocketPolicy(_ipAddress, _port); Debug.Log("Listen for connections "); try { _listener = new TcpListener(_conn.HosHostAddress, (int)_conn.Port); _listener.Start(); PrimeNetMessage message = new PrimeNetMessage() { NetMessage = EPrimeNetMessage.ServerListening, SenderIP = _conn.HosHostAddress.ToString(), DestinationIP = _conn.HosHostAddress.ToString(), MessageBody = "Asynchronously listening for connections" }; PublishNetworkMessage(new NetworkMessageEvent(message)); _listener.BeginAcceptSocket(OnServerSocketConnect, _listener); } catch (SocketException ex) { Debug.Log("There was a socket exception" + ex.Message); } }
/// <summary> /// Send a direct message to a specific client based on it's unique GUID. The GUID is currently created by the Server /// </summary> /// <param name="id"></param> /// <param name="message"></param> public void Send(Guid id, PrimeNetMessage message) { if (!IsRunning) { return; } _networkServer.DirectMessage(id, message); }
public void StatusMessage(string statusText) { var message = new PrimeNetMessage() { NetMessage = EPrimeNetMessage.Status, MessageBody = statusText }; PublishNetworkMessage(new NetworkMessageEvent(message)); }
/// <summary> /// Look into the Queue to see if there's any messages and return a copy of the first item in the queue /// without removing from the queue /// </summary> /// <returns></returns> public PrimeNetMessage Peek() { PrimeNetMessage nextMessage = null; if (_mQueue.Count > 0) { _mQueue.TryPeek(out nextMessage); } return(nextMessage); }
/// <summary> /// Sends a message to all the connected clients /// </summary> /// <param name="m"></param> public void Broadcast(PrimeNetMessage m) { Debug.Log("Broadcasting message. Running state? " + IsRunning); if (!IsRunning) { return; } Debug.Log("Broadcasting message"); _networkServer.Broadcast(m); }
private void OnSocketRead(IAsyncResult ar) { if (_hbTimer != null) { _hbTimer.ResetTimer(); } Debug.Log("Beginning to receive socket data"); int length = _stream.EndRead(ar); if (length <= 0) { Debug.Log("Someone disconnected"); Debug.Log("The connection is -> " + _connectInfo); var message = new PrimeNetMessage { MessageBody = ClientNumber.ToString(), NetMessage = _connectInfo.IsServer ? EPrimeNetMessage.ClientDisconnected : EPrimeNetMessage.ServerDisconnected, SenderIP = _connectInfo.HosHostAddress.ToString() }; Debug.Log("HB Timer? " + _hbTimer != null); if (_hbTimer != null) { Debug.Log("Stopping timer after disconnect from socket"); _hbTimer.Stop(); } IsActive = false; Debug.Log("Is message being sent?"); PublishDataReceived(new DataReceivedEvent(message.Serialize())); return; } string newMessage = System.Text.Encoding.UTF8.GetString(buffer, 0, length); var receivedData = System.Text.Encoding.Default.GetString(buffer); Debug.Log("Recieved message " + receivedData); PublishDataReceived(new DataReceivedEvent(receivedData)); IsActive = true; // Clear current buffer and look for more data from the server Array.Clear(buffer, 0, buffer.Length); _stream.BeginRead(buffer, 0, buffer.Length, OnSocketRead, null); }
public void Broadcast2(PrimeNetMessage message) { Debug.Log("broadcasting message " + message.MessageBody); StatusMessage("broadcasting message " + message.MessageBody); foreach (var client in _clientList) { if (client.IsConnected()) { message.SenderIP = _conn.HosHostAddress.ToString(); StatusMessage("Sending to a connected client"); client.Send(message.Serialize()); } } }
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; } } } }
public void Disconnect() { if (_hbTimer != null) { _hbTimer.Stop(); } PrimeNetMessage message = new PrimeNetMessage { MessageBody = "Disconnected from remote end", NetMessage = _connectInfo.IsServer ? EPrimeNetMessage.ClientConnected : EPrimeNetMessage.ServerDisconnected, SenderIP = _connectInfo.HosHostAddress.ToString() }; PublishDataReceived(new DataReceivedEvent(message.Serialize())); }
public void Read() { IsActive = true; PrimeNetMessage message = new PrimeNetMessage { MessageBody = ClientID.ToString(), NetMessage = _connectInfo.IsServer ? EPrimeNetMessage.ClientConnected : EPrimeNetMessage.ServerConnected, SenderIP = _connectInfo.HosHostAddress.ToString() }; PublishDataReceived(new DataReceivedEvent(message.Serialize())); Stream.BeginRead(buffer, 0, buffer.Length, OnRead, null); }
/// <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> /// Returns the next avaialble item in the queue, uses an underlying ConcurrentQueue /// </summary> /// <returns></returns> public PrimeNetMessage Dequeue() { PrimeNetMessage result = null; if (!_mQueue.IsEmpty) { if (_mQueue.TryDequeue(out result)) { Debug.Log(string.Format("Concurrrent Dequeue succeeded - ({0}) ", result.MessageBody)); } else { Debug.Log("Concurrrent Dequeue failed"); } } return(result); }
private void OnRead(IAsyncResult ar) { _hbTimer.ResetTimer(); Debug.Log("OnRead: Beginning to receive data"); int length = Stream.EndRead(ar); if (length <= 0) { Debug.Log("OnRead: Someone disconnected"); PrimeNetMessage message = new PrimeNetMessage { MessageBody = ClientID.ToString(), NetMessage = _connectInfo.IsServer ? EPrimeNetMessage.ClientConnected : EPrimeNetMessage.ServerDisconnected, SenderIP = _connectInfo.HosHostAddress.ToString() }; PublishDataReceived(new DataReceivedEvent(message.Serialize())); _hbTimer.Stop(); IsActive = false; return; } if (length == 1) // HB { } string newMessage = System.Text.Encoding.UTF8.GetString(buffer, 0, length); var receivedData = System.Text.Encoding.Default.GetString(buffer); Debug.Log("Recieved message " + receivedData); PublishDataReceived(new DataReceivedEvent(receivedData)); // Clear current buffer and look for more data from the server Array.Clear(buffer, 0, buffer.Length); Stream.BeginRead(buffer, 0, buffer.Length, OnRead, null); }
public void Broadcast(PrimeNetMessage message) { Debug.Log("Broadcasting message to socket {" + message.MessageBody); StatusMessage("broadcasting message " + message.MessageBody); foreach (var client in _clientList) { try { if (client.IsSocketConnected()) { message.SenderIP = _conn.HosHostAddress.ToString(); StatusMessage("Sending to a connected client"); client.SocketSend(message.Serialize()); } } catch (Exception ex) { Debug.Log("Error sending message to a client - " + ex.Message); } } }
public void DirectMessage(Guid id, PrimeNetMessage message) { Debug.Log("Sending a message to specific client {" + message.MessageBody + "}"); // StatusMessage("Sending message " + message.MessageBody); var client = _clientList.Find(x => x.ClientID == id); if (client != null) { try { if (client.IsSocketConnected()) { message.SenderIP = _conn.HosHostAddress.ToString(); StatusMessage("Sending to a connected client"); client.SocketSend(message.Serialize()); } } catch (Exception ex) { Debug.Log("Error sending message to a client - " + ex.Message); } } }
/// <summary> /// When a new message is received from the Network Transport, enqueue and Publish to listener on the /// Network Service /// </summary> /// <param name="message">This a packaged version of a low level net message</param> public void ProcessIncommingMessages(PrimeNetMessage message) { Debug.Log("Enqueuing a new message"); _mQueue.Enqueue(message); PublishMessageAvailable(new EventArgs()); // send a signal that there are new messages }
public PrimeNetMessage GetLastMessage() { return (PrimeNetMessage.Deserialize(_lastMessage.Data)); }
public NetworkMessageEvent(PrimeNetMessage data) { Debug.Log("Creating prime net message"); Data = data; }