コード例 #1
0
ファイル: SocketBase.cs プロジェクト: zqlovejyc/Socket
        protected virtual void Listenning()
        {
            while (true)
            {
                Thread.Sleep(200);
                foreach (var keyValue in this._connections)
                {
                    //心跳检测

                    if (!this.HeartbeatCheck(keyValue.Value))
                    {
                        Connection remConn;
                        this._connections.TryRemove(keyValue.Key, out remConn);
                        continue;
                    }
                    try
                    {
                        this.Receive(keyValue.Value); //接收数据
                        this.Send(keyValue.Value);    //发送数据
                    }
                    catch (Exception ex)
                    {
                        keyValue.Value.NetworkStream.Close();
                        ConCloseMessagesEventArgs ce = new ConCloseMessagesEventArgs(keyValue.Value.ConnectionName, new ConcurrentQueue <Message>(keyValue.Value.messageQueue), ex);
                        this.OnConnectionClose(this, ce);
                    }
                }
            }
        }
コード例 #2
0
ファイル: SocketBase.cs プロジェクト: zqlovejyc/Socket
 private void OnConnectionClose(object sender, ConCloseMessagesEventArgs e)
 {
     if (ConnectionClose != null)
     {
         this.ConnectionClose(sender, e);
     }
 }
コード例 #3
0
        /// <summary>
        /// 触发连接关闭
        /// </summary>
        /// <param name="connectionName"></param>
        /// <param name="messageQueue"></param>
        /// <param name="ex"></param>
        protected void connClose(string connectionName, List <Message> messageQueue, Exception ex)
        {
            ConCloseMessagesEventArgs ce = new ConCloseMessagesEventArgs(connectionName,
                                                                         messageQueue, ex);

            this.OnConnectionClose(this, ce);
        }
コード例 #4
0
ファイル: SocketBase.cs プロジェクト: zqlovejyc/Socket
        /// <summary>
        /// 心跳检测
        /// </summary>
        private bool HeartbeatCheck(Connection connection)
        {
            bool bol = false;

            byte[] buffer = new Message(Message.CommandType.Seartbeat, "心跳包,可忽略").ToBytes();
            try
            {
                lock (this)
                {
                    connection.NetworkStream.Write(buffer, 0, buffer.Length);
                    bol = true;
                }
            }
            catch (Exception ex) //连接已经断开
            {
                connection.NetworkStream.Close();
                ConCloseMessagesEventArgs ce = new ConCloseMessagesEventArgs(connection.ConnectionName, new ConcurrentQueue <Message>(connection.messageQueue), ex);
                this.OnConnectionClose(this, ce);
            }
            return(bol);
        }