/// <summary> /// 启动各组服务---已经日志 /// </summary> public void start() { //尝试连接3次数据库 ConnectDB(); #region 与前端设备交互的服务启动 ChannelStart(); #endregion #region 与界面交互的socket服务端启动 try { TcpServer_UI = new ToUI.TcpServer(wrx.XMLObj.UiTcpModel.IP, wrx.XMLObj.UiTcpModel.PORT, "UI"); TcpServer_UI.Start(); TcpServer_UI.OnConnected += new EventHandler <ToUI.ConnectedEventArgs>(TcpServer_UI_OnConnected); TcpServer_UI.OnReceivedData += new EventHandler <ToUI.ReceivedDataEventArgs>(TcpServer_UI_OnReceivedData); TcpServer_UI.OnDisconnected += new EventHandler <ToUI.DisconnectedEventArgs>(TcpServer_UI_OnDisconnected); log.Warn(DateTime.Now + "界面交互服务(" + wrx.XMLObj.UiTcpModel.IP + ":" + wrx.XMLObj.UiTcpModel.PORT + ")启动成功!"); } catch (Exception ex) { log.Warn("tcp服务(" + wrx.XMLObj.UiTcpModel.IP + ":" + wrx.XMLObj.UiTcpModel.PORT + ")启动失败!", ex); throw ex; } #endregion //透传的客户端启动 TCstart(); //访问中心 AccessCenter(); log.Warn("启动工作完成!"); log.Warn("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); }
//界面传过来的召测数据报 void TcpServer_UI_OnReceivedData(object sender, ToUI.ReceivedDataEventArgs e) { try { ToUI.TcpServer tcp = sender as ToUI.TcpServer; ToUI.TcpBussiness.UpdSocket(tcp, e.ClientSocket); ServiceBussiness.WriteQxsd(e.RevData, this); } catch (Exception ex) { log.Warn(ex.ToString()); } }
void TcpServer_UI_OnDisconnected(object sender, ToUI.DisconnectedEventArgs e) { try { ToUI.TcpServer tcp = sender as ToUI.TcpServer; //从连接列表删除 ToUI.TcpBussiness.DelSocket(tcp, e.ClientSocket); } catch (Exception ex) { log.Warn(ex.ToString()); } }
void TcpServer_UI_OnConnected(object sender, ToUI.ConnectedEventArgs e) { try { ToUI.TcpServer tcp = sender as ToUI.TcpServer; //添加入连接列表 ToUI.TcpBussiness.AddSocket(tcp, e.ClientSocket); //第一次连接后,把所有通讯列表发送到客户端界面 ServiceBussiness.SendCommandListState(); } catch (Exception ex) { log.Warn(ex.ToString()); } }
protected override void OnStart(string[] args) { #region Silverlight用socket访问服务,是否有需要安全策略文件请求(服务器的端口必须启动) try { ToUI.TcpServer Silverlight_UI = new ToUI.TcpServer("0.0.0.0", 943, "Silverlight"); Silverlight_UI.Start(); Silverlight_UI.OnReceivedData += new EventHandler <ToUI.ReceivedDataEventArgs>(Silverlight_UI_OnReceivedData); //log.Warn("Silverlight访问服务安全策略服务启动成功!"); } catch (Exception ex) { //log.Warn("Silverlight访问服务安全策略服务启动失败!", ex); throw ex; } #endregion }
static void Main(string[] args) { #region Silverlight用socket访问服务,是否有需要安全策略文件请求(服务器的端口必须启动) try { ToUI.TcpServer Silverlight_UI = new ToUI.TcpServer("0.0.0.0", 943, "Silverlight"); Silverlight_UI.Start(); Silverlight_UI.OnReceivedData += new EventHandler <ToUI.ReceivedDataEventArgs>(Silverlight_UI_OnReceivedData); log.Warn("Silverlight访问服务安全策略服务启动成功!"); } catch (Exception ex) { log.Warn("Silverlight访问服务安全策略服务启动失败!", ex); throw ex; } #endregion Console.ReadLine(); }
/// <summary> /// 从回复队列中回复数据 /// </summary> /// <param name="TS">tcp服务</param> public static void SendData(TcpServer TS) { string ServiceId = TS.ServiceID; ConcurrentQueue <TcpSendData> Qtsd = TS.TQ.Qtsd; List <TcpSocket> Ts = TS.Ts; UIModel uim = new UIModel(); lock (Ts) //lock (ServiceQueue.QUIM) while (ServiceQueue.QUIM.Count > 0) { if (ServiceQueue.QUIM.Count > 100) { for (int i = 0; i < ServiceQueue.QUIM.Count; i++) { ServiceQueue.QUIM.TryDequeue(out uim); } foreach (var item in Ts) { try { item.TCPSOCKET.Send(Encoding.UTF8.GetBytes("++" + DateTime.Now.ToString("MM-dd HH:mm:ss") + "缓冲区数据超100条,服务自动清空减压...")); System.Threading.Thread.Sleep(1); } catch (Exception ex) { ServiceControl.log.Warn(DateTime.Now + ex.ToString()); //经常会出现socket客户端断开,此线程正在执行send方法 } } return; } uim = null; ServiceQueue.QUIM.TryDequeue(out uim); //uim = ServiceQueue.QUIM.Dequeue(); if (uim != null) { foreach (var item in Ts) { try { if (uim.DataType == 1) { item.TCPSOCKET.Send(Encoding.UTF8.GetBytes(uim.EXPLAIN)); } else if (uim.DataType == 2) { item.TCPSOCKET.Send(Encoding.UTF8.GetBytes(uim.EXPLAIN)); } System.Threading.Thread.Sleep(1); } catch (Exception ex) { ServiceControl.log.Warn(DateTime.Now + ex.ToString()); //经常会出现socket客户端断开,此线程正在执行send方法 } } } } }