/// <summary>The thread acting as the ISource for Ethernet, this is where /// reading of the TAP is performed. Use Subscribe to receive the packets /// coming from here.</summary> /// <remarks>The same max MTU byte array is always read into. This is then /// copied to a minimum sized MemBlock and send to the subscriber. </remarks> protected void ReadLoop() { byte[] read_buffer = new byte[MTU]; BufferAllocator ba = new BufferAllocator(MTU, 1.1); while(_running) { int length = -1; try { length = _tap.Read(read_buffer); } catch(ThreadInterruptedException x) { if(_running && ProtocolLog.Exceptions.Enabled) { ProtocolLog.Write(ProtocolLog.Exceptions, x.ToString()); } } catch(Exception e) { ProtocolLog.WriteIf(ProtocolLog.Exceptions, e.ToString()); } if (length == 0 || length == -1) { ProtocolLog.WriteIf(IpopLog.TapLog, "Couldn't read TAP"); continue; } Array.Copy(read_buffer, 0, ba.Buffer, ba.Offset, length); MemBlock packet = MemBlock.Reference(ba.Buffer, ba.Offset, length); ba.AdvanceBuffer(length); Subscriber s = _sub; if(s != null) { try { s.Handle(packet, this); } catch(Exception e) { ProtocolLog.WriteIf(ProtocolLog.Exceptions, e.ToString()); } } } }
/** * This is a System.Threading.ThreadStart delegate * We loop waiting for edges that need to send, * or data on the socket. * * This is the only thread that can touch the socket, * therefore, we do not need to lock the socket. */ protected void ListenThread() { Thread.CurrentThread.Name = "udp_listen_thread"; BufferAllocator ba = new BufferAllocator(8 + Int16.MaxValue); EndPoint end = new IPEndPoint(IPAddress.Any, 0); DateTime last_debug = DateTime.UtcNow; int debug_period = 5000; bool logging = ProtocolLog.Monitor.Enabled; while(1 == _running) { if(logging) { DateTime now = DateTime.UtcNow; if(last_debug.AddMilliseconds(debug_period) < now) { last_debug = now; ProtocolLog.Write(ProtocolLog.Monitor, String.Format("I am alive: {0}", now)); } } try { int max = ba.Capacity; int rec_bytes = _s.ReceiveFrom(ba.Buffer, ba.Offset, max, SocketFlags.None, ref end); //Get the id of this edge: if( rec_bytes >= 8 ) { int remoteid = NumberSerializer.ReadInt(ba.Buffer, ba.Offset); int localid = NumberSerializer.ReadInt(ba.Buffer, ba.Offset + 4); MemBlock packet_buffer = MemBlock.Reference(ba.Buffer, ba.Offset + 8, rec_bytes - 8); ba.AdvanceBuffer(rec_bytes); if( localid < 0 ) { /* * We never give out negative id's, so if we got one * back the other node must be sending us a control * message. */ HandleControlPacket(remoteid, localid, packet_buffer, null); } else { HandleDataPacket(remoteid, localid, packet_buffer, end, null); } } } catch(SocketException x) { if((1 == _running) && ProtocolLog.Exceptions.Enabled) { ProtocolLog.Write(ProtocolLog.Exceptions, x.ToString()); } } } //Let everyone know we are out of the loop _listen_finished_event.Set(); _s.Close(); //Allow garbage collection _s = null; }
public SocketState(TcpEdgeListener tel) { _sock_to_rs = new Hashtable(); _sock_to_constate = new ListDictionary(); _con_socks = new ArrayList(); ListenSock = tel._listen_sock; _socks_to_send = new ArrayList(); AllSockets = new ArrayList(); ReadSocks = new ArrayList(); ErrorSocks = new ArrayList(); WriteSocks = new ArrayList(); Run = true; /* Use a shared BufferAllocator for all Edges */ BA = new BufferAllocator(2 + Packet.MaxLength); ListenSock.Listen(10); TEL = tel; }
public ReceiveState(TcpEdge e, BufferAllocator ba) { Edge = e; _s = e.Socket; _ba = ba; }
public SimulationEdgeListener(int id, double loss_prob, TAAuthorizer ta_auth, bool use_delay) { _edges = new Hashtable(); _use_delay = use_delay; _sync = new object(); _ba = new BufferAllocator(Int16.MaxValue); _listener_id = id; _ploss_prob = loss_prob; if (ta_auth == null) { _ta_auth = new ConstantAuthorizer(TAAuthorizer.Decision.Allow); } else { _ta_auth = ta_auth; } _tas = new ArrayList(); _tas.Add(TransportAddressFactory.CreateInstance("b.s://" + _listener_id)); _rand = new Random(); }
static AHSender() { SenderFactory.Register("ah", CreateInstance); _buf_alloc = new BufferAllocator(System.UInt16.MaxValue); }