mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-29 01:12:24 +08:00
gofmt
This commit is contained in:
@ -1,18 +1,18 @@
|
|||||||
package bitswap
|
package bitswap
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/jbenet/go-ipfs/blocks"
|
"github.com/jbenet/go-ipfs/blocks"
|
||||||
"github.com/jbenet/go-multihash"
|
"github.com/jbenet/go-multihash"
|
||||||
)
|
)
|
||||||
|
|
||||||
// aliases
|
// aliases
|
||||||
|
|
||||||
type Ledger struct {
|
type Ledger struct {
|
||||||
// todo
|
// todo
|
||||||
}
|
}
|
||||||
|
|
||||||
type BitSwap struct {
|
type BitSwap struct {
|
||||||
Ledgers map[string]*Ledger
|
Ledgers map[string]*Ledger
|
||||||
HaveList map[string]*blocks.Block
|
HaveList map[string]*blocks.Block
|
||||||
WantList []*multihash.Multihash
|
WantList []*multihash.Multihash
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package netmux
|
package netmux
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
)
|
)
|
||||||
|
|
||||||
// An interface is the module connecting netmux
|
// An interface is the module connecting netmux
|
||||||
@ -9,88 +9,88 @@ import (
|
|||||||
// It keeps the relevant connections open.
|
// It keeps the relevant connections open.
|
||||||
type Interface struct {
|
type Interface struct {
|
||||||
|
|
||||||
// Interface network (e.g. udp4, tcp6)
|
// Interface network (e.g. udp4, tcp6)
|
||||||
Network string
|
Network string
|
||||||
|
|
||||||
// Own network address
|
// Own network address
|
||||||
Address string
|
Address string
|
||||||
ResolvedAddress *net.UDPAddr
|
ResolvedAddress *net.UDPAddr
|
||||||
|
|
||||||
// Connection
|
// Connection
|
||||||
conn net.Conn
|
conn net.Conn
|
||||||
|
|
||||||
// next packets + close control channels
|
// next packets + close control channels
|
||||||
Input chan *Packet
|
Input chan *Packet
|
||||||
Output chan *Packet
|
Output chan *Packet
|
||||||
Closed chan bool
|
Closed chan bool
|
||||||
Errors chan error
|
Errors chan error
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUDPInterface(network, addr string) (*Interface, error) {
|
func NewUDPInterface(network, addr string) (*Interface, error) {
|
||||||
raddr, err := net.ResolveUDPAddr(network, addr)
|
raddr, err := net.ResolveUDPAddr(network, addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
conn, err := net.ListenUDP(network, raddr)
|
conn, err := net.ListenUDP(network, raddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
i := &Interface{
|
i := &Interface{
|
||||||
Network: network,
|
Network: network,
|
||||||
Address: addr,
|
Address: addr,
|
||||||
ResolvedAddress: raddr,
|
ResolvedAddress: raddr,
|
||||||
conn: conn,
|
conn: conn,
|
||||||
}
|
}
|
||||||
|
|
||||||
go i.processUDPInput()
|
go i.processUDPInput()
|
||||||
go i.processOutput()
|
go i.processOutput()
|
||||||
return i, nil
|
return i, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Interface) processOutput() {
|
func (i *Interface) processOutput() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-i.Closed:
|
case <-i.Closed:
|
||||||
break
|
break
|
||||||
|
|
||||||
case buffer := <-i.Output:
|
case buffer := <-i.Output:
|
||||||
i.conn.Write([]byte(buffer.Data))
|
i.conn.Write([]byte(buffer.Data))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Interface) processUDPInput() {
|
func (i *Interface) processUDPInput() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-i.Closed:
|
case <-i.Closed:
|
||||||
break
|
break
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Interface) Read(buffer []byte) bool {
|
func (i *Interface) Read(buffer []byte) bool {
|
||||||
_, err := i.conn.Read(buffer)
|
_, err := i.conn.Read(buffer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
i.Errors <- err
|
i.Errors <- err
|
||||||
i.Close()
|
i.Close()
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Interface) Close() {
|
func (i *Interface) Close() {
|
||||||
// closing net connection
|
// closing net connection
|
||||||
err := i.conn.Close()
|
err := i.conn.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
i.Errors <- err
|
i.Errors <- err
|
||||||
}
|
}
|
||||||
|
|
||||||
// closing channels
|
// closing channels
|
||||||
close(i.Input)
|
close(i.Input)
|
||||||
close(i.Output)
|
close(i.Output)
|
||||||
close(i.Closed)
|
close(i.Closed)
|
||||||
close(i.Errors)
|
close(i.Errors)
|
||||||
}
|
}
|
||||||
|
@ -6,41 +6,41 @@ package netmux
|
|||||||
// and multiplex everything over one interface.
|
// and multiplex everything over one interface.
|
||||||
|
|
||||||
type Netmux struct {
|
type Netmux struct {
|
||||||
// the list of NetMux interfaces
|
// the list of NetMux interfaces
|
||||||
Interfaces []*Interface
|
Interfaces []*Interface
|
||||||
|
|
||||||
// The channels to send/recv from
|
// The channels to send/recv from
|
||||||
Incoming <-chan *Packet
|
Incoming <-chan *Packet
|
||||||
Outgoing chan<- *Packet
|
Outgoing chan<- *Packet
|
||||||
|
|
||||||
// internally managed other side of channels
|
// internally managed other side of channels
|
||||||
incomingSrc chan<- *Packet
|
incomingSrc chan<- *Packet
|
||||||
outgoingSrc <-chan *Packet
|
outgoingSrc <-chan *Packet
|
||||||
}
|
}
|
||||||
|
|
||||||
// Warning: will probably change to adopt multiaddr format
|
// Warning: will probably change to adopt multiaddr format
|
||||||
type Packet struct {
|
type Packet struct {
|
||||||
// the network addresses to send to
|
// the network addresses to send to
|
||||||
// e.g. tcp4://127.0.0.1:12345
|
// e.g. tcp4://127.0.0.1:12345
|
||||||
NetAddrTo string
|
NetAddrTo string
|
||||||
|
|
||||||
// the network addresses to recv from
|
// the network addresses to recv from
|
||||||
// e.g. tcp4://127.0.0.1:12345
|
// e.g. tcp4://127.0.0.1:12345
|
||||||
// may be left blank to select one automatically.
|
// may be left blank to select one automatically.
|
||||||
NetAddrFrom string
|
NetAddrFrom string
|
||||||
|
|
||||||
// the data to send.
|
// the data to send.
|
||||||
Data []byte
|
Data []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewNetmux() *Netmux {
|
func NewNetmux() *Netmux {
|
||||||
n := &Netmux{}
|
n := &Netmux{}
|
||||||
|
|
||||||
// setup channels
|
// setup channels
|
||||||
och := make(chan *Packet)
|
och := make(chan *Packet)
|
||||||
ich := make(chan *Packet)
|
ich := make(chan *Packet)
|
||||||
n.Incoming, n.incomingSrc = ich, ich
|
n.Incoming, n.incomingSrc = ich, ich
|
||||||
n.Outgoing, n.outgoingSrc = och, och
|
n.Outgoing, n.outgoingSrc = och, och
|
||||||
|
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user