private void SendThread() //发送消息的线程 { while (!IsClose) { //var tn = Thread.CurrentThread.ManagedThreadId; //Debug.LogError("ThreadN: "+tn); //var st = Util.GetTimeNow(); signal.WaitOne(); //阻塞等待,当调用signal.set()方法时被唤醒,当signal.set()被调用后需要调用signal.Reset()才能时WaitOne()重新阻塞 if (IsClose) { break; } MsgBuffer mb = null; lock (msgBuffer) { if (msgBuffer.Count > 0) { mb = msgBuffer.Dequeue(); } } if (mb != null) { try { client.GetStream().Write(mb.buffer, mb.position, mb.Size); //传输网络数据 /* * ml.queueInLoop(() => * { * Bundle.ReturnBundle(mb.bundle); * }); */ } catch (Exception exception) { Debug.LogError(exception.ToString()); Close(); } } lock (msgBuffer) { if (msgBuffer.Count <= 0) { signal.Reset(); } } //var et = Util.GetTimeNow(); //Debug.LogError("DiffTime: "+(et-st)); } }
public void Send(byte[] data, Bundle bundle) //Send命令主要把2进制数据流封装了一下,然后放入了msgBuffer的Queue中等待处理 { lock (msgBuffer) { var mb = new MsgBuffer() { position = 0, buffer = data, bundle = null }; Bundle.ReturnBundle(bundle); msgBuffer.Enqueue(mb); } signal.Set(); }
//网络数据保存到消息缓冲区 public void SaveMsgToBuffer(int pMsgLen) { ReceCount++; ReceSize += pMsgLen; lock (MsgBuffer) { MsgBuffer.WriteBytes(Buffer, pMsgLen); } if (ReceCount % 10 == 0) { MainForm.Ins.UpdateReceInfo(m_ListItem, ReceCount, ReceSize); } }
public void Message_g_ReadTest() { //Arrange byte[] bytearray = new byte[] { 0, 250, 2, 9, 0, 10 }; Message_g msg = new Message_g(250, 2, 9, 10); //act MsgBuffer msgBuffer = msg.ToAciMsgBuffer(); int i = 0; foreach (var bytes in msgBuffer.Buffer) { Assert.AreEqual(bytes, bytearray[i]); i++; } }
public void Message_g_WriteTest() { //Arrange byte[] bytearray = new byte[] { 0, 250, 2, 9, 0, 10, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8, 0, 9 }; List <int> parlist = new List <int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }); Message_g msg = new Message_g(250, 2, 9, 10, parlist); //act MsgBuffer msgBuffer = msg.ToAciMsgBuffer(); int i = 0; foreach (var bytes in msgBuffer.Buffer) { Assert.AreEqual(bytes, bytearray[i]); i++; } }
public void Message_qTest() { //Arrange byte[] bytearray = new byte[] { 250, 0x01, 0, 1, 0xF, 0xA1, 1, 0x4D, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8, 0, 9 }; List <int> parlist = new List <int>(new int[] { 1, 4001, 333, 4, 5, 6, 7, 8, 9 }); Message_q msg = new Message_q(250, 0x01, parlist); //act MsgBuffer msgBuffer = msg.ToAciMsgBuffer(); int i = 0; foreach (var bytes in msgBuffer.Buffer) { Assert.AreEqual(bytes, bytearray[i]); i++; } }
private void SendThread() { while (!IsClose) { signal.WaitOne(); if (IsClose) { break; } MsgBuffer mb = null; lock (msgBuffer) { if (msgBuffer.Count > 0) { mb = msgBuffer[0]; msgBuffer.RemoveAt(0); } } if (mb != null) { try { //client.BeginSend(mb.buffer, mb.buffer.Length, endPoint, OnSend, null); client.Send(mb.buffer, mb.buffer.Length, endPoint); } catch (Exception exp) { Debug.LogError(exp.ToString()); Close(); } } lock (msgBuffer) { if (msgBuffer.Count <= 0) { signal.Reset(); } } } }
public void Message_m_change_prioTest() { //Arrange byte[] bytearray = new byte[] { 00, 55, 4, 99 }; Message_m msg = new Message_m(55, 4, 99); //act MsgBuffer msgBuffer = msg.ToAciMsgBuffer(); int i = 0; foreach (var bytes in msgBuffer.Buffer) { Assert.AreEqual(bytes, bytearray[i]); i++; } ; }
public void Message_hpil_multiTest() { //Arrange byte[] bytearray = new byte[] { 00, 02, 0x01, 0x37, 5, 100, 0, 100, 20, 0 }; Message_hpil_multi msg = new Message_hpil_multi(2, 311, (byte)HPIL_CODE.HPIL_CMDRDMU, 100, 100, 20, null); //act MsgBuffer msgBuffer = msg.ToAciMsgBuffer(); int i = 0; foreach (var bytes in msgBuffer.Buffer) { Assert.AreEqual(bytes, bytearray[i]); i++; } ; }
public void Message_hpil_wordTest() { //Arrange byte[] bytearray = new byte[] { 00, 02, 0x01, 0x37, 2, 0, 0, 100, 0, 100, 2, 0, 0, 101, 0, 101 }; Message_hpil_word msg = new Message_hpil_word(2, 311, (byte)HPIL_CODE.HPIL_CMDWR, 100, 100, (byte)HPIL_CODE.HPIL_CMDWR, 101, 101); //act MsgBuffer msgBuffer = msg.ToAciMsgBuffer(); int i = 0; foreach (var bytes in msgBuffer.Buffer) { Assert.AreEqual(bytes, bytearray[i]); i++; } ; }
public void SendPacket(CGPlayerCmd.Builder cg) { Log.Net("UDP Send: " + cg.ToString()); Bundle bundle; var data = Bundle.GetPacketFull(cg, out bundle); Bundle.ReturnBundle(bundle); var mb = new MsgBuffer() { position = 0, buffer = data.data, bundle = null, }; lock (msgBuffer) { msgBuffer.Add(mb); } signal.Set(); }
public ClientBuffer() { m_rawBuffer = new MsgBuffer(); m_msgBuffer = new MsgBuffer(); //m_sendTmpBA = new ByteBuffer(); m_sendTmpBuffer = new MsgBuffer(); m_socketSendBA = new ByteBuffer(); //m_socketSendBA.m_id = 1000; //m_dynBuff = new DynamicBuffer<byte>(8096); m_unCompressHeaderBA = new ByteBuffer(); m_sendData = new ByteBuffer(); m_tmpData = new ByteBuffer(4); m_tmp1fData = new ByteBuffer(4); m_readMutex = new MMutex(false, "ReadMutex"); m_writeMutex = new MMutex(false, "WriteMutex"); #if MSG_ENCRIPT m_cryptContext = new CryptContext(); #endif }