static void Main(string[] args) { var port = 1055; if (args.Length > 0) { port = int.Parse(args[0]); } var server = new ReactiveListener(port); server.Connections.Subscribe(socket => { Console.WriteLine("New socket connected {0}", socket.GetHashCode()); var protocol = new StringChannel(socket); // Here we hook the "echo" prototocol protocol.Receiver.Subscribe( s => { Console.Write(s); protocol.SendAsync(s).Wait(); }, e => Console.WriteLine(e), () => Console.WriteLine("Socket receiver completed")); socket.Disconnected += (sender, e) => Console.WriteLine("Socket disconnected {0}", sender.GetHashCode()); socket.Disposed += (sender, e) => Console.WriteLine("Socket disposed {0}", sender.GetHashCode()); }); server.Start(); Console.WriteLine("Press Enter to exit"); Console.ReadLine(); }
/// <summary> /// Starts listening for incoming requests and starts all underlying hubs. /// </summary> /// <returns>True on success, false otherwise.</returns> public override bool Start() { if (m_Started) { return(m_Started); } Log.Debug("Starting root hub"); if (!_serviceStarted) { var baseAddress = new Uri("net.tcp://localhost:26760/ScpRootHubService"); var binding = new NetTcpBinding { TransferMode = TransferMode.Streamed, Security = new NetTcpSecurity { Mode = SecurityMode.None } }; _rootHubServiceHost = new ServiceHost(this, baseAddress); _rootHubServiceHost.AddServiceEndpoint(typeof(IScpCommandService), binding, baseAddress); _rootHubServiceHost.Open(); _serviceStarted = true; try { _udpServer.Start(26760); } catch (SocketException ex) { Log.FatalFormat("Couldn't start UDP server: {0}", ex); return(false); } } try { _rxFeedServer.Start(); } catch (SocketException sex) { Log.FatalFormat("Couldn't start native feed server: {0}", sex); return(false); } m_Started |= _scpBus.Start(); m_Started |= _usbHub.Start(); m_Started |= _bthHub.Start(); Log.Debug("Root hub started"); // make some noise =) if (GlobalConfiguration.Instance.IsStartupSoundEnabled) { AudioPlayer.Instance.PlayCustomFile(GlobalConfiguration.Instance.StartupSoundFile); } return(m_Started); }
public void when_connected_then_can_exchange_fixed_length_messages() { var serverReceives = new List <string>(); var clientReceives = new List <string>(); var server = new ReactiveListener(1055); server.Start(); Func <IObservable <byte>, IObservable <string> > parse = socket => from header in socket.Buffer(4) let length = BitConverter.ToInt32(header.ToArray(), 0) let body = socket.Take(length) select Encoding.UTF8.GetString(body.ToEnumerable().ToArray()); Func <string, byte[]> convert = s => { var body = Encoding.UTF8.GetBytes(s); var header = BitConverter.GetBytes(body.Length); var payload = header.Concat(body).ToArray(); return(payload); }; server.Connections.Subscribe(socket => { Console.WriteLine("Server socket created: {0}", socket.GetHashCode()); parse(socket.Receiver).Subscribe( x => serverReceives.Add(x.Trim()), e => Console.WriteLine("Server socket error: {0}", e.Message), () => Console.WriteLine("Server socket completed")); socket.SendAsync(convert("Welcome!")).Wait(); }); var client = new ReactiveClient("127.0.0.1", 1055); Console.WriteLine("Client socket created: {0}", client.GetHashCode()); client.ConnectAsync().Wait(); parse(client.Receiver).Subscribe( x => clientReceives.Add(x.Trim()), e => Console.WriteLine("Client socket error: {0}", e.Message), () => Console.WriteLine("Client socket completed")); client.SendAsync(convert("Hello")).Wait(); Thread.Sleep(100); Assert.Equal(1, serverReceives.Count); Assert.Equal(1, clientReceives.Count); Assert.Equal("Welcome!", clientReceives[0]); Assert.Equal("Hello", serverReceives[0]); server.Dispose(); }
public void when_connected_then_can_exchange_fixed_length_messages() { var serverReceives = new List<string>(); var clientReceives = new List<string>(); var server = new ReactiveListener(1055); server.Start(); Func<IObservable<byte>, IObservable<string>> parse = socket => from header in socket.Buffer(4) let length = BitConverter.ToInt32(header.ToArray(), 0) let body = socket.Take(length) select Encoding.UTF8.GetString(body.ToEnumerable().ToArray()); Func<string, byte[]> convert = s => { var body = Encoding.UTF8.GetBytes(s); var header = BitConverter.GetBytes(body.Length); var payload = header.Concat(body).ToArray(); return payload; }; server.Connections.Subscribe(socket => { Console.WriteLine("Server socket created: {0}", socket.GetHashCode()); parse(socket.Receiver).Subscribe( x => serverReceives.Add(x.Trim()), e => Console.WriteLine("Server socket error: {0}", e.Message), () => Console.WriteLine("Server socket completed")); socket.SendAsync(convert("Welcome!")).Wait(); }); var client = new ReactiveClient("127.0.0.1", 1055); Console.WriteLine("Client socket created: {0}", client.GetHashCode()); client.ConnectAsync().Wait(); parse(client.Receiver).Subscribe( x => clientReceives.Add(x.Trim()), e => Console.WriteLine("Client socket error: {0}", e.Message), () => Console.WriteLine("Client socket completed")); client.SendAsync(convert("Hello")).Wait(); Thread.Sleep(100); Assert.Equal(1, serverReceives.Count); Assert.Equal(1, clientReceives.Count); Assert.Equal("Welcome!", clientReceives[0]); Assert.Equal("Hello", serverReceives[0]); server.Dispose(); }
public override bool Start() { try { if (!_limitInstance.IsOnlyInstance) // existing root hub running as desktop app { throw new RootHubAlreadyStartedException("The root hub is already running, please close the ScpServer first!"); } } catch (UnauthorizedAccessException) // existing root hub running as service { throw new RootHubAlreadyStartedException("The root hub is already running, please stop the ScpService first!"); } if (m_Started) { return(m_Started); } Log.Info("Starting root hub"); if (!_serviceStarted) { var baseAddress = new Uri("net.tcp://localhost:26760/ScpRootHubService"); var binding = new NetTcpBinding(); _rootHubServiceHost = new ServiceHost(this, baseAddress); _rootHubServiceHost.AddServiceEndpoint(typeof(IScpCommandService), binding, baseAddress); _rootHubServiceHost.Open(); _serviceStarted = true; } try { _rxFeedServer.Start(); } catch (SocketException sex) { Log.FatalFormat("Couldn't start native feed server: {0}", sex); return(false); } scpMap.Start(); m_Started |= _scpBus.Start(); m_Started |= _usbHub.Start(); m_Started |= _bthHub.Start(); Log.Info("Root hub started"); return(m_Started); }
public void when_calling_dispose_after_disconnect_then_work() { using (var server = new ReactiveListener(1055)) { var client = new ReactiveClient("127.0.0.1", 1055); server.Start(); client.ConnectAsync().Wait(); client.Disconnect(); client.Dispose(); } }
public void when_disposed_then_complete_connections_observable() { var listener = new ReactiveListener(1055); listener.Start(); bool completed = false; listener.Connections.Subscribe(x => { }, () => completed = true); listener.Dispose(); Assert.True(completed); }
public void when_connecting_then_raises_connected() { using (var server = new ReactiveListener(1055)) { var client = new ReactiveClient("127.0.0.1", 1055); var connected = false; client.Connected += (sender, args) => connected = true; server.Start(); client.ConnectAsync().Wait(); Assert.True(client.IsConnected); Assert.True(connected); } }
public static void AddListener(TCPSocketListener model) { if (listeners.ContainsKey(model.Name)) { listeners[model.Name].Dispose(); listeners.Remove(model.Name); } ReactiveListener server = new ReactiveListener(model.Port); server.Connections.Subscribe(socket => new Observer(socket, model.Name, model.BufferSize)); server.Start(); listeners.Add(model.Name, server); }
/// <summary> /// Starts listening for incoming requests and starts all underlying hubs. /// </summary> /// <returns>True on success, false otherwise.</returns> public override bool Start() { if (m_Started) { return(m_Started); } Log.Debug("Starting root hub"); if (!_serviceStarted) { var baseAddress = new Uri("net.tcp://localhost:26760/ScpRootHubService"); var binding = new NetTcpBinding { TransferMode = TransferMode.Streamed, Security = new NetTcpSecurity { Mode = SecurityMode.None } }; _rootHubServiceHost = new ServiceHost(this, baseAddress); _rootHubServiceHost.AddServiceEndpoint(typeof(IScpCommandService), binding, baseAddress); _rootHubServiceHost.Open(); _serviceStarted = true; } try { _rxFeedServer.Start(); } catch (SocketException sex) { Log.FatalFormat("Couldn't start native feed server: {0}", sex); return(false); } m_Started |= _scpBus.Start(); m_Started |= _usbHub.Start(); m_Started |= _bthHub.Start(); Log.Debug("Root hub started"); return(m_Started); }
static void Main(string[] args) { var server = new ReactiveListener(41337); server.Connections.Subscribe(socket => { Console.WriteLine("New socket connected {0}", socket.GetHashCode()); var protocol = new ProtobufChannel <Person>(socket); protocol.Receiver.Subscribe(person => { if (person != null) { Console.WriteLine("Person {0} {1} connected", person.FirstName, person.LastName); } }); }); server.Start(); Console.WriteLine("Press Enter to exit"); Console.ReadLine(); }
public void Start() { _server.Start(); }
public void when_client_reconnects_then_can_exchange_fixed_length_messages() { var serverReceives = new List<string>(); var clientReceives = new List<string>(); var messageLength = 32; var server = new ReactiveListener(1055); server.Start(); Func<IObservable<byte>, IObservable<string>> parse = socket => from message in socket.Buffer(messageLength) select Encoding.UTF8.GetString(message.ToArray()); Func<string, byte[]> convert = s => { return Encoding.UTF8.GetBytes(new string(' ', messageLength - s.Length) + s); }; server.Connections.Subscribe(socket => { Console.WriteLine("Server socket created: {0}", socket.GetHashCode()); parse(socket.Receiver).Subscribe( x => serverReceives.Add(x.Trim()), e => Console.WriteLine("Server socket error: {0}", e.Message), () => Console.WriteLine("Server socket completed")); socket.SendAsync(convert("Welcome!")).Wait(); }); var client = new ReactiveClient("127.0.0.1", 1055); Console.WriteLine("Client socket created: {0}", client.GetHashCode()); client.ConnectAsync().Wait(); parse(client.Receiver).Subscribe( x => clientReceives.Add(x.Trim()), e => Console.WriteLine("Client socket error: {0}", e.Message), () => Console.WriteLine("Client socket completed")); client.SendAsync(convert("Hello")).Wait(); Thread.Sleep(1200); // Give it time to detect the disconnection from the server. while (client.IsConnected) client.SendAsync(new byte[1]); Assert.Throws<InvalidOperationException>(() => client.SendAsync(convert("World")).Wait()); client.Disconnect(); client.Dispose(); var tcp = new TcpClient("127.0.0.1", 1055); var bytes = convert("World"); tcp.GetStream().Write(bytes, 0, bytes.Length); //client = new TcpClientSocket("127.0.0.1", 1055); //// Reconnect ansd send one more string. //client.ConnectAsync().Wait(); //client.SendAsync(convert("World")).Wait(); Thread.Sleep(1200); //while (serverReceives.Count < 2) // Thread.Sleep(100); // Fails :( Assert.Equal("World", serverReceives.Last()); client.Dispose(); server.Dispose(); }
public void when_client_reconnects_then_can_exchange_fixed_length_messages() { var serverReceives = new List <string>(); var clientReceives = new List <string>(); var messageLength = 32; var server = new ReactiveListener(1055); server.Start(); Func <IObservable <byte>, IObservable <string> > parse = socket => from message in socket.Buffer(messageLength) select Encoding.UTF8.GetString(message.ToArray()); Func <string, byte[]> convert = s => { return(Encoding.UTF8.GetBytes(new string(' ', messageLength - s.Length) + s)); }; server.Connections.Subscribe(socket => { Console.WriteLine("Server socket created: {0}", socket.GetHashCode()); parse(socket.Receiver).Subscribe( x => serverReceives.Add(x.Trim()), e => Console.WriteLine("Server socket error: {0}", e.Message), () => Console.WriteLine("Server socket completed")); socket.SendAsync(convert("Welcome!")).Wait(); }); var client = new ReactiveClient("127.0.0.1", 1055); Console.WriteLine("Client socket created: {0}", client.GetHashCode()); client.ConnectAsync().Wait(); parse(client.Receiver).Subscribe( x => clientReceives.Add(x.Trim()), e => Console.WriteLine("Client socket error: {0}", e.Message), () => Console.WriteLine("Client socket completed")); client.SendAsync(convert("Hello")).Wait(); Thread.Sleep(1200); // Give it time to detect the disconnection from the server. while (client.IsConnected) { client.SendAsync(new byte[1]); } Assert.Throws <InvalidOperationException>(() => client.SendAsync(convert("World")).Wait()); client.Disconnect(); client.Dispose(); var tcp = new TcpClient("127.0.0.1", 1055); var bytes = convert("World"); tcp.GetStream().Write(bytes, 0, bytes.Length); //client = new TcpClientSocket("127.0.0.1", 1055); //// Reconnect ansd send one more string. //client.ConnectAsync().Wait(); //client.SendAsync(convert("World")).Wait(); Thread.Sleep(1200); //while (serverReceives.Count < 2) // Thread.Sleep(100); // Fails :( Assert.Equal("World", serverReceives.Last()); client.Dispose(); server.Dispose(); }