コード例 #1
0
		private void Socket_OnReceive(object sender, ClientSocketReceiveEventArgs e) {
			SocketMessager messager = null;
			switch (e.Messager.Action) {
				case "ExecuteDataSet":
					string sql = e.Messager.Arg.ToString();
					DataSet ds = null;
					try {
						ds = ConsoleApp.ExecuteDataSet(this.ConnectionString, sql);
					} catch (Exception ex) {
						this.Socket_OnError(this, new ClientSocketErrorEventArgs(ex, 0));
					}
					messager = new SocketMessager(e.Messager.Action, ds);
					messager.Id = e.Messager.Id;
					this._socket.Write(messager);
					break;
				case "ExecuteNonQuery":
					string sql2 = e.Messager.Arg.ToString();
					int val = 0;
					try {
						val = ConsoleApp.ExecuteNonQuery(this.ConnectionString, sql2);
					} catch (Exception ex) {
						this.Socket_OnError(this, new ClientSocketErrorEventArgs(ex, 0));
					}
					messager = new SocketMessager(e.Messager.Action, val);
					messager.Id = e.Messager.Id;
					this._socket.Write(messager);
					break;
				default:
					Console.WriteLine("[" + DateTime.Now.ToString("MM-dd HH:mm:ss") + "] " + "您当前使用的版本未能实现功能!");
					break;
			}
		}
コード例 #2
0
 protected virtual void OnReceive(ClientSocketReceiveEventArgs e)
 {
     if (this.Receive != null)
     {
         try {
             this.Receive(this, e);
         } catch (Exception ex) {
             this.OnError(ex);
         }
     }
 }
コード例 #3
0
        private void Socket_OnReceive(object sender, ClientSocketReceiveEventArgs e)
        {
            SocketMessager messager = null;

            switch (e.Messager.Action)
            {
            case "ExecuteDataSet":
                string  sql = e.Messager.Arg.ToString();
                DataSet ds  = null;
                try {
                    ds = ExecuteDataSet(sql);
                } catch (Exception ex) {
                    this.Socket_OnError(this, new ClientSocketErrorEventArgs(ex, 0));
                }
                messager    = new SocketMessager(e.Messager.Action, ds);
                messager.Id = e.Messager.Id;
                this._socket.Write(messager);
                break;

            case "ExecuteNonQuery":
                string sql2 = e.Messager.Arg.ToString();
                int    val  = 0;
                try {
                    val = ExecuteNonQuery(sql2);
                } catch (Exception ex) {
                    this.Socket_OnError(this, new ClientSocketErrorEventArgs(ex, 0));
                }
                messager    = new SocketMessager(e.Messager.Action, val);
                messager.Id = e.Messager.Id;
                this._socket.Write(messager);
                break;

            default:
                Lib.Msgbox("您当前使用的版本未能实现功能!");
                break;
            }
        }
コード例 #4
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));
        }
    }
コード例 #5
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();
        }
    }