Пример #1
0
        ///

        /// 異步連接回調函數
        ///
        ///
        void connectedCallback(IAsyncResult iar)
        {
            #region <remarks>
            /// 1、置位IsconnectSuccess
            #endregion </remarks>

            lock (lockObj_IsConnectSuccess)
            {
                Socket client = (Socket)iar.AsyncState;
                try
                {
                    client.EndConnect(iar);
                    IsconnectSuccess = true;
                    ConnReport.On_Connection_Connected("Connected");
                    StartKeepAlive(); //開始KeppAlive檢測
                }
                catch (Exception e)
                {
                    //Console.WriteLine(e.ToString());
                    SockErrorStr     = e.ToString();
                    IsconnectSuccess = false;
                    ConnReport.On_Connection_Disconnected(e.Message);
                }
                finally
                {
                    TimeoutObject.Set();
                }
            }
        }
Пример #2
0
        void IConnectionReport.On_Connection_Disconnected(object MsgObj)
        {
            this._IsConnected = false;
            this.Status       = "Disconnected";
            this.processState = DeviceController.PROCESS_STATE_UNKNOWN;

            if (ConnReport != null)
            {
                ConnReport.On_Connection_Disconnected(_Config);
            }
        }
Пример #3
0
 private void OnReceiveCallback(IAsyncResult ar)
 {
     try
     {
         Socket peerSock  = (Socket)ar.AsyncState;
         int    BytesRead = peerSock.EndReceive(ar);
         if (BytesRead > 0)
         {
             byte[] tmp = new byte[BytesRead];
             Array.ConstrainedCopy(buffer, 0, tmp, 0, BytesRead);
             if (socketDataArrival != null)
             {
                 socketDataArrival(tmp);
             }
         }
         else//對端gracefully關閉一個連接
         {
             if (theSocket.Connected)//上次socket的狀態
             {
                 if (socketDisconnected != null)
                 {
                     //1-重連
                     socketDisconnected();
                     //2-退出,不再執行BeginReceive
                     return;
                 }
             }
         }
         //此處buffer似乎要清空--待實現 zq
         for (int i = 0; i < buffer.Length; i++)// Initial buffer
         {
             buffer[i] = 0;
         }
         theSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(OnReceiveCallback), theSocket);
     }
     catch (Exception ex)
     {
         if (socketDisconnected != null)
         {
             IsconnectSuccess = false;
             theSocket.Disconnect(true);
             ConnReport.On_Connection_Disconnected("Disconnected");
             //socketDisconnected(); //Keepalive檢測網線斷開引發的異常在這裏捕獲
             return;
         }
     }
 }
Пример #4
0
        public void Close()
        {
            try
            {
                if (tcpClient != null)
                {
                    if (tcpClient.Connected)
                    {
                        tcpClient.Close();

                        ConnReport.On_Connection_Disconnected("Close");
                    }
                }
            }
            catch (Exception e)
            {
                logger.Error("[" + Config.DeviceName + "] 關閉異常" + e.StackTrace);
                ConnReport.On_Connection_Error("[" + Config.DeviceName + "] " + e.Message + "\n" + e.StackTrace);
            }
        }
Пример #5
0
        //private void CheckAvailable()
        //{
        //    while (true)
        //    {
        //        bool CheckResult = SocketConnected(SckSPort);

        //        if (!CheckResult)
        //        {
        //            ConnReport.On_Connection_Disconnected("(" + RmIp + ":" + SPort + ") is disconnected.");
        //            break;
        //        }
        //        SpinWait.SpinUntil(() => false, 500);
        //    }
        //}

        //bool SocketConnected(Socket s)
        //{
        //    bool part1 = s.Poll(1000, SelectMode.SelectRead);
        //    bool part2 = (s.Available == 0);
        //    if (part1 && part2)
        //        return false;
        //    else
        //        return true;
        //    //try
        //    //{
        //    //    byte[] testByte = new byte[1];
        //    //    //使用Peek測試連線是否仍存在
        //    //    if (s.Connected && s.Poll(0, SelectMode.SelectRead))
        //    //    {
        //    //        return !(s.Receive(testByte, SocketFlags.Peek) == 0);
        //    //    }
        //    //    else
        //    //    {
        //    //        return true;
        //    //    }
        //    //}
        //    //catch (SocketException se)
        //    //{
        //    //    return false;
        //    //}
        //}

        private void SckSReceiveProc()
        {
            try
            {
                int IntAcceptData;

                byte[] clientData = new byte[RDataLen];

                while (true)
                {
                    if (!SckSPort.Connected)
                    {
                        //logger.Error(Desc + " (" + RmIp + ":" + SPort + ") is disconnected.");
                        ConnReport.On_Connection_Disconnected("(" + RmIp + ":" + SPort + ") is disconnected.");
                        break;
                    }
                    // 程式會被 hand 在此, 等待接收來自 Server 端傳來的資料

                    IntAcceptData = SckSPort.Receive(clientData);

                    // 往下就自己寫接收到來自Server端的資料後要做什麼事唄~^^”

                    string S = Encoding.Default.GetString(clientData, 0, IntAcceptData);
                    //Console.WriteLine(S);
                    //logger.Info("[Rev<--]" + S.Replace("\n", "") + "(From " + Desc + " " + RmIp + ":" + SPort + ")");
                    if (!S.Trim().Equals(""))
                    {
                        ThreadPool.QueueUserWorkItem(new WaitCallback(ConnReport.On_Connection_Message), S);

                        //ConnReport.On_Connection_Message(S);
                    }
                }
            }

            catch (Exception e)
            {
                //logger.Error("(From " + RmIp + ":" + SPort + ")" + e.Message + "\n" + e.StackTrace);
                ConnReport.On_Connection_Error("SckSReceiveProc (" + RmIp + ":" + SPort + ")" + e.Message + "\n" + e.StackTrace);
            }
        }
Пример #6
0
 public void Close()
 {
     port.Close();
     Connected = false;
     ConnReport.On_Connection_Disconnected("Close");
 }