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 void endReceive(IAsyncResult ar) { IPEndPoint pp = null; byte[] data = client.EndReceive(ar, ref pp); Packet p = new Packet(data, pp); Thread t = new Thread(handle_packet); t.IsBackground = true; t.Start(p); client.BeginReceive(endReceive, null); }
public SendOperation(IPEndPoint dest, Packet[] data, int opId) { destination = dest; table = new Dictionary<short, byte[]>(); foreach (Packet p in data) table.Add(p.HandshakeID, p._data); operationID = opId; client = new UdpClient(AddressFamily.InterNetworkV6); client.Client.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false); client.AllowNatTraversal(true); client.Connect(dest); }
public static void Send(Packet[] pcks, int operationID, IPEndPoint ip) { MemoryStream memsr = new MemoryStream(); BinaryWriter writer = new BinaryWriter(memsr); writer.Write(new byte[] { 0xCF, 0xCF, 0xCF, 0xCF }); writer.Write((short)(new Random().Next())); writer.Write(operationID); writer.Write(pcks.Length); for(int i = 0; i < pcks.Length; i++) { writer.Write(pcks[i].HandshakeID); } writer.Flush(); byte[] data = memsr.ToArray(); client.Send(data, data.Length, ip); Operations.Add(operationID, pcks); }
static void onReceived(Packet p) { Log.Info(string.Format("Received {0} bytes.", p.GetCleanData().Length)); done = true; }
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); }
private Packet GetPacket() { List<byte> final_array = new List<byte>(); lock(recv) // Useless but just to be sure. { for (int i = 0; i < totalCount; i++) { var retval = recv[order[i]]; for (int k = 0; k < retval.Length; k++) final_array.Add(retval[k]); } } byte[] final = final_array.ToArray(); Packet p = new Packet(final, from); return p; }