internal void Write(IoBuffer ioBuffer, ExceptionCaught exceptionCaught) { if (!IsConnected()) { throw new Exception("can't send data,socket connect is closed."); } if (ioBuffer.Length == 0) { logReport.OnDebugReport("skip write to sockt,iobuffer is empty"); return; } if (writing) { logReport.OnDebugReport("skip write to sockt in this loop"); return; } lock (wrlock) { if (writing) { logReport.OnDebugReport("skip write to sockt in this loop"); return; } writing = true; } Socket socket = mTcp.Client; byte[] data = new byte[ioBuffer.Length]; ioBuffer.Read(data, 0, data.Length); try { Object[] args = new Object[2]; args[0] = socket; args[1] = exceptionCaught; socket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(SendCallback), args); } finally { writing = false; } }
public void HttpAsyncEvent(Msg msg) { if (msg == null) { return; } byte[] data = encoder.encode(msg); HttpWebRequest myRequest = null; Stream stream = null; try { // 发送请求 myRequest = (HttpWebRequest)WebRequest.Create(url); myRequest.Timeout = 30000; myRequest.KeepAlive = false; myRequest.CookieContainer = myCookie; myRequest.Method = "POST"; myRequest.ContentLength = data.Length; stream = myRequest.GetRequestStream(); stream.Write(data, 0, data.Length); stream.Flush(); // 获得回复 HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); Stream responseStream = myResponse.GetResponseStream(); HttpStatusCode code = myResponse.StatusCode; if (code == HttpStatusCode.OK) { byte[] temp = new byte[128]; int i = 0; IoBuffer buffer = new IoBuffer(); do { i = responseStream.Read(temp, 0, 128); if (i > 0) { buffer.Write(temp, 0, i); } } while (i > 0); responseStream.Close(); int len = buffer.Length; byte[] datas = new byte[len]; buffer.Read(datas, 0, len); //单包模型 Msg revMsg = decoder.DeCode(datas); if (logReport != null) { logReport.OnLogReport("rev http rsp,cmd:" + revMsg.Cmd + " rsCode:" + revMsg.GetParam(BaseCodeMap.BaseParam.RS_CODE)); } mDataPackets.Put(new RevEvent(this, revMsg)); } else { if (logReport != null) { logReport.OnWarningReport("http cmd:" + msg.Cmd + " fail,code:" + code); } msg.AddParam(BaseCodeMap.BaseParam.RS_CODE, BaseCodeMap.BaseRsCode.TIME_OUT); mDataPackets.Put(new TimeOutEvent(this, msg)); } return; } catch (Exception ex) { if (logReport != null) { logReport.OnWarningReport("http cmd:" + msg.Cmd + " ex:" + ex.ToString()); } mDataPackets.Put(new TimeOutEvent(this, msg)); } finally { if (stream != null) { stream.Close(); } } }