Interface TunnelTransport
How packets reach the tunnel, which is where the two platforms differ.
This interface EXPOSES that difference rather than hiding it, because hiding it would mean pretending one of the platforms works like the other:
- On Android a
VpnServiceruns in the app's own process and hands over a file descriptor. Reading it blocks, and the read loop owns a thread. - A host that delivers packets through a completion handler instead --
an
NEPacketTunnelProvideris the example, though iOS does not run these tunnels -- has nothing to block on, and a loop that tried to would deadlock it. The simulation takes this shape so the case is exercised.
isBlocking() is the discriminator, and the host reads it rather than
guessing from the platform. A tunnel written against VpnTunnel never
sees any of this; it is here for the ports and for the simulation.
-
Method Summary
Modifier and TypeMethodDescriptionbuffers()The buffers this transport reads into, sized for the link.voidclose()Releases whatever the platform gave the tunnel.booleanWhetherread(PacketBuffer[])blocks until packets arrive.intread(PacketBuffer[] into) Takes the next packets, fillingintoand answering how many.voidwrite(PacketBuffer packet) Sends one packet back out.
-
Method Details
-
isBlocking
boolean isBlocking()Whether
read(PacketBuffer[])blocks until packets arrive.True on Android, where the host gives the loop a thread. False for a host that arms a callback and returns, which is the simulation.
-
read
Takes the next packets, filling
intoand answering how many.Answers
0when the tunnel is going down, which is how a blocking loop learns to stop. -
write
Sends one packet back out. -
buffers
PacketBuffer[] buffers()The buffers this transport reads into, sized for the link.
Owned by the transport and reused; see
PacketBuffer. -
close
void close()Releases whatever the platform gave the tunnel.
-