コード例 #1
0
        private void Receive(Socket handler)
        {
            try
            {
                StateObject state = new StateObject();
                state.workSocket = handler;
                state.Type       = SocketObjType.Server;

                if (this.SocketErrAccur != null)
                {
                    SocketErrAccur.BeginInvoke(2, LogLevel.Detail, "Begin Receive", null, this);
                }

                handler.ReceiveTimeout = 13 * 1000;
                handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                                     new AsyncCallback(ReceiveCallback), state);
            }
            catch (Exception e)
            {
                if (this.SocketErrAccur != null)
                {
                    SocketErrAccur.BeginInvoke(2, LogLevel.Warning, "Receive Err:" + e.Message, null, this);
                }
            }
        }
コード例 #2
0
        private void DoAcceptLoop(Socket listen)
        {
            if (this.SocketErrAccur != null)
            {
                SocketErrAccur.BeginInvoke(2, LogLevel.Warning, "DoAcceptLoop", null, this);
            }

            while (this.bDoAcceptLoop)
            {
                try
                {
                    allDone.Reset();

                    Listener.BeginAccept(new AsyncCallback(AcceptCallback), listen);
                }
                catch (Exception e)
                {
                    if (this.SocketErrAccur != null)
                    {
                        SocketErrAccur.BeginInvoke(2, LogLevel.Warning, "DoAcceptLoop Err:" + e.Message, null, this);
                    }
                }
                finally
                {
                    allDone.WaitOne();
                }
            }
        }
コード例 #3
0
        private void AcceptCallback(IAsyncResult ar)
        {
            try
            {
                allDone.Set();

                Socket listen = (Socket)ar.AsyncState;

                Socket handler = listen.EndAccept(ar);

                if (this.SocketErrAccur != null)
                {
                    SocketErrAccur.BeginInvoke(2, LogLevel.Detail, "AcceptCallback:" + handler.RemoteEndPoint.ToString(), null, this);
                }

                Receive(handler);
            }
            catch (Exception e)
            {
                if (this.SocketErrAccur != null)
                {
                    SocketErrAccur.BeginInvoke(2, LogLevel.Warning, "AcceptCallback Err:" + e.Message, null, this);
                }
            }
        }
コード例 #4
0
 private void AcceptLoopStopCallBack(IAsyncResult ar)
 {
     if (this.SocketErrAccur != null)
     {
         SocketErrAccur.BeginInvoke(2, LogLevel.Detail, "AcceptLoopStopCallBack", null, this);
     }
 }
コード例 #5
0
        private void ConnectSync(Socket handle)
        {
            try
            {
                handle.Connect(IPAddress.Parse("127.0.0.1"),
                               plinkConfig.LoopBackPingPort);

                byte[] byteData = Encoding.ASCII.GetBytes(PingData + "<EOF>");
                handle.SendTimeout = 10 * 1000;
                handle.Send(byteData, 0, byteData.Length, 0);

                if (this.SocketErrAccur != null)
                {
                    SocketErrAccur.BeginInvoke(2, LogLevel.Detail, "Sending Ping Data:" + PingData, null, this);
                }
                //关闭后收不到数据
                //handle.Shutdown(SocketShutdown.Send);
            }
            catch (Exception e)
            {
                if (this.SocketErrAccur != null)
                {
                    SocketErrAccur.BeginInvoke(2, LogLevel.Warning, "ConnectSync Err:" + e.Message, null, this);
                }
            }
        }
コード例 #6
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            try
            {
                String content = String.Empty;

                StateObject state   = (StateObject)ar.AsyncState;
                Socket      handler = state.workSocket;

                int bytesRead = handler.EndReceive(ar);
                if (bytesRead > 0)
                {
                    state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));

                    content = state.sb.ToString();
                    if (content == this.PingData + "<EOF>")
                    {
                        HasReceived = true;
                        if (this.SocketErrAccur != null)
                        {
                            SocketErrAccur.BeginInvoke(2, LogLevel.Detail, "ReceiveCallback data:" + this.PingData.ToString(), null, this);
                        }
                    }
                    handler.Shutdown(SocketShutdown.Receive);
                    handler.Close();

                    //if (content.IndexOf("<EOF>") > -1)
                    //else
                    //{
                    //    handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                    //    new AsyncCallback(ReceiveCallback), state);
                    //}
                }
                else
                if (this.SocketErrAccur != null)
                {
                    SocketErrAccur.BeginInvoke(2, LogLevel.Warning, "ReceiveCallback No Data Received", null, this);
                }
            }
            catch (Exception e)
            {
                if (this.SocketErrAccur != null)
                {
                    SocketErrAccur.BeginInvoke(2, LogLevel.Warning, "ReceiveCallback Err:" + e.Message, null, this);
                }
            }
        }
コード例 #7
0
        private void PingWaitTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            PingWaitTimer.Enabled = false;
            PingData = DateTime.Now.ToString("yyyy-MM-dd HH:mm:sssssss");

            try
            {
                this.StopClient();
            }
            catch (Exception err)
            {
                if (SocketErrAccur != null)
                {
                    SocketErrAccur.BeginInvoke(2, LogLevel.Warning, "PingWaitTimer_Elapsed Err" + err.Message, null, this);
                }
            }

            if (HasReceived)
            {
                HasReceived     = false;
                PingFailedTimes = 0;
                if (SocketErrAccur != null)
                {
                    SocketErrAccur.BeginInvoke(2, LogLevel.Warning, "PingTest success", null, this);
                }
            }
            else
            {
                PingFailedTimes++;
                if (SocketErrAccur != null)
                {
                    SocketErrAccur.BeginInvoke(2, LogLevel.Warning, "PingTest failed " + PingFailedTimes + " times", null, this);
                }

                if (PingFailedTimes >= 3)
                {
                    PingFailedTimes = 0;
                    if (PingFaildThree != null)
                    {
                        PingFaildThree.BeginInvoke(null, this);
                    }
                }
            }
        }
コード例 #8
0
        private void SendCallback(IAsyncResult ar)
        {
            try
            {
                // Retrieve the socket from the state object.
                Socket handler = (Socket)ar.AsyncState;
                // Complete sending the data to the remote device.
                int bytesSent = handler.EndSend(ar);

                handler.Shutdown(SocketShutdown.Both);
                handler.Close();
            }
            catch (Exception e)
            {
                if (this.SocketErrAccur != null)
                {
                    SocketErrAccur.BeginInvoke(2, LogLevel.Warning, "SendCallback Err:" + e.Message, null, this);
                }
            }
        }
コード例 #9
0
        private void ConnectCallback(IAsyncResult ar)
        {
            try
            {
                // Retrieve the socket from the state object.
                Socket client = (Socket)ar.AsyncState;

                // Complete the connection.
                client.EndConnect(ar);

                // Signal that the connection has been made.
                connectDone.Set();
                Send(client, PingData);
            }
            catch (Exception e)
            {
                if (this.SocketErrAccur != null)
                {
                    SocketErrAccur.BeginInvoke(2, LogLevel.Warning, "ConnectCallback Err:" + e.Message, null, this);
                }
            }
        }
コード例 #10
0
        private void StartClient()
        {
            try
            {
                PingWaitTimer.Enabled = true;
                Socket Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                if (SocketErrAccur != null)
                {
                    SocketErrAccur.BeginInvoke(2, LogLevel.Detail, "StartClient", null, this);
                }

                DoConnectHandler conn = new DoConnectHandler(ConnectSync);
                arConnect = conn.BeginInvoke(Client, null, Client);
            }
            catch (Exception e)
            {
                if (SocketErrAccur != null)
                {
                    SocketErrAccur.BeginInvoke(2, LogLevel.Warning, "StartClient Err:" + e.Message, null, this);
                }
            }
        }