void DoReceive(int count) { //other thread _pr.process(_recvBuf, 0, count, _packets); foreach (var p in _packets) { Process(p); } _packets.Clear(); }
void OnReadCB(Channel c, byte[] buff, int nread) { if (nread < 0) { //remove //RemoveChannel(c); //Console.WriteLine($"remove channel:{c.Id}"); _connected = false; } else { List <Packet> packets = new List <Packet>(); _packetRead.process(buff, 0, nread, packets); Console.WriteLine($"Recv:{nread} at channel:{c.Id}"); _dispatcher.Process(_loop, c, packets); } }
void RecvCB(IAsyncResult r) { try { RobotAgent agent = (RobotAgent)r.AsyncState; int length = agent._s.EndReceive(r); List <Packet> plist = new List <Packet>(); pr.process(_recvBuf, 0, length, plist); foreach (var p in plist) { //string s = pr.ReadString(p); //Trace.WriteLine($"Receive:{length},con:{s}"); } _s.BeginReceive(_recvBuf, 0, _recvBuf.Length, SocketFlags.None, RecvCB, this); } catch (Exception e) { Trace.WriteLine($"Exception:{e}"); } }
public void Test_packet(params object[] values) { int id = 3; ostream.WritePacketHead((byte)PacketType.HEART_BEAT, 0); int size = 0; foreach (var v in values) { size += GetSize(v); } int psize = OutputStream.GetSize(id); //ostream.WriteInt32(psize + size); ostream.WritePacketHead((byte)PacketType.REQ, (uint)(psize + size)); ostream.WriteInt32(id); foreach (var v in values) { WriteToStream(ostream, v); } Console.WriteLine($"PacketLen:{ostream.Length}"); PacketRead pr = new PacketRead(); List <Packet> packets = new List <Packet>(); int smallBuffLen = 3; byte[] smallBuff = new byte[smallBuffLen]; for (int i = 0; i < ostream.Length;) { int count = smallBuffLen; int left = ostream.Length - i; if (left < count) { count = left; } Buffer.BlockCopy(bytes, i, smallBuff, 0, count); pr.process(smallBuff, 0, count, packets); i += smallBuffLen; } Assert.IsTrue(packets.Count == 2); Packet hearbeat = packets[0]; Assert.IsTrue(hearbeat.packetType == (byte)PacketType.HEART_BEAT); Packet p = packets[1]; Assert.IsTrue(p.packetType == (byte)PacketType.REQ); InputStream stream = new InputStream(p.body); int id2 = stream.ReadInt32(); Assert.IsTrue(id == id2); foreach (var obj in values) { //WriteToStream(ostream, v); Type t = obj.GetType(); if (t == typeof(int)) { Assert.IsTrue((int)obj == stream.ReadInt32()); } else if (t == typeof(long)) { Assert.IsTrue((long)obj == stream.ReadInt64()); } else if (t == typeof(string)) { string s = stream.ReadString(); Assert.IsTrue((string)obj == s, $"{obj},{s}"); //Assert.AreEqual((string)obj, s); } } }