/// <summary>
 /// 通知消息
 /// </summary>
 /// <param name="connectInfo"></param>
 public void Notify(ConnectInfo connectInfo)
 {
     _innerListener.Push(connectInfo);
 }
Example #2
0
        public void Push(ConnectInfo connectInfo)
        {
            var ar = box1.BeginInvoke(new Action(() =>
            {
                if (box1.Items.Count >= rowCount)
                {
                    box1.Items.RemoveAt(box1.Items.Count - 1);
                }

                var msgType = ParseMessageType.Info;
                if (!connectInfo.Connected)
                {
                    msgType = ParseMessageType.Error;
                }

                box1.Items.Insert(0,
                    new ParseMessageEventArgs
                    {
                        MessageType = msgType,
                        LineHeader = string.Format("【{0}】 {1}:{2} {3}", connectInfo.ConnectTime, connectInfo.IPAddress, connectInfo.Port, connectInfo.Connected ? "连接" : "断开"),
                        MessageText = string.Format("{0}:{1} {4} {2}:{3}", connectInfo.IPAddress, connectInfo.Port, connectInfo.ServerIPAddress, connectInfo.ServerPort, connectInfo.Connected ? "Connect to" : "Disconnect from"),
                        Source = connectInfo
                    });

                box1.Invalidate();
                control.TabPages[1].Text = "连接信息(" + box1.Items.Count + ")";

                if (writeLog)
                {
                    var item = box1.Items[0];
                    var message = string.Format("{0}\r\n{1}", item.LineHeader, item.MessageText);
                    SimpleLog.Instance.WriteLogForDir("ConnectInfo", message);
                }
            }));

            box1.EndInvoke(ar);
        }
        void server_ClientConnected(object sender, ServerClientEventArgs e)
        {
            var endPoint = (e.Client.RemoteEndPoint as ScsTcpEndPoint);
            container.Write(string.Format("User connection {0}:{1}!", endPoint.IpAddress, endPoint.TcpPort), LogType.Information);
            e.Client.MessageReceived += Client_MessageReceived;
            e.Client.MessageSent += Client_MessageSent;
            e.Client.MessageError += Client_MessageError;

            //处理登入事件
            var connect = new ConnectInfo
            {
                ConnectTime = DateTime.Now,
                IPAddress = endPoint.IpAddress,
                Port = endPoint.TcpPort,
                ServerIPAddress = epServer.IpAddress ?? DnsHelper.GetIPAddress(),
                ServerPort = epServer.TcpPort,
                Connected = true
            };

            MessageCenter.Instance.Notify(connect);
        }
        void server_ClientDisconnected(object sender, ServerClientEventArgs e)
        {
            var endPoint = (e.Client.RemoteEndPoint as ScsTcpEndPoint);
            container.Write(string.Format("User Disconnection {0}:{1}!", endPoint.IpAddress, endPoint.TcpPort), LogType.Error);

            //处理登出事件
            var connect = new ConnectInfo
            {
                ConnectTime = DateTime.Now,
                IPAddress = endPoint.IpAddress,
                Port = endPoint.TcpPort,
                ServerIPAddress = epServer.IpAddress ?? DnsHelper.GetIPAddress(),
                ServerPort = epServer.TcpPort,
                Connected = false
            };

            MessageCenter.Instance.Notify(connect);
        }
        /// <summary>
        /// 通知消息
        /// </summary>
        /// <param name="connectInfo"></param>
        public void Notify(ConnectInfo connectInfo)
        {
            if (_listeners.Count == 0) return;

            MessageListener[] listeners = _listeners.ToArray();
            foreach (MessageListener lstn in listeners)
            {
                try
                {
                    var options = lstn.Options;
                    if (options.PushClientConnect)
                    {
                        lstn.Notify(connectInfo);
                    }
                }
                catch (SocketException ex)
                {
                    RemoveListener(lstn);
                }
                catch (Exception ex)
                {
                    if (OnError != null) OnError(ex);
                }
            }
        }