Exemple #1
0
        /// <summary>
        /// 关闭端口
        /// </summary>
        /// <param name="remoteIP"></param>
        /// <param name="remotePort"></param>
        /// <returns>返回端口号;为了兼容</returns>
        public int MoxaClose(string remoteIP, int remotePort)
        {
            MOXATcpClient moxaTcpClient         = null;
            bool          bContainMoxaTcpClient = ContainTcpClient(remoteIP, remotePort, out moxaTcpClient);

            if (bContainMoxaTcpClient)
            {
                moxaTcpClient.Stop();
                return(0); //成功返回0
            }
            return(-1);    //失败返回-1
        }
Exemple #2
0
        /// <summary>
        /// 打开moxa连接;存在就直接打开,不存在就新建再打开
        /// </summary>
        /// <param name="remoteIP">此端口需要变化增加4000</param>
        /// <param name="remotePort"></param>
        /// <returns>返回端口号;为了兼容</returns>
        public int MoxaOpen(string remoteIP, int remotePort)
        {
            MOXATcpClient moxaTcpClient         = null;
            bool          bContainMoxaTcpClient = ContainTcpClient(remoteIP, remotePort, out moxaTcpClient);

            if (!bContainMoxaTcpClient)
            {
                moxaTcpClient = new MOXATcpClient(remoteIP, remotePort + MOXA_BASEPORT);
                moxaTcpClient.DataReceived += MoxaTcpClient_DataReceived;
                moxaTcpClient.Connected    += MoxaTcpClient_Connected;
                moxaTcpClient.Disconnected += MoxaTcpClient_Disconnected;
            }
            else
            {
                moxaTcpClient.Stop();//每次打开前,先关闭一下
            }

            if (moxaTcpClient.Start())
            {
                if (!bContainMoxaTcpClient)
                {
                    lstMoxaTcpClient.Add(moxaTcpClient);
                }
                try
                {
                    bool moxaOpenSuccess = moxaOpenAutoResetEvent.WaitOne(600);//(ping*3)*3
                    if (moxaOpenSuccess)
                    {
                        //保存连接
                        return(remotePort);
                    }
                }
                catch (Exception)
                {
                }
            }
            return(-1);//打开失败返回-1
        }
Exemple #3
0
        private bool ContainTcpClient(string remoteIP, int remotePort, out MOXATcpClient moxaTcpClient)
        {
            try
            {
                for (int i = 0; i < lstMoxaTcpClient.Count; i++)
                {
                    int port = lstMoxaTcpClient[i].ServerPort - MOXA_BASEPORT;
                    if (lstMoxaTcpClient[i].ServerIp.Trim().Equals(remoteIP) &&
                        port == remotePort)
                    {
                        moxaTcpClient = lstMoxaTcpClient[i];
                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                InternalLogger.Log.Error(String.Format("判断Moxa连接表中是否包含{0}:{1}出错.{2}",
                                                       remoteIP, remotePort, ex.Message));
            }

            moxaTcpClient = null;
            return(false);
        }