public SimPacket(SimPacketType type, IPEndPoint source, byte[] packet, DhtNetwork dest, TcpConnect tcp, ulong id) { Type = type; Source = source; Packet = packet; Dest = dest; Tcp = tcp; SenderID = id; }
public int SendPacket(SimPacketType type, DhtNetwork network, byte[] packet, System.Net.IPEndPoint target, TcpConnect tcp) { if (type == SimPacketType.Tcp) { if (!TcpEndPoints.ContainsKey(target)) { //this is what actually happens -> throw new Exception("Disconnected"); return(-1); } } if (!UdpEndPoints.ContainsKey(target) || !SimMap.ContainsKey(target.Address) || !SimMap.ContainsKey(network.Core.Sim.RealIP)) { return(0); } DhtNetwork targetNet = (type == SimPacketType.Tcp) ? TcpEndPoints[target] : UdpEndPoints[target]; if (network.IsLookup != targetNet.IsLookup) { Debug.Assert(false); } IPEndPoint source = new IPEndPoint(network.Core.Sim.RealIP, 0); source.Port = (type == SimPacketType.Udp) ? network.UdpControl.ListenPort : network.TcpControl.ListenPort; SimInstance sourceInstance = SimMap[source.Address]; SimInstance destInstance = SimMap[target.Address]; if (packet != null) { sourceInstance.BytesSent += (ulong)packet.Length; } // tcp connection must be present to send tcp if (type == SimPacketType.Tcp) { if (!TcpSourcetoDest.SafeContainsKey(tcp)) { //this is what actually happens -> throw new Exception("Disconnected"); return(-1); } } // add destination to nat table if (type == SimPacketType.Udp && sourceInstance.RealFirewall == FirewallType.NAT) { sourceInstance.NatTable[target] = true; } // if destination blocked drop udp / tcp connect requests if (destInstance.RealFirewall == FirewallType.Blocked) { if (type == SimPacketType.Udp || type == SimPacketType.TcpConnect) { return(0); } } // if destination natted drop udp (unless in nat table) / tcp connect requests else if (destInstance.RealFirewall == FirewallType.NAT) { if (type == SimPacketType.TcpConnect) { return(0); } if (type == SimPacketType.Udp && !destInstance.NatTable.ContainsKey(source)) { return(0); } } // randomly test tcp send buffer full if (TestTcpFullBuffer) { if (type == SimPacketType.Tcp && packet.Length > 4 && RndGen.Next(2) == 1) { int newlength = packet.Length / 2; byte[] newpacket = new byte[newlength]; Buffer.BlockCopy(packet, 0, newpacket, 0, newlength); packet = newpacket; } } if (packet != null && packet.Length == 0) { Debug.Assert(false, "Empty Packet"); } int index = destInstance.ThreadIndex; lock (OutPackets[index]) OutPackets[index].Add(new SimPacket(type, source, packet, targetNet, tcp, network.Local.UserID)); if (packet == null) { return(0); } return(packet.Length); }
public int SendPacket(SimPacketType type, DhtNetwork network, byte[] packet, System.Net.IPEndPoint target, TcpConnect tcp) { if (type == SimPacketType.Tcp) if (!TcpEndPoints.ContainsKey(target)) { //this is what actually happens -> throw new Exception("Disconnected"); return -1; } if( !UdpEndPoints.ContainsKey(target) || !SimMap.ContainsKey(target.Address) || !SimMap.ContainsKey(network.Core.Sim.RealIP)) return 0; DhtNetwork targetNet = (type == SimPacketType.Tcp) ? TcpEndPoints[target] : UdpEndPoints[target]; if (network.IsLookup != targetNet.IsLookup) Debug.Assert(false); IPEndPoint source = new IPEndPoint(network.Core.Sim.RealIP, 0); source.Port = (type == SimPacketType.Udp) ? network.UdpControl.ListenPort : network.TcpControl.ListenPort; SimInstance sourceInstance = SimMap[source.Address]; SimInstance destInstance = SimMap[target.Address]; if(packet != null) sourceInstance.BytesSent += (ulong)packet.Length; // tcp connection must be present to send tcp if (type == SimPacketType.Tcp) if( !TcpSourcetoDest.SafeContainsKey(tcp)) { //this is what actually happens -> throw new Exception("Disconnected"); return -1; } // add destination to nat table if (type == SimPacketType.Udp && sourceInstance.RealFirewall == FirewallType.NAT) sourceInstance.NatTable[target] = true; // if destination blocked drop udp / tcp connect requests if (destInstance.RealFirewall == FirewallType.Blocked) { if (type == SimPacketType.Udp || type == SimPacketType.TcpConnect) return 0; } // if destination natted drop udp (unless in nat table) / tcp connect requests else if (destInstance.RealFirewall == FirewallType.NAT) { if (type == SimPacketType.TcpConnect) return 0; if (type == SimPacketType.Udp && !destInstance.NatTable.ContainsKey(source) ) return 0; } // randomly test tcp send buffer full if(TestTcpFullBuffer) if (type == SimPacketType.Tcp && packet.Length > 4 && RndGen.Next(2) == 1) { int newlength = packet.Length / 2; byte[] newpacket = new byte[newlength]; Buffer.BlockCopy(packet, 0, newpacket, 0, newlength); packet = newpacket; } if (packet != null && packet.Length == 0) Debug.Assert(false, "Empty Packet"); int index = destInstance.ThreadIndex; lock (OutPackets[index]) OutPackets[index].Add(new SimPacket(type, source, packet, targetNet, tcp, network.Local.UserID)); if (packet == null) return 0; return packet.Length; }