private static void DoSend() { while (true) { if (CSMsgQueue.Count <= 0) { Thread.Sleep(1000); continue; } Stream postStream = null; HttpWebResponse hwresponse = null; Stream responseStream = null; try { TBase csMsg; lock (CSMsgQueue.SyncRoot) { csMsg = CSMsgQueue.Dequeue() as TBase; } if (csMsg == null) { continue; } Logger.Log("Do send" + csMsg.GetType()); var hwrequest = (HttpWebRequest)WebRequest.Create(ServiceManager.ServerData.Url); hwrequest.Accept = "*/*"; hwrequest.AllowAutoRedirect = true; hwrequest.UserAgent = "http_requester/0.1"; hwrequest.Timeout = 60000; hwrequest.Method = Method; hwrequest.Headers.Add("ISESSION", sessionId); hwrequest.ContentType = "multipart/form-data"; var msg = new ThriftCSMessage(csMsg); var postByteArray = msg.Encode(); if (postByteArray == null || postByteArray.Length <= 0) { // Logger.LogError("send data is null or length is 0, msg type = " + csMsg.GetType().ToString()); continue; } hwrequest.ContentLength = postByteArray.Length; postStream = hwrequest.GetRequestStream(); postStream.Write(postByteArray, 0, postByteArray.Length); postStream.Close(); postStream = null; // deal with receive sc msg hwresponse = (HttpWebResponse)hwrequest.GetResponse(); Logger.Log("response status:" + hwresponse.StatusCode); //Throw the message receive event. if (OnMessageReceived != null) { OnMessageReceived(); } if (hwresponse.StatusCode == HttpStatusCode.OK) { var respLen = (int)hwresponse.ContentLength; Logger.Log("respLen:" + respLen); if (respLen <= 0) { hwresponse.Close(); hwresponse = null; continue; } responseStream = hwresponse.GetResponseStream(); ByteBuffer recBuffer = new ByteBuffer(respLen); var recDataPerTime = new byte[PkgReadLenPerTime]; long totalBytesRead = 0; int bytesRead; while ((bytesRead = responseStream.Read(recDataPerTime, 0, recDataPerTime.Length)) > 0) { totalBytesRead += bytesRead; recBuffer.Put(recDataPerTime, bytesRead); } if (totalBytesRead < respLen) { Logger.Log("An error occurred: msg read len is not:" + respLen); continue; } recBuffer.Flip(); var recDatas = new byte[respLen]; recBuffer.Get(recDatas); responseStream.Close(); responseStream = null; SocketClient.DecodeMsg(recDatas, respLen, SCMsgQueue); sessionId = hwresponse.GetResponseHeader("ISESSION"); } hwresponse.Close(); hwresponse = null; } catch (Exception e) { if (OnMessageThrowException != null) { OnMessageThrowException(); } Logger.Log("An error occurred: " + e.Message); MtaManager.ReportError("An error occurred: " + e.Message); // ThriftSCMessage globalmessage = new ClientSCMessage((short)MessageType.SC_SYSTEM_INFO_MSG, e.Message); // lock (SCMsgQueue.SyncRoot) // { // SCMsgQueue.Enqueue(globalmessage); // } } finally { if (postStream != null) { try { postStream.Close(); } catch (Exception e) { Logger.Log("An error occurred close postStream: " + e.Message); MtaManager.ReportError("An error occurred close postStream: " + e.Message); } } if (responseStream != null) { try { responseStream.Close(); } catch (Exception e) { Logger.Log("An error occurred close responseStream: " + e.Message); MtaManager.ReportError("An error occurred close postStream: " + e.Message); } } if (hwresponse != null) { try { hwresponse.Close(); } catch (Exception e) { Logger.Log("An error occurred close hwresponse: " + e.Message); MtaManager.ReportError("An error occurred close postStream: " + e.Message); } } } } }
/// <summary> /// send msg to server /// </summary> /// <param name="msgContent"></param> public void SendMessage(TBase msgContent) { var msg = new ThriftCSMessage(msgContent); var bytes = msg.Encode(); if (bytes == null || bytes.Length <= 0) { Logger.LogError("send data is null or length is 0, msg type = " + msgContent.GetType().ToString()); return; } // TODO Encrypt Message Data StartSendMsg(bytes); }