コード例 #1
0
 public void Close()
 {
     this._running = false;
     this._tcpClient.Close();
     this._server.CloseClient(this);
     int[] keys = new int[this._receiveHandlers.Count];
     try {
         this._receiveHandlers.Keys.CopyTo(keys, 0);
     } catch {
         lock (this._receiveHandlers_lock) {
             keys = new int[this._receiveHandlers.Count];
             this._receiveHandlers.Keys.CopyTo(keys, 0);
         }
     }
     foreach (int key in keys)
     {
         SyncReceive receiveHandler = null;
         if (this._receiveHandlers.TryGetValue(key, out receiveHandler))
         {
             receiveHandler.Wait.Set();
         }
     }
     lock (this._receiveHandlers_lock) {
         this._receiveHandlers.Clear();
     }
 }
コード例 #2
0
        private void OnDataAvailable(DataReadInfo dr)
        {
            SocketMessager messager = SocketMessager.Parse(dr.ResponseStream.ToArray());

            if (string.Compare(messager.Action, SocketMessager.SYS_QUIT.Action) == 0)
            {
                dr.AcceptSocket.Close();
            }
            else if (string.Compare(messager.Action, SocketMessager.SYS_TEST_LINK.Action) != 0)
            {
                ReceiveEventArgs e       = new ReceiveEventArgs(this._receives++, messager, this);
                SyncReceive      receive = null;

                if (this._receiveHandlers.TryGetValue(messager.Id, out receive))
                {
                    this._server._receiveSyncWQ.Enqueue(delegate() {
                        try {
                            receive.ReceiveHandler(this, e);
                        } catch (Exception ex) {
                            this.OnError(ex);
                        } finally {
                            receive.Wait.Set();
                        }
                    });
                }
                else
                {
                    this._server._receiveWQ.Enqueue(delegate() {
                        this.OnReceive(e);
                    });
                }
            }
            this._lastActive = DateTime.Now;
            HandleDataReceived();
        }
コード例 #3
0
    public void Write(SocketMessager messager, ServerSocketReceiveEventHandler receiveHandler, TimeSpan timeout)
    {
        if (!messager._isChangeId)
        {
            messager.Id = -messager.Id;
        }
        SyncReceive syncReceive = null;

        try {
            if (receiveHandler != null)
            {
                syncReceive = new SyncReceive(receiveHandler);
                lock (this._receiveHandlers_lock) {
                    if (!this._receiveHandlers.ContainsKey(messager.Id))
                    {
                        this._receiveHandlers.Add(messager.Id, syncReceive);
                    }
                    else
                    {
                        this._receiveHandlers[messager.Id] = syncReceive;
                    }
                }
            }
            lock (_write_lock) {
                NetworkStream ns = this._tcpClient.GetStream();
                base.Write(ns, messager);
            }
            this._lastActive = DateTime.Now;
            if (syncReceive != null)
            {
                syncReceive.Wait.Reset();
                syncReceive.Wait.WaitOne(timeout, false);
                syncReceive.Wait.Set();
                lock (this._receiveHandlers_lock) {
                    this._receiveHandlers.Remove(messager.Id);
                }
            }
        } catch (Exception ex) {
            this._running = false;
            this.OnError(ex);
            if (syncReceive != null)
            {
                syncReceive.Wait.Set();
                lock (this._receiveHandlers_lock) {
                    this._receiveHandlers.Remove(messager.Id);
                }
            }
        }
    }
コード例 #4
0
        public void Close()
        {
            if (this._running == true)
            {
                this._beginRead.AsyncWaitHandle.Close();

                this._running = false;
                if (this._tcpClient != null)
                {
                    this._tcpClient.Dispose();
                    this._tcpClient = null;
                }
                this.OnClosed();
                this._server.CloseClient(this);
                int[] keys = new int[this._receiveHandlers.Count];
                try {
                    this._receiveHandlers.Keys.CopyTo(keys, 0);
                } catch {
                    lock (this._receiveHandlers_lock) {
                        keys = new int[this._receiveHandlers.Count];
                        this._receiveHandlers.Keys.CopyTo(keys, 0);
                    }
                }
                foreach (int key in keys)
                {
                    SyncReceive receiveHandler = null;
                    if (this._receiveHandlers.TryGetValue(key, out receiveHandler))
                    {
                        receiveHandler.Wait.Set();
                    }
                }
                lock (this._receiveHandlers_lock) {
                    this._receiveHandlers.Clear();
                }
            }
        }
コード例 #5
0
    public AcceptSocket(ServerSocket server, TcpClient tcpClient, int id)
    {
        this._running    = true;
        this._id         = id;
        this._server     = server;
        this._tcpClient  = tcpClient;
        this._lastActive = DateTime.Now;
        this._thread     = new Thread(delegate() {
            while (this._running)
            {
                try {
                    NetworkStream ns = this._tcpClient.GetStream();
                    ns.ReadTimeout   = 1000 * 20;
                    if (ns.DataAvailable)
                    {
                        SocketMessager messager = base.Read(ns);
                        Server.Protocol.debugAppendLog?.Invoke(messager.ToString());
                        if (string.Compare(messager.Action, SocketMessager.SYS_TEST_LINK.Action) != 0)
                        {
                            ServerSocketReceiveEventArgs e = new ServerSocketReceiveEventArgs(this._receives++, messager, this);
                            SyncReceive receive            = null;

                            if (this._receiveHandlers.TryGetValue(messager.Id, out receive))
                            {
                                new Thread(delegate() {
                                    try {
                                        receive.ReceiveHandler(this, e);
                                    } catch (Exception ex) {
                                        this.OnError(ex);
                                    } finally {
                                        receive.Wait.Set();
                                    }
                                }).Start();
                            }
                            else
                            {
                                new Thread(delegate() {
                                    this.OnReceive(e);
                                }).Start();
                            }
                        }
                        this._lastActive = DateTime.Now;
                    }
                    else if (_accepted)
                    {
                        TimeSpan ts = DateTime.Now - _lastActive;
                        if (ts.TotalSeconds > 5)
                        {
                            this.Write(SocketMessager.SYS_TEST_LINK);
                        }
                    }
                    if (!ns.DataAvailable)
                    {
                        Thread.CurrentThread.Join(100);
                    }
                } catch (Exception ex) {
                    this._running = false;
                    this.OnError(ex);
                }
            }
            this.Close();
            this.OnClosed();
        });
        this._thread.Start();
    }
コード例 #6
0
    public void Connect(string hostname, int port)
    {
        if (this._isDisposed == false && this._running == false)
        {
            this._running = true;
            try {
                this._tcpClient = new TcpClient();
                this._tcpClient.Connect(hostname, port);
                this._receiveWQ     = new WorkQueue();
                this._receiveSyncWQ = new WorkQueue();
            } catch (Exception ex) {
                this._running = false;
                this.OnError(ex);
                this.OnClosed();
                return;
            }
            this._receives   = 0;
            this._errors     = 0;
            this._lastActive = DateTime.Now;
            ManualResetEvent waitWelcome = new ManualResetEvent(false);
            this._thread = new Thread(delegate() {
                while (this._running)
                {
                    try {
                        NetworkStream ns = this._tcpClient.GetStream();
                        ns.ReadTimeout   = 1000 * 20;
                        if (ns.DataAvailable)
                        {
                            SocketMessager messager = base.Read(ns);
                            if (string.Compare(messager.Action, SocketMessager.SYS_TEST_LINK.Action) == 0)
                            {
                            }
                            else if (this._receives == 0 &&
                                     string.Compare(messager.Action, SocketMessager.SYS_HELLO_WELCOME.Action) == 0)
                            {
                                this._receives++;
                                this.Write(messager);
                                waitWelcome.Set();
                            }
                            else if (string.Compare(messager.Action, SocketMessager.SYS_ACCESS_DENIED.Action) == 0)
                            {
                                throw new Exception(SocketMessager.SYS_ACCESS_DENIED.Action);
                            }
                            else
                            {
                                ClientSocketReceiveEventArgs e = new ClientSocketReceiveEventArgs(this._receives++, messager);
                                SyncReceive receive            = null;

                                if (this._receiveHandlers.TryGetValue(messager.Id, out receive))
                                {
                                    this._receiveSyncWQ.Enqueue(delegate() {
                                        try {
                                            receive.ReceiveHandler(this, e);
                                        } catch (Exception ex) {
                                            this.OnError(ex);
                                        } finally {
                                            receive.Wait.Set();
                                        }
                                    });
                                }
                                else if (this.Receive != null)
                                {
                                    this._receiveWQ.Enqueue(delegate() {
                                        this.OnReceive(e);
                                    });
                                }
                            }
                            this._lastActive = DateTime.Now;
                        }
                        else
                        {
                            TimeSpan ts = DateTime.Now - _lastActive;
                            if (ts.TotalSeconds > 3)
                            {
                                this.Write(SocketMessager.SYS_TEST_LINK);
                            }
                        }
                        if (!ns.DataAvailable)
                        {
                            Thread.CurrentThread.Join(1);
                        }
                    } catch (Exception ex) {
                        this._running = false;
                        this.OnError(ex);
                    }
                }
                this.Close();
                this.OnClosed();

                try {
                    this._tcpClient.Close();
                } catch { }
                this._tcpClient.Dispose();
                this._tcpClient = null;

                int[] keys = new int[this._receiveHandlers.Count];
                try {
                    this._receiveHandlers.Keys.CopyTo(keys, 0);
                } catch {
                    lock (this._receiveHandlers_lock) {
                        keys = new int[this._receiveHandlers.Count];
                        this._receiveHandlers.Keys.CopyTo(keys, 0);
                    }
                }
                foreach (int key in keys)
                {
                    SyncReceive receiveHandler = null;
                    if (this._receiveHandlers.TryGetValue(key, out receiveHandler))
                    {
                        receiveHandler.Wait.Set();
                    }
                }
                lock (this._receiveHandlers_lock) {
                    this._receiveHandlers.Clear();
                }
                if (this._receiveWQ != null)
                {
                    this._receiveWQ.Dispose();
                }
                if (this._receiveSyncWQ != null)
                {
                    this._receiveSyncWQ.Dispose();
                }
                if (this._closeWait != null)
                {
                    this._closeWait.Set();
                }
            });
            this._thread.Start();
            waitWelcome.Reset();
            waitWelcome.WaitOne(TimeSpan.FromSeconds(5));
        }
    }
コード例 #7
0
    public void Connect(string hostname, int port)
    {
        if (this._isDisposed == false && this._running == false)
        {
            this._running = true;
            try {
                IPAddress[] ips = Dns.GetHostAddresses(hostname);
                if (ips.Length == 0)
                {
                    throw new Exception("无法解析“" + hostname + "”");
                }
                this._remotePoint = new IPEndPoint(ips[0], port);
                this._tcpClient   = new TcpClient();
                this._tcpClient.Connect(this._remotePoint);
            } catch (Exception ex) {
                this._running = false;
                this.OnError(ex);
                this.OnClosed();
                return;
            }
            this._receives   = 0;
            this._errors     = 0;
            this._lastActive = DateTime.Now;
            this._thread     = new Thread(delegate() {
                while (this._running)
                {
                    try {
                        NetworkStream ns = this._tcpClient.GetStream();
                        ns.ReadTimeout   = 1000 * 20;
                        if (ns.DataAvailable)
                        {
                            SocketMessager messager = base.Read(ns);
                            if (string.Compare(messager.Action, SocketMessager.SYS_TEST_LINK.Action) == 0)
                            {
                            }
                            else if (this._receives == 0 &&
                                     string.Compare(messager.Action, SocketMessager.SYS_HELLO_WELCOME.Action) == 0)
                            {
                                this._receives++;
                                this.Write(messager);
                            }
                            else if (string.Compare(messager.Action, SocketMessager.SYS_ACCESS_DENIED.Action) == 0)
                            {
                                throw new Exception(SocketMessager.SYS_ACCESS_DENIED.Action);
                            }
                            else
                            {
                                ClientSocketReceiveEventArgs e = new ClientSocketReceiveEventArgs(this._receives++, messager);
                                SyncReceive receive            = null;

                                if (this._receiveHandlers.TryGetValue(messager.Id, out receive))
                                {
                                    new Thread(delegate() {
                                        try {
                                            receive.ReceiveHandler(this, e);
                                        } catch (Exception ex) {
                                            this.OnError(ex);
                                        } finally {
                                            receive.Wait.Set();
                                        }
                                    }).Start();
                                }
                                else if (this.Receive != null)
                                {
                                    new Thread(delegate() {
                                        this.OnReceive(e);
                                    }).Start();
                                }
                            }
                            this._lastActive = DateTime.Now;
                        }
                        else
                        {
                            TimeSpan ts = DateTime.Now - _lastActive;
                            if (ts.TotalSeconds > 3)
                            {
                                this.Write(SocketMessager.SYS_TEST_LINK);
                            }
                        }
                        if (!ns.DataAvailable)
                        {
                            Thread.CurrentThread.Join(1);
                        }
                    } catch (Exception ex) {
                        this._running = false;
                        this.OnError(ex);
                    }
                }
                this.Close();
                this.OnClosed();
            });
            this._thread.Start();
        }
    }