32 lines
941 B
Go
32 lines
941 B
Go
type SharedStream struct {
|
|
Stream *network.Stream
|
|
Lock *sync.Mutex
|
|
}
|
|
...
|
|
// Inside the TUN-read loop:
|
|
if found {
|
|
dst = route.Target.ID
|
|
go node.sendPacket(dst, packet, plen)
|
|
}
|
|
...
|
|
func (node *Node) sendPacket(dst peer.ID, packet []byte, plen int) {
|
|
// Check if we already have an open connection to the destination peer.
|
|
ms, ok := node.activeStreams[dst]
|
|
if ok {
|
|
if func() bool {
|
|
ms.Lock.Lock()
|
|
defer ms.Lock.Unlock()
|
|
// Write out the packet's length to the libp2p stream to ensure
|
|
// we know the full size of the packet at the other end.
|
|
err := binary.Write(*ms.Stream, binary.LittleEndian, uint16(plen))
|
|
if err == nil {
|
|
// Write the packet out to the libp2p stream.
|
|
_, err = (*ms.Stream).Write(packet[:plen])
|
|
...
|
|
}
|
|
...
|
|
}() { return }
|
|
}
|
|
...
|
|
}
|