private void btnConnect_Click(object sender, EventArgs e) { try { client = new TcpClient("127.0.0.1", 2000); ns = client.GetStream(); sr = new StreamReader(ns); sw = new StreamWriter(ns); String response = sr.ReadLine(); DelegateResponse dr = new DelegateResponse(); string rawData = "Hola mundo"; String data = stringToHexa(rawData); Console.WriteLine(data); sw.WriteLine(data); sw.Flush(); } catch (Exception err) { Console.WriteLine(err.ToString()); throw; } }
//异步的http 请求 public void UF_HttpGetRequest(string url, string headinfo, int timeOut, DelegateResponse callback) { if (m_WebRequest != null) { m_WebRequest.UF_AsynGetRequest(url, headinfo, timeOut, callback); } }
static int UF_httpGet(IntPtr L) { #if UNITY_EDITOR if (LuaDLL.lua_isstring(L, 1) == 0) { LuaDLL.luaL_typerror(L, 1, "string"); } if (LuaDLL.lua_isstring(L, 2) == 0) { LuaDLL.luaL_typerror(L, 2, "string"); } if (LuaDLL.lua_isnumber(L, 3) == 0) { LuaDLL.luaL_typerror(L, 3, "number"); } #endif string url = LuaDLL.lua_tostring(L, 1); string headinfo = LuaDLL.lua_tostring(L, 2); int timeout = (int)LuaDLL.lua_tonumber(L, 3); DelegateResponse callback = null; if (LuaDLL.lua_isfunction(L, 4)) { LuaFunction luafunction = ToLua.ToLuaFunction(L, 4); callback = delegate(int retcode, object param) { UF_OnHttpResponse(luafunction, retcode, param); }; } NetworkSystem.UF_GetInstance().UF_HttpGetRequest(url, headinfo, timeout, callback); return(0); }
/// <summary> /// 异步请求 /// 注意回调函数可能在另外一个线程中执行,要避免线程引发的异常 /// <summary> public bool UF_AsynGetRequest(string url, string headinfo, int timeOut, DelegateResponse callback) { lock (m_ListAsyncRequestOpera) { m_ListAsyncRequestOpera.Add(new AsyncRequestOpera(TYPE_GET, url, "", headinfo, timeOut, callback)); } return(true); // return AsynRequest(TYPE_GET,url,"",headinfo,timeOut,callback); }
private void UF_CallbackAsynRequest(IAsyncResult result) { AsyncRequestStruct ars = ((AsyncRequestStruct)result.AsyncState); DelegateResponse call = ars.callback; HttpWebRequest _request = ars.request; int retcode = WebRequestRetcode.SUCCESS; byte[] bdata = null; HttpWebResponse response = null; //handle 列表中优先移除 UF_RemoveAsyncHandle(ars.id); try{ response = (HttpWebResponse)_request.EndGetResponse(result); Stream st = response.GetResponseStream(); List <byte> tmpDatas = new List <byte>(); int value = -1; while ((value = st.ReadByte()) != -1) { tmpDatas.Add((byte)value); } bdata = tmpDatas.ToArray(); st.Close(); // response.Close(); //300以上为异常请求 if (retcode < (int)response.StatusCode) { retcode = WebRequestRetcode.SUCCESS; } else { Debugger.UF_Error("Http Response Abnormal: " + (int)response.StatusCode); retcode = WebRequestRetcode.ABNORMAL; } } catch (Exception e) { Debugger.UF_Error("Http error<CallbackAsynRequest>:" + e.Message); retcode = WebRequestRetcode.EXCEPTION_ON_RESPONSE; } if (response != null) { response.Close(); } if (_request != null) { _request.Abort(); } //回归主线程调用 UF_InvokCallback(call, retcode, bdata); }
private void UF_InvokCallback(DelegateResponse callback, int retcode, object param) { if (callback != null) { FrameHandle.UF_CallMethod(() => { if (callback != null) { callback(retcode, param); } }); } }
static int UF_httpDownload(IntPtr L) { #if UNITY_EDITOR if (LuaDLL.lua_isstring(L, 1) == 0) { LuaDLL.luaL_typerror(L, 1, "string"); } if (LuaDLL.lua_isstring(L, 2) == 0) { LuaDLL.luaL_typerror(L, 2, "string"); } if (LuaDLL.lua_isnumber(L, 3) == 0) { LuaDLL.luaL_typerror(L, 3, "number"); } #endif string url = LuaDLL.lua_tostring(L, 1); string headinfo = LuaDLL.lua_tostring(L, 2); int timeout = (int)LuaDLL.lua_tonumber(L, 3); DelegateResponse callback = null; if (LuaDLL.lua_isfunction(L, 4)) { LuaFunction luafunction = ToLua.ToLuaFunction(L, 4); callback = delegate(int retcode, object param) { // string msg = string.Empty; byte[] data = param as byte[]; LuaFunction func = luafunction; func.BeginPCall(); func.Push(retcode); if (data != null) { uint uniCode = m_TableBytes.UF_Add(data); func.Push(uniCode); } func.PCall(); func.EndPCall(); }; } NetworkSystem.UF_GetInstance().UF_HttpGetRequest(url, headinfo, timeout, callback); return(0); }
public AsyncRequestOpera(int _stype, string _url, string _param, string _headinfo, int _timeOut, DelegateResponse _callback) { stype = _stype; url = _url; param = _param; headinfo = _headinfo; timeOut = _timeOut; callback = _callback; }
/// <summary> /// 异步上传文件 /// </summary> public bool UF_UploadFile(string url, string filePath, string headinfo, int timeOut, DelegateResponse callback) { if (!File.Exists(filePath)) { //文件不存在 Debugger.UF_Error("UploadFile Not Exist: " + filePath); if (callback != null) { callback(WebRequestRetcode.FILE_NOT_EXIST, null); } return(false); } AsyncRequestStruct ars = new AsyncRequestStruct(); int uid = GID; HttpWebRequest request = null; try{ ars.id = uid; ars.stamp = System.Environment.TickCount; ars.callback = callback; lock (m_ListAsyncHandle){ m_ListAsyncHandle.Add(ars); } FileStream fs = File.OpenRead(filePath); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, (int)fs.Length); fs.Close(); //request = (HttpWebRequest)HttpWebRequest.Create(url); request = System.Net.WebRequest.CreateHttp(url); if (!string.IsNullOrEmpty(headinfo)) { request.Headers.Add(HttpRequestHeader.Authorization, headinfo); } request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = buffer.Length; Stream postStream = request.GetRequestStream(); postStream.Write(buffer, 0, buffer.Length); postStream.Close(); ars.request = request; ars.request.Timeout = timeOut; ars.request.BeginGetResponse(new AsyncCallback(UF_CallbackAsynRequest), ars); } catch (Exception e) { UF_RemoveAsyncHandle(uid); if (request != null) { request.Abort(); } Debugger.UF_Error("Http error<UploadFile>:" + e.Message); if (callback != null) { callback(WebRequestRetcode.EXCEPTION_ON_REQUEST, null); } return(false); } return(true); }
/// <summary> /// 异步请求 /// 注意回调函数可能在另外一个线程中执行,要避免线程引发的异常 /// 如果已经在执行AsynRequest 已经执行 或 BegineAsynRequest正在执行 ,该接口调用无效,将返回false /// </summary> public bool UF_AsynRequest(int stype, string url, string param, string headinfo, int timeOut, DelegateResponse callback) { HttpWebRequest request = null; AsyncRequestStruct ars = new AsyncRequestStruct(); int uid = GID; try{ ars.id = uid; ars.stamp = System.Environment.TickCount; ars.callback = callback; lock (m_ListAsyncHandle){ m_ListAsyncHandle.Add(ars); } request = UF_CreateRequest(url, stype, param, headinfo, timeOut); ars.request = request; ars.request.Timeout = timeOut; ars.request.BeginGetResponse(new AsyncCallback(UF_CallbackAsynRequest), ars); return(true); } catch (Exception e) { UF_RemoveAsyncHandle(uid); if (request != null) { request.Abort(); } Debugger.UF_Error("Http error<AsynRequest>:" + e.Message); UF_InvokCallback(callback, WebRequestRetcode.EXCEPTION_ON_REQUEST, null); } return(false); }
public void UF_HttpUploadFile(string url, string filePath, string headinfo, int timeOut, DelegateResponse callback) { if (m_WebRequest != null) { m_WebRequest.UF_UploadFile(url, filePath, headinfo, timeOut, callback); } }
static int UF_httpDownloadTo(IntPtr L) { #if UNITY_EDITOR if (LuaDLL.lua_isstring(L, 1) == 0) { LuaDLL.luaL_typerror(L, 1, "string"); } if (LuaDLL.lua_isstring(L, 2) == 0) { LuaDLL.luaL_typerror(L, 2, "string"); } if (LuaDLL.lua_isnumber(L, 3) == 0) { LuaDLL.luaL_typerror(L, 3, "number"); } if (LuaDLL.lua_isstring(L, 4) == 0) { LuaDLL.luaL_typerror(L, 4, "string"); } #endif string url = LuaDLL.lua_tostring(L, 1); string headinfo = LuaDLL.lua_tostring(L, 2); int timeout = (int)LuaDLL.lua_tonumber(L, 3); string localPath = LuaDLL.lua_tostring(L, 4); if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(localPath))) { System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(localPath)); } DelegateResponse callback = null; if (LuaDLL.lua_isfunction(L, 5)) { LuaFunction luafunction = ToLua.ToLuaFunction(L, 5); callback = delegate(int retcode, object param) { // string msg = string.Empty; byte[] data = param as byte[]; //save to local try{ if (System.IO.File.Exists(localPath)) { System.IO.File.Delete(localPath); } System.IO.FileInfo fi = new System.IO.FileInfo(localPath); System.IO.FileStream fs = fi.Open(System.IO.FileMode.CreateNew); fs.Write(data, 0, data.Length); fs.Close(); }catch (System.Exception e) { Debugger.UF_Exception(e); } LuaFunction func = luafunction; func.BeginPCall(); func.Push(retcode); func.PCall(); func.EndPCall(); }; } NetworkSystem.UF_GetInstance().UF_HttpGetRequest(url, headinfo, timeout, callback); return(0); }