public void ServerThread(object obj) { var clientId = 0; lock (_graphics) { clientId = nextClientId++; } try { using (TcpClient client = (TcpClient)obj) { StreamReader reader = new StreamReader(client.GetStream(), Encoding.UTF8); while (client.Connected) { var line = reader.ReadLine(); if (!String.IsNullOrWhiteSpace(line)) { Logger.LogMessage("got message.."); Graphic request = JsonConvert.DeserializeObject <Graphic>(line); SendGraphic(request, clientId); Logger.LogMessage("sent graphic.."); } } } } catch (Exception err) { // maybe log stuff here.. Logger.LogMessage(String.Format("Exception: {0}", err)); return; } finally { lock (_graphics) { // lost connection, remove this connection's graphics foreach (var gid in _graphics.Keys.ToArray()) { InternalGraphic g = _graphics[gid]; if (g.ClientId == clientId) { _graphics.Remove(gid); } } } } }
public void Start() { this._listener.Start(); this._renderer.Start(this); lock (_graphics) { Logger.LogMessage("JSON server thread startup"); var version = Assembly.GetEntryAssembly().GetName().Version; var banner = new Graphic { TTL = 15, Id = "_", Color = "yellow", Size = GraphicType.FONT_LARGE, X = 30, Y = 130, Text = $"/EDMC Overlay V{version} /" }; var intro = new InternalGraphic(banner, -1); _graphics.Add(banner.Id, intro); } while (true) { foreach (var thread in _threads.ToArray()) { if (thread.Join(10)) { _threads.Remove(thread); } } var client = _listener.AcceptTcpClient(); Logger.LogMessage(String.Format("New connection from {0}", client.Client.RemoteEndPoint)); Thread conn = new Thread(new ParameterizedThreadStart(ServerThread)); _threads.Add(conn); conn.Start(client); } }
public void ServerThread(object obj) { var clientId = 0; lock (_graphics) { clientId = nextClientId++; } try { using (TcpClient client = (TcpClient)obj) { StreamReader reader = new StreamReader(client.GetStream(), Encoding.UTF8); bool lostConnection = false; while (client.Connected && !lostConnection) { if (client.Client.Poll(100 * 1000, SelectMode.SelectRead)) { // poll returned true, we either have some data or the connection was closed by the client String line; do { // the connection should block here if the client is still alive line = reader.ReadLine(); if (line == null) { // Logger.LogMessage(String.Format("client {0} disconnected..", clientId)); lostConnection = true; break; // client disconnected } if (!String.IsNullOrWhiteSpace(line)) { // try and deserialize the buffer. Logger.LogMessage("got message.."); Graphic request = JsonConvert.DeserializeObject <Graphic>(line); ProcessCommand(request, client); SendGraphic(request, clientId); Logger.LogMessage("sent graphic.."); } } while (line != null); } } } } catch (Exception err) { // maybe log stuff here.. Logger.LogMessage(String.Format("Exception: {0}", err)); Interlocked.Increment(ref this.messageErrorCount); return; } finally { lock (_graphics) { // lost connection, remove this connection's graphics foreach (var gid in _graphics.Keys.ToArray()) { InternalGraphic g = _graphics[gid]; if (g.ClientId == clientId) { _graphics.Remove(gid); } } } } }