예제 #1
0
        /// <summary>
        /// 监听socket函数,调用
        /// </summary>
        private void ListenerHandleFunc()
        {
            byte[] buffer = new byte[Const.MAX_PACKET_LENGTH];

            while (true)
            {
                if (this.listenerSocket == null || this.listenerSocket.Available < 1)
                {
                    Thread.Sleep(20);
                    continue;
                }

                int length = 0;
                try
                {
                    length = this.listenerSocket.ReceiveFrom(buffer, ref this.epRemotePoint);
                }
                catch (SocketException)
                {
                    //在SendTo的目的端口没有Socket在监听,或原有的监听Socket被关闭时,这里监听会抛出10054异常
                    //Console.WriteLine("SocketException" + ex.ErrorCode);
                    continue;
                }
                //Console.WriteLine(((IPEndPoint)this.epRemotePoint).Port + " " + this.listenPort);
                //如果接收到数据的是从监听端口发出的,将消息写入消息队列
                if (((IPEndPoint)this.epRemotePoint).Port == this.listenPort)
                {
                    PacketInfo packetInfo = new PacketInfo(this.listenerNo, buffer);

                    PhyPortManager.GetInstance().HandleReceive(packetInfo);
                }
            }
        }
예제 #2
0
        /// <summary>
        /// 暴露给上层
        /// </summary>
        /// <param name="buffer">发送内容</param>
        public static Const.EN_RET_CODE Send(byte[] buffer)
        {
            if (buffer.Length > Const.MAX_PACKET_LENGTH)
            {
                return(Const.EN_RET_CODE.EN_RET_PACKET_LENGTH_OVERFOLW);
            }

            Const.EN_RET_CODE retVal = Const.EN_RET_CODE.EN_RET_INIT;
            retVal = PhyPortManager.GetInstance().SendTo(0, buffer);
            return(retVal);
        }
예제 #3
0
 /// <summary>
 /// 获取单例
 /// </summary>
 /// <returns></returns>
 public static PhyPortManager GetInstance()
 {
     if (null == instance)
     {
         lock (objLock)
         {
             //解锁后可能其他线程已经新建了一个实例,所以需要重新判断
             if (null == instance)
             {
                 instance = new PhyPortManager();
             }
         }
     }
     return(instance);
 }
예제 #4
0
        public static Const.EN_RET_CODE InitFromFile(string fileName)
        {
            XmlDocument doc = new XmlDocument();

            try
            {
                doc.Load(fileName);
            }
            catch (Exception)
            {
                Util.Log(Util.EN_LOG_LEVEL.EN_LOG_FATAL, fileName + "拓扑结构文件不存在!");
                return(Const.EN_RET_CODE.EN_RET_FILE_NOT_EXIST);
            }

            Util.Log(Util.EN_LOG_LEVEL.EN_LOG_INFO, "读取拓扑文件");

            try
            {
                XmlElement root = doc.DocumentElement;

                //遍历所有Host
                XmlNodeList hostList = root.GetElementsByTagName("Host");

                foreach (XmlNode hostNode in hostList)
                {
                    string strHostID     = "";
                    string strHostIP     = "";
                    string strHostPort   = "";
                    string strSwitchPort = "";

                    int iHostID     = Const.INVALID_NUM;
                    int iHostPort   = Const.INVALID_NUM;
                    int iSwitchPort = Const.INVALID_NUM;

                    strHostID = ((XmlElement)hostNode).GetElementsByTagName("HostID")[0].InnerText;
                    strHostIP = ((XmlElement)hostNode).GetElementsByTagName("HostIP")[0].InnerText;

                    bool boolTryParseResult = false;

                    boolTryParseResult = int.TryParse(strHostID, out iHostID);
                    if (boolTryParseResult != true)
                    {
                        Util.Log(Util.EN_LOG_LEVEL.EN_LOG_FATAL, "主机ID转换整型变量失败");
                        return(Const.EN_RET_CODE.EN_RET_INT_TRY_PARSE_ERR);
                    }

                    if (iHostID != Program.iCurHostID)
                    {
                        continue;
                    }

                    strHostPort   = ((XmlElement)hostNode).GetElementsByTagName("HostPort")[0].InnerText;
                    strSwitchPort = ((XmlElement)hostNode).GetElementsByTagName("switchPort")[0].InnerText;

                    boolTryParseResult = int.TryParse(strHostPort, out iHostPort);
                    if (boolTryParseResult != true)
                    {
                        Util.Log(Util.EN_LOG_LEVEL.EN_LOG_FATAL, "主机端口号转换整型变量失败");
                        return(Const.EN_RET_CODE.EN_RET_INT_TRY_PARSE_ERR);
                    }

                    boolTryParseResult = int.TryParse(strSwitchPort, out iSwitchPort);
                    if (boolTryParseResult != true)
                    {
                        Util.Log(Util.EN_LOG_LEVEL.EN_LOG_FATAL, "交换机端口号转换整型变量失败");
                        return(Const.EN_RET_CODE.EN_RET_INT_TRY_PARSE_ERR);
                    }

                    Const.EN_RET_CODE retVal = Const.EN_RET_CODE.EN_RET_INIT;
                    //添加物理端口
                    retVal = PhyPortManager.GetInstance().AddPort(0, iSwitchPort, iHostPort);
                    if (retVal != Const.EN_RET_CODE.EN_RET_SUCC)
                    {
                        Util.Log(Util.EN_LOG_LEVEL.EN_LOG_FATAL, "添加物理端口失败");
                    }
                }
            }
            catch (XmlException)
            {
                Util.Log(Util.EN_LOG_LEVEL.EN_LOG_FATAL, "Xml格式错误");
                return(Const.EN_RET_CODE.EN_XML_FILE_FORMAT_ERR);
            }

            return(Const.EN_RET_CODE.EN_RET_SUCC);
        }