|
|
2 дней назад | |
|---|---|---|
| .. | ||
| .gitignore | 2 дней назад | |
| README.md | 2 дней назад | |
| chunk.go | 2 дней назад | |
| chunk_queue.go | 2 дней назад | |
| conn.go | 2 дней назад | |
| conn_map.go | 2 дней назад | |
| delay_filter.go | 2 дней назад | |
| errors.go | 2 дней назад | |
| interface.go | 2 дней назад | |
| loss_filter.go | 2 дней назад | |
| nat.go | 2 дней назад | |
| net.go | 2 дней назад | |
| resolver.go | 2 дней назад | |
| router.go | 2 дней назад | |
| tbf.go | 2 дней назад | |
| udpproxy.go | 2 дней назад | |
| udpproxy_direct.go | 2 дней назад | |
| vnet.go | 2 дней назад | |
A virtual network layer for pion.
......................................
: Virtual Network (vnet) :
: :
+-------+ * 1 +----+ +--------+ :
| :App |------------>|:Net|--o<-----|:Router | :
+-------+ +----+ | | :
+-----------+ * 1 +----+ | | :
|:STUNServer|-------->|:Net|--o<-----| | :
+-----------+ +----+ | | :
+-----------+ * 1 +----+ | | :
|:TURNServer|-------->|:Net|--o<-----| | :
+-----------+ +----+ [1] | | :
: 1 | | 1 <<has>> :
: +---<>| |<>----+ [2] :
: | +--------+ | :
To form | *| v 0..1 :
a subnet tree | o [3] +-----+ :
: | ^ |:NAT | :
: | | +-----+ :
: +-------+ :
......................................
Note:
o: NIC (Netork Interface Controller)
[1]: Net implments NIC interface.
[2]: Root router has no NAT. All child routers have a NAT always.
[3]: Router implements NIC interface for accesses from the
parent router.
Net provides 3 interfaces:
Router access via NIC interface
(Pion module/app, ICE servers, etc.)
+-----------+
| :App |
+-----------+
* |
| <<uses>>
1 v
+---------+ 1 * +-----------+ 1 * +-----------+ 1 * +------+
..| :Router |----+------>o--| :Net |<>------|:Interface |<>------|:Addr |
+---------+ | NIC +-----------+ +-----------+ +------+
| <<interface>> (vnet.Interface) (net.Addr)
|
| * +-----------+ 1 * +-----------+ 1 * +------+
+------>o--| :Router |<>------|:Interface |<>------|:Addr |
NIC +-----------+ +-----------+ +------+
<<interface>> (vnet.Interface) (net.Addr)
The instance of
Netwill be the one passed around the project. Net class has public methods for configuration and for application use.
Net (of type vnet.Net) property. (just like how
we distribute LoggerFactory throughout the pion project.lo0 and eth0 interface, and lo0 will
have one IP address, 127.0.0.1. (this is not used in pion/ice, however)eth0
interface.
import (
"net"
"github.com/pion/transport/vnet"
"github.com/pion/logging"
)
// Create WAN (a root router).
wan, err := vnet.NewRouter(&RouterConfig{
CIDR: "0.0.0.0/0",
LoggerFactory: logging.NewDefaultLoggerFactory(),
})
// Create a network.
// You can specify a static IP for the instance of Net to use. If not specified,
// router will assign an IP address that is contained in the router's CIDR.
nw := vnet.NewNet(&vnet.NetConfig{
StaticIP: "27.1.2.3",
})
// Add the network to the router.
// The router will assign an IP address to `nw`.
if err = wan.AddNet(nw); err != nil {
// handle error
}
// Start router.
// This will start internal goroutine to route packets.
// If you set child routers (using AddRouter), the call on the root router
// will start the rest of routers for you.
if err = wan.Start(); err != nil {
// handle error
}
//
// Your application runs here using `nw`.
//
// Stop the router.
// This will stop all internal goroutines in the router tree.
// (No need to call Stop() on child routers)
if err = wan.Stop(); err != nil {
// handle error
}
The instance of vnet.Net wraps a subset of net package to enable operations
on the virtual network. Your project must be able to pass the instance to
all your routines that do network operation with net package. A typical way
is to use a config param to create your instances with the virtual network
instance (nw in the above example) like this:
type AgentConfig struct {
:
Net: *vnet.Net,
}
type Agent struct {
:
net: *vnet.Net,
}
func NetAgent(config *AgentConfig) *Agent {
if config.Net == nil {
config.Net = vnet.NewNet(nil) // defaults to native operation
}
return &Agent {
:
net: config.Net,
}
}
// a.net is the instance of vnet.Net class
func (a *Agent) listenUDP(...) error {
conn, err := a.net.ListenPacket(udpString, ...)
if err != nil {
return nil, err
}
:
}
net(built-in) |
vnet |
Note |
|---|---|---|
| net.Interfaces() | a.net.Interfaces() | |
| net.InterfaceByName() | a.net.InterfaceByName() | |
| net.ResolveUDPAddr() | a.net.ResolveUDPAddr() | |
| net.ListenPacket() | a.net.ListenPacket() | |
| net.ListenUDP() | a.net.ListenUDP() | (ListenPacket() is recommended) |
| net.Listen() | a.net.Listen() | (TODO) |
| net.ListenTCP() | (not supported) | (Listen() would be recommended) |
| net.Dial() | a.net.Dial() | |
| net.DialUDP() | a.net.DialUDP() | |
| net.DialTCP() | (not supported) | |
| net.Interface | vnet.Interface | |
| net.PacketConn | (use it as-is) | |
| net.UDPConn | vnet.UDPConn | Use vnet.UDPPacketConn in your code |
| net.TCPConn | vnet.TCPConn | (TODO) |
| net.Dialer | vnet.Dialer | Use a.net.CreateDialer() to create it. The use of vnet.Dialer is currently experimental. |
a.netis an instance of Net class, and types are defined under the package namevnetMost of other
interfacetypes in net package can be used as is.Please post a github issue when other types/methods need to be added to vnet/vnet.Net.