/// <inheritdoc /> public void ConnectTo(string serverAddress, int port) { if (client != null) { if (client.Host == serverAddress && client.Port == port) { Debug.Log($"Client already created: {client.Host}:{client.Port}"); return; } else { Debug.Log($"Disconnecting existing client {client.Host}:{client.Port}"); client.Stop(); client.Connected -= OnClientConnected; client.Disconnected -= OnClientDisconnected; client = null; } } Debug.LogFormat($"Connecting to {serverAddress}:{port}"); client = SocketerClient.CreateSender(SocketerClient.Protocol.TCP, serverAddress, port); client.Connected += OnClientConnected; client.Disconnected += OnClientDisconnected; client.Start(); }
public TCPNetworkConnection(SocketerClient socketerClient, string address, int sourceId = 0) { this.socketerClient = socketerClient; this.sourceId = sourceId; this.address = address; this.lastActiveTimestamp = DateTime.UtcNow; }
/// <summary> /// Call to begin acting as a server listening on the provided port /// </summary> /// <param name="port">port to listen on</param> public void StartListening(int port) { if (server == null) { server = DoStartListening(port); } }
private void OnDisable() { socketer.Stop(); socketer.Connected -= Socketer_Connected; socketer.Disconnected -= Socketer_Disconnected; socketer.Message -= Socketer_Message; socketer = null; }
private void Socketer_Message(SocketerClient sender, MessageEvent e) { // messages come in on a different thread, so we move them to the update thread lock (stateQueue) { stateQueue.Enqueue(e); } }
public SocketEndpoint(SocketerClient socketerClient, TimeSpan timeoutInterval, string address, int sourceId = 0) { this.socketerClient = socketerClient; this.timeoutInterval = timeoutInterval; this.sourceId = sourceId; this.Address = address; this.lastActiveTimestamp = DateTime.UtcNow; }
/// <summary> /// Call to start acting as a client connected to the provided server and port /// </summary> /// <param name="serverAddress">server to connect to</param> /// <param name="port">port to use for communication</param> public void ConnectTo(string serverAddress, int port) { Debug.LogFormat($"Connecting to {serverAddress}:{port}"); client = SocketerClient.CreateSender(SocketerClient.Protocol.TCP, serverAddress, port); client.Connected += OnClientConnected; client.Disconnected += OnClientDisconnected; client.Start(); }
private void OnClientConnected(SocketerClient client, int sourceId, string hostAddress) { Debug.Log("Client connected to " + hostAddress); SocketEndpoint socketEndpoint = new SocketEndpoint(client, timeoutInterval, hostAddress, sourceId); clientConnection = socketEndpoint; socketEndpoint.QueueIncomingMessages(inputMessageQueue); newConnections.Enqueue(socketEndpoint); }
private void OnServerConnected(SocketerClient client, int sourceId, string clientAddress) { Debug.Log("Server connected to " + clientAddress); SocketEndpoint socketEndpoint = new SocketEndpoint(client, timeoutInterval, clientAddress, sourceId); serverConnections[sourceId] = socketEndpoint; socketEndpoint.QueueIncomingMessages(inputMessageQueue); newConnections.Enqueue(socketEndpoint); }
private void OnClientConnected(SocketerClient client, MessageEvent e) { Debug.Log("Client connected to " + e.SourceHost); SocketEndpoint socketEndpoint = new SocketEndpoint(client, timeoutInterval, e.SourceHost, e.SourceId); clientConnection = socketEndpoint; socketEndpoint.QueueIncomingMessages(inputMessageQueue); newConnections.Enqueue(socketEndpoint); }
private void OnServerConnected(SocketerClient client, int sourceId, string clientAddress) { Debug.Log("Server connected to " + clientAddress); TCPNetworkConnection connection = new TCPNetworkConnection(client, clientAddress, sourceId); serverConnections[sourceId] = connection; connection.SetIncomingMessageQueue(inputMessageQueue); newConnections.Enqueue(connection); }
protected void DoStopListening(ref SocketerClient listener) { if (listener != null) { Debug.Log("Stopped listening on port " + listener.Port); listener.Stop(); listener = null; } }
private void OnClientDisconnected(SocketerClient client, int sourceId, string hostAddress) { if (clientConnection != null) { Debug.Log("Client disconnected"); clientConnection.StopIncomingMessageQueue(); oldConnections.Enqueue(clientConnection); clientConnection = null; } }
private void Socket_Message(SocketerClient arg1, MessageEvent e) { // This event is sent to all socket endpoints. Make sure this message matches the server (connectionId == 0) or the correct client (sourceId == e.SourceId) if (sourceId == 0 || sourceId == e.SourceId) { lastActiveTimestamp = DateTime.UtcNow; IncomingMessage incomingMessage = new IncomingMessage(this, e.Message, e.Message.Length); incomingQueue.Enqueue(incomingMessage); } }
private SocketerClient DoStartListening(int port) { Debug.Log("Listening on port " + port); SocketerClient newServer = SocketerClient.CreateListener(SocketerClient.Protocol.TCP, port); newServer.Connected += OnServerConnected; newServer.Disconnected += OnServerDisconnected; newServer.Start(); return(newServer); }
private void OnClientDisconnected(SocketerClient client, MessageEvent e) { if (clientConnection != null) { Debug.Log("Client disconnected"); clientConnection.StopIncomingMessageQueue(); oldConnections.Enqueue(clientConnection); clientConnection = null; } }
protected virtual void OnServerDisconnected(SocketerClient client, int sourceId, string clientAddress) { TCPNetworkConnection connection; if (serverConnections.TryRemove(sourceId, out connection)) { Debug.Log("Server disconnected from " + clientAddress); connection.SetIncomingMessageQueue(null); oldConnections.Enqueue(connection); } }
protected virtual void OnServerDisconnected(SocketerClient client, MessageEvent e) { SocketEndpoint socketEndpoint; if (serverConnections.TryRemove(e.SourceId, out socketEndpoint)) { Debug.Log("Server disconnected from " + e.SourceHost); socketEndpoint.StopIncomingMessageQueue(); oldConnections.Enqueue(socketEndpoint); } }
private void OnClientConnected(SocketerClient client, int sourceId, string hostAddress) { Debug.Log("Client connected to " + hostAddress); TCPNetworkConnection connection = new TCPNetworkConnection(client, hostAddress, sourceId); if (!AttemptReconnectWhenClient) { connection.StopConnectionAttempts(); } clientConnection = connection; connection.SetIncomingMessageQueue(inputMessageQueue); newConnections.Enqueue(connection); }
private void OnEnable() { if (Direction == SocketerClient.ProtocolDirection.Listener) { socketer = SocketerClient.CreateListener(Protocol, Port); } else { socketer = SocketerClient.CreateSender(Protocol, string.IsNullOrEmpty(Host) ? "127.0.0.1" : Host, Port); } socketer.Connected += Socketer_Connected; socketer.Disconnected += Socketer_Disconnected; socketer.Message += Socketer_Message; socketer.Start(); }
/// <summary> /// Disconnect all connections /// </summary> public void DisconnectAll() { // Make sure the client stops before attempting to disconnect // anything else. Otherwise, a race condition could cause the client // to automatically reconnect to the disconnected endpoints. if (client != null) { client.Stop(); client = null; } if (clientConnection != null) { clientConnection.Disconnect(); clientConnection = null; } foreach (SocketEndpoint endpoint in serverConnections.Values) { endpoint.Disconnect(); } serverConnections.Clear(); }
private void OnClientDisconnected(SocketerClient client, int sourceId, string hostAddress) { if (clientConnection != null) { Debug.Log("Client disconnected"); clientConnection.SetIncomingMessageQueue(null); oldConnections.Enqueue(clientConnection); clientConnection = null; } if (!AttemptReconnectWhenClient) { Debug.Log("Stopping subscriptions to disconnected client"); client.Stop(); client.Connected -= OnClientConnected; client.Disconnected -= OnClientDisconnected; if (this.client == client) { Debug.Log("Clearing client cache"); this.client = null; } } }
public void StartServer() { socketer = SocketerClient.CreateListener(Protocol, Port); Init(); }
public void StartClient() { socketer = SocketerClient.CreateSender(Protocol, string.IsNullOrEmpty(Host) ? "127.0.0.1" : Host, Port); Init(); }
/// <summary> /// Gets the IP of this machine. Suitable for use as a Host on another Socketer. /// </summary> /// <returns></returns> public static string GetLocalIPAddress() { return(SocketerClient.GetLocalIPAddress()); }
private void Socketer_Disconnected(SocketerClient sender, int sourceId, string remoteHost) { Disconnected?.Invoke(this, sourceId, remoteHost); }