private void OnConnectionClosed(MeterLink e) { this.iConnectionCount--; try { Log.getInstance().Write("断开连接" + e.IP + e.MAC, MsgType.Information); } catch { Log.getInstance().Write("断开连接-OnConnectionClosed失败", MsgType.Information); } Log.getInstance().Write("当前连接数量:" + iConnectionCount.ToString(), MsgType.Information); }
public string Initi(string mac, MeterLink link, byte control, byte[] data, IotProtocolType protocolType = IotProtocolType.RanQiBiao) { if (this._meterLink != null) { return("IsExit"); } this._meterLink = link; this._meterLink.OnConnectClosed += _meterLink_OnConnectClosed; try { //读取当前表的信息并初始化 //Stopwatch sw = new Stopwatch(); //sw.Start(); meter = tms.GetMeter(mac); this._meterLink.OnReviced += _meterLink_OnReviced; //应答并上报数据 ReportData(mac, control, data, protocolType); if (meter == null) { return(""); } //读取当前表任务 List <Task> taskList = tms.GetTaskList(mac); //Log.getInstance().Write(MsgType.Information, string.Format("处理MAC上报数据:{0} Time:{1}", mac, sw.ElapsedMilliseconds / 1000.00)); //Log.getInstance().Write(new OneMeterDataLogMsg(mac, string.Format("处理MAC上报数据:{0} Time:{1}; 时间:{2}", mac, sw.ElapsedMilliseconds / 1000.00, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")))); this._reportWatch.Start(); //创建并启动任务执行线程 this._taskThread = new Thread(new ParameterizedThreadStart(DoTask)); this._reportWatch.Restart(); this._taskThread.Start(taskList); } catch (Exception e) { Log.getInstance().Write(MsgType.Error, "创建IoTMetre对象失败,原因:" + e.Message); Log.getInstance().Write(new OneMeterDataLogMsg(this.MAC, "创建IoTMetre对象失败,原因:" + e.Message + e.ToString())); Log.getInstance().Write(e, MsgType.Error); return("创建IoTMetre对象失败,原因:" + e.Message); } return(""); }
void _meterLink_OnConnectClosed(MeterLink e) { this.meter = null; this.isRunning = false; }
/// <summary> /// 启动侦听。 /// </summary> public void Start() { if (this._tcpLister != null) { return; } try { //IPAddress ipAddress = Dns.GetHostAddresses(ipbound)[0];//IPAddress.Parse(ipbound) _tcpLister = new TcpListener(IPAddress.Any, mPort); _tcpLister.Start(); #if DEBUG Log.getInstance().Write(MsgType.Information, "侦听已启动,等待连接."); //Console.WriteLine("侦听已启动,等待连接..."); #endif } catch (Exception ex) { Log.getInstance().Write("创建监听", MsgType.Information); Log.getInstance().Write(ex, MsgType.Error); return; } Stopwatch watch = new Stopwatch(); watch.Start(); while (true) { if (this._tcpLister == null) { break; } try { Socket ServerSocketForClient = _tcpLister.AcceptSocket(); MeterLink clk = new MeterLink(ServerSocketForClient); if (this.OnReceviceConnected != null) { this.OnReceviceConnected(clk); } clk.OnConnectClosed += OnConnectionClosed; this.iConnectionCount++; watch.Restart(); #if DEBUG Log.getInstance().Write("已接受了一个连接,时间:" + DateTime.Now.ToString() + " IP:" + clk.IP, MsgType.Information); Log.getInstance().Write("当前连接数量:" + iConnectionCount.ToString(), MsgType.Information); #endif } catch (InvalidOperationException ioe)//尚未通过调用 TcpListener.Start() 来启动该侦听器。 { Log.getInstance().Write(ioe, MsgType.Error); } catch (Exception e) { Log.getInstance().Write(e, MsgType.Error); } } watch.Stop(); if (this.OnTcpListenerStoped != null) { this.OnTcpListenerStoped(this, new EventArgs()); } }
/// <summary> /// 接收到一个连接,建立和终端通讯链接对象。 /// </summary> /// <param name="e"></param> private void OnReceviceConnected(MeterLink e) { //e.DataChannelList = this._channelList; }
void _listener_OnConnectClosed(MeterLink e) { //throw new NotImplementedException(); //一个链接关闭了 }