private void endReceive(IAsyncResult ar) { IPEndPoint ip = new IPEndPoint(IPAddress.IPv6Any, 00000); byte[] data = client.EndReceive(ar, ref ip); Packet p = new Packet(data, ip); from = ip; Log.Debug("AYY"); if (p.OperationID == operationID) { recv.Add(p.HandshakeID, p.GetCleanData()); Packet reply = p.MakeReply(); client.Send(reply._data, reply._data.Length, ip); Log.Info(string.Format("Received {0} bytes from {1}.", new object[] { data.Length, ip.ToString() })); currentCount++; } else { Log.Error(string.Format("Invalid packet Operation ID {0} expected {1}.", new object[] { p.OperationID.ToString("X"), operationID.ToString("X") })); } if(currentCount != totalCount) { client.BeginReceive(endReceive, null); } else { Log.Info(string.Format("R operation {0} has been completed successfully.", operationID.ToString("X"))); Packet packet = GetPacket(); client.Close(); UdpPacketRouter.QuitReceiveOp(operationID); callBack(packet); } }
private static void endReceive(IAsyncResult ar) { IPEndPoint ip = new IPEndPoint(IPAddress.IPv6Any, 000000); byte[] data = client.EndReceive(ar, ref ip); Packet p = new Packet(data, ip); if (p.IsAWAIT) // We are going to get some data ! { int count = p.Reader.ReadInt32(); List<short> order = new List<short>(); for(int i = 0; i < count; i++) { order.Add(p.Reader.ReadInt16()); } p.DestinationPort = (ushort)(new Random().Next(2000, IPEndPoint.MaxPort)); Packet reply = p.MakeReply(); client.BeginSend(reply._data, reply._data.Length, ip, endSend, new object[] { p.DestinationPort, p.OperationID, order }); } if(p.IsGOTCHA) // Let's send some data ! { if(Operations.ContainsKey(p.OperationID)) { Log.Info(string.Format("Preparing S operation for {0}.", p.OperationID.ToString("X"))); SendOperation so = new SendOperation(ip, Operations[p.OperationID], p.OperationID); lock(Pending) { Pending.Remove(p.OperationID); } SOp.Add(p.OperationID, so); so.BeginOperation(Done); } else { Log.Error("Invalid operation ID."); } } client.BeginReceive(endReceive, null); }