예제 #1
0
        /// <summary>
        /// 连接到指定的服务器终结点。
        /// </summary>
        /// <param name="hostname">服务地址</param>
        /// <param name="port">服务端口</param>
        /// <returns>是否连接成功</returns>
        public bool Connect(string hostname, int port)
        {
            BlazeLog.InfoFormat("[TcpClient]Connecting to {0}:{1}", hostname, port);
            if (mIsCleanedUp)
            {
                throw new ObjectDisposedException(GetType().FullName);
            }
            if (hostname == null)
            {
                throw new ArgumentNullException("hostname");
            }
            if (!validateTcpPort(port))
            {
                throw new ArgumentOutOfRangeException("port");
            }

            try
            {
                if (mClientSocket == null)
                {
                    mClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                }
                mClientSocket.Connect(hostname, port);
                mNetworkStream = new NetworkStream(mClientSocket);
            }
            catch (Exception e)
            {
                BlazeLog.ErrorFormat("[TcpClient]Connection failed -> " + e);
                Debug.LogError("[TcpClient]Connection failed -> " + e);
                return(false);
            }
            return(true);
        }
예제 #2
0
        /// <summary>
        /// 获取指定路径下子物体上的组件。
        /// </summary>
        /// <typeparam name="T">组件类型</typeparam>
        /// <param name="path">路径</param>
        protected T GetChild <T>(string path) where T : Component
        {
            var child = Transform.Find(path);

            if (child == null)
            {
                BlazeLog.ErrorFormat("Can't find child '{0}' in {1}", path, GameObject.name);
                return(null);
            }
            var component = child.GetComponent <T>();

            if (component == null)
            {
                BlazeLog.ErrorFormat("Can't find component '{0}' in {1}", typeof(T).Name, GameObject.name);
                return(null);
            }
            return(component);
        }