// 发送消息 public void SendMsg(Engine.PackageOut pkg) { //Utility.Log.Error("MsgCode:{0}", pkg.getCode()); if (this.Connected && socketstate == SCOKET_CONNECT_STATE.CONNECTED) { if (m_sender != null) { m_sender.Send(pkg); if (m_NetLinkMonitor != null) { m_NetLinkMonitor.OnSend(pkg); } } } else { Utility.Log.Trace("已经断开连接,发包错误,包进入缓存"); if (m_sender != null) { m_sender.enqueErrorPack(pkg); } //if( m_NetLinkSink != null ) //{ // m_NetLinkSink.OnConnectError(NetWorkError.NetWorkError_ConnectError); //} //NetManager.Instance().PushConnectError(NetWorkError.NetWorkError_ConnectError); } }
public void PopPackage(out List <Engine.PackageOut> lstPackage) { lstPackage = new List <Engine.PackageOut>(); Utility.Log.Trace("PopPackage start->lstPackage count=" + lstPackage.Count); if (packoutCmd != null) { lstPackage.Add(packoutCmd); } if (packout != null) { while (packout.Count > 0) { Engine.PackageOut pkg = packout.Dequeue(); if (pkg != packoutCmd) { lstPackage.Add(pkg); } } } //for (int i = 0; i < m_packetList.m_InList.Count; ++i ) //{ // //Engine.PackageOut pkg = m_packetQueue.Dequeue(); // if (m_packetList.m_InList[i] != packoutCmd) // { // lstPackage.Add(m_packetList.m_InList[i]); // } //} //Utility.Log.Trace("PopPackage end ->lstPackage count=" + lstPackage.Count); }
protected override void Main() { while (!IsTerminateFlagSet() && m_SendEvent != null) { m_SendEvent.WaitOne(); m_packetList.Switch(); while (m_packetList.m_OutList.Count > 0) { Engine.PackageOut pkg = m_packetList.m_OutList[0]; //pkg.pack(); packoutCmd = pkg; m_packetList.m_OutList.RemoveAt(0); if (m_tcpClient != null && m_tcpClient.Connected) { try { // encode byte[] buff = pkg.GetBuffer(); // if (NetWork.DEENCODE) // { // // 加密编码 // //Engine.Encrypt.Encode(ref buff); // } NetStream.Write(buff, 0, (int)pkg.Length); NetStream.Flush(); if (m_tcpClient == null || !m_tcpClient.Connected) { enqueErrorPack(pkg); } } catch (IOException e) { Utility.Log.Error("发包{0}异常: {1}", pkg.getCode(), e.ToString()); enqueErrorPack(pkg); } } else { // Utility.Log.Trace("断线发包: code=" + pkg.getCode().ToString()); enqueErrorPack(pkg); break; } } // end while(m_packetList.m_OutList.Count > 0) } //end while }
public bool Write(Pmd.ForwardNullUserPmd_CS package, Engine.INetLink netLink, bool isFirst) { if (netLink == null) { Engine.Utility.Log.Error("严重错误:NetLink为空。"); return(false); } count++; if (isFirst) { Engine.Utility.Log.Error("isfirst bitmask Bitmask_Reconnect。"); package.bitmask |= (int)Pmd.FrameHeader.Bitmask_Reconnect; } // 消息压缩 if (package.data.Length > 32 && count != 1) { //var zip = Common.ZlibCodec.Encode(package.data); //if (zip.Length < package.data.Length) //{ // package.bitmask |= (int)Pmd.FrameHeader.Bitmask_Compress; // package.data = zip; //} } using (var mem = new MemoryStream()) { package.SerializeLengthDelimited(mem); //Console.WriteLine("SEND: {0}\t{1}", mem.Length, BitConverter.ToString(mem.ToArray())); // Engine.Utility.Log.Error("SEND: {0}\t{1}", mem.Length, BitConverter.ToString(mem.ToArray())); Engine.PackageOut pack = new Engine.PackageOut(mem, (int)package.byCmd); netLink.SendMsg(pack); } return(true); }
public void enqueErrorPack(Engine.PackageOut pkg) { packout.Enqueue(pkg); }
public void Send(Engine.PackageOut p) { m_packetList.Push(p); m_SendEvent.Set(); }