Beispiel #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);
        }
Beispiel #2
0
        private IEnumerator perform(HttpRequest request)
        {
            WWW www;

            if (request.Bytes.Length == 0)
            {
                //GET
                var form = new WWWForm();
                foreach (var header in request.Headers)
                {
                    form.headers.Add(header.Key, header.Value);
                }
                if (mCookies != null)
                {
                    form.headers.Add("Cookie", mCookies);
                }
                foreach (var key in request.QueryString.Keys)
                {
                    form.AddField(key, request.QueryString[key]);
                }
                if (form.data.Length == 0)
                {
                    www = new WWW(request.Uri.ToString());
                }
                else
                {
                    www = new WWW(request.Uri.ToString(), form);
                }
            }
            else
            {
                //POST
                var headers = new Dictionary <string, string>(request.Headers);
                if (mCookies != null)
                {
                    headers.Add("Cookie", mCookies);
                }
                www = new WWW(request.Uri.ToString(), request.Bytes, headers);
            }
            using (www)
            {
                var beginTime = Time.time;
                while (!www.isDone)
                {
                    yield return(null);

                    if (request.Timeout > 0 && Time.time - beginTime >= request.Timeout)
                    {
                        www.Dispose();
                        request.Fail("timeout");
                        yield break;
                    }
                }

                if (www.error == null)
                {
                    BlazeLog.InfoFormat("[HttpClient]text={0}", www.text);
                    request.Finish(www.bytes);
                    performCookies(www);
                }
                else
                {
                    BlazeLog.WarningFormat("[HttpClient]error={0}", www.error);
                    request.Fail(www.error);
                }
            }
        }