public void CleanUntilLast()
        {
            var sourceList = this.m_tcpClientList;

            try
            {
                Monitor.TryEnter(sourceList, 1000);
                var       list   = new List <TcpClient>();
                TcpClient client = null;


                while (!sourceList.IsEmpty)
                {
                    if (!sourceList.TryDequeue(out client))
                    {
                        break;
                    }
                    if (sourceList.IsEmpty)
                    {
                        list.Add(client);
                    }
                    else
                    {
                        CtkNetUtil.DisposeTcpClient(client);
                    }
                }

                foreach (var tc in list)
                {
                    sourceList.Enqueue(tc);
                }
            }
            catch (Exception ex) { CtkLog.Write(ex); }
            finally { Monitor.Exit(sourceList); }
        }
        public void CleanDisconnect()
        {
            try
            {
                Monitor.TryEnter(this.TcpClientList, 1000);
                var       list   = new List <TcpClient>();
                TcpClient client = null;
                while (!this.TcpClientList.IsEmpty)
                {
                    if (!this.TcpClientList.TryDequeue(out client))
                    {
                        break;
                    }
                    if (client.Client != null && client.Connected)
                    {
                        list.Add(client);
                    }
                    else
                    {
                        CtkNetUtil.DisposeTcpClient(client);
                    }
                }

                foreach (var tc in list)
                {
                    this.m_tcpClientList.Enqueue(tc);
                }
            }
            catch (Exception ex) { CtkLog.Write(ex); }
            finally { Monitor.Exit(this.TcpClientList); }
        }
Exemplo n.º 3
0
        public void ReceiveRepeat()
        {
            try
            {
                this.IsWaitTcpReceive = true;
                while (this.IsWaitTcpReceive && !this.disposed)
                {
                    var state = new CtkTcpSocketStateEventArgs()
                    {
                        sender     = this,
                        workSocket = this.WorkSocket,//Actively socket 為連線的socket本身
                        buffer     = new byte[1518]
                    };

                    state.dataSize = state.workSocket.Receive(state.buffer, 0, state.buffer.Length, SocketFlags.None);
                    if (state.dataSize == 0)
                    {
                        break;
                    }
                    this.OnReceiveData(state);
                }
            }
            finally
            {
                if (this.ConnSocket != this.WorkSocket)
                {
                    CtkNetUtil.DisposeSocket(this.WorkSocket);
                }
            }
        }
        public void Disconnect()
        {
            if (this.threadNonStopConnect != null)
            {
                this.threadNonStopConnect.Abort();
                this.threadNonStopConnect = null;
            }
            CtkNetUtil.DisposeTcpClient(this.activeWorkClient);

            //一旦結束就死了, 需要重new, 所以清掉event沒問題
            CtkEventUtil.RemoveEventHandlersFromOwningByFilter(this, (dlgt) => true);
        }
Exemplo n.º 5
0
 public void Disconnect()
 {
     CtkNetUtil.DisposeSocket(this.m_connSocket);
 }
Exemplo n.º 6
0
 public void DisposeSelf()
 {
     this.IsWaitTcpReceive = false;
     CtkNetUtil.DisposeSocket(this.m_connSocket);
     CtkNetUtil.DisposeSocket(this.WorkSocket);
 }