mirror of
https://github.com/go-i2p/go-i2p.git
synced 2025-07-21 11:01:24 -04:00
* start on initial i2np transport code
* move ssu such that it's a subdirectory of transport package * fix i2np package name to be i2np not stdi2p
This commit is contained in:
@@ -1 +1,3 @@
|
||||
package stdi2p
|
||||
package i2np
|
||||
|
||||
type I2NPMessage []byte
|
||||
|
4
lib/transport/doc.go
Normal file
4
lib/transport/doc.go
Normal file
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
i2np messages transports
|
||||
*/
|
||||
package transport
|
8
lib/transport/errors.go
Normal file
8
lib/transport/errors.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package transport
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
// error for when we have no transports available to use
|
||||
var ErrNoTransportAvailable = errors.New("no transports available")
|
85
lib/transport/multi.go
Normal file
85
lib/transport/multi.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package transport
|
||||
|
||||
import (
|
||||
"github.com/bounce-chat/go-i2p/lib/common"
|
||||
)
|
||||
|
||||
// muxes multiple transports into 1 Transport
|
||||
// implements transport.Transport
|
||||
type TransportMuxer struct {
|
||||
// the underlying transports we are using in order of most prominant to least
|
||||
trans []Transport
|
||||
}
|
||||
|
||||
// mux a bunch of transports together
|
||||
func Mux(t ...Transport) (tmux *TransportMuxer) {
|
||||
tmux = new(TransportMuxer)
|
||||
tmux.trans = append(tmux.trans, t...)
|
||||
return
|
||||
}
|
||||
|
||||
// set the identity for every transport
|
||||
func (tmux *TransportMuxer) SetIdentity(ident common.RouterIdentity) (err error) {
|
||||
for _, t := range tmux.trans {
|
||||
err = t.SetIdentity(ident)
|
||||
if err != nil {
|
||||
// an error happened let's return and complain
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// close every transport that this transport muxer has
|
||||
func (tmux *TransportMuxer) Close() (err error) {
|
||||
for _, t := range tmux.trans {
|
||||
err = t.Close()
|
||||
if t != nil {
|
||||
// TODO: handle error (?)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// the name of this transport with the names of all the ones that we mux
|
||||
func (tmux *TransportMuxer) Name() string {
|
||||
name := "Muxed Transport: "
|
||||
for _, t := range tmux.trans {
|
||||
name += t.Name() + ", "
|
||||
}
|
||||
return name[len(name)-3:]
|
||||
}
|
||||
|
||||
// get a transport session given a router info
|
||||
// return session and nil if successful
|
||||
// return nil and ErrNoTransportAvailable if we failed to get a session
|
||||
func (tmux *TransportMuxer) GetSession(routerInfo common.RouterInfo) (s TransportSession, err error) {
|
||||
for _, t := range tmux.trans {
|
||||
// pick the first one that is compatable
|
||||
if t.Compatable(routerInfo) {
|
||||
// try to get a session
|
||||
s, err = t.GetSession(routerInfo)
|
||||
if err != nil {
|
||||
// we could not get a session
|
||||
// try the next transport
|
||||
continue
|
||||
}
|
||||
// we got a session
|
||||
return
|
||||
}
|
||||
}
|
||||
// we failed to get a session for this routerInfo
|
||||
err = ErrNoTransportAvailable
|
||||
return
|
||||
}
|
||||
|
||||
// is there a transport that we mux that is compatable with this router info?
|
||||
func (tmux *TransportMuxer) Compatable(routerInfo common.RouterInfo) (compat bool) {
|
||||
for _, t := range tmux.trans {
|
||||
if t.Compatable(routerInfo) {
|
||||
compat = true
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
47
lib/transport/transport.go
Normal file
47
lib/transport/transport.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package transport
|
||||
|
||||
import (
|
||||
"github.com/bounce-chat/go-i2p/lib/common"
|
||||
"github.com/bounce-chat/go-i2p/lib/i2np"
|
||||
)
|
||||
|
||||
// a session between 2 routers for tranmitting i2np messages securly
|
||||
type TransportSession interface {
|
||||
// queue an i2np message to be sent over the session
|
||||
// will block as long as the send queue is full
|
||||
// does not block if the queue is not full
|
||||
QueueSendI2NP(msg i2np.I2NPMessage)
|
||||
// return how many i2np messages are not completely sent yet
|
||||
SendQueueSize() int
|
||||
// blocking read the next fully recv'd i2np message from this session
|
||||
ReadNextI2NP() (i2np.I2NPMessage, error)
|
||||
// close the session cleanly
|
||||
// returns any errors that happen while closing the session
|
||||
Close() error
|
||||
}
|
||||
|
||||
type Transport interface {
|
||||
|
||||
// Set the router identity for this transport.
|
||||
// will bind if the underlying socket is not already
|
||||
// if the underlying socket is already bound update the RouterIdentity
|
||||
// returns any errors that happen if they do
|
||||
SetIdentity(ident common.RouterIdentity) error
|
||||
|
||||
// Obtain a transport session with a router given its RouterInfo.
|
||||
// If a session with this router is NOT already made attempt to create one and block until made or until an error happens
|
||||
// returns an established TransportSession and nil on success
|
||||
// returns nil and an error on error
|
||||
GetSession(routerInfo common.RouterInfo) (TransportSession, error)
|
||||
|
||||
// return true if a routerInfo is compatable with this transport
|
||||
Compatable(routerInfo common.RouterInfo) bool
|
||||
|
||||
// close the transport cleanly
|
||||
// blocks until done
|
||||
// returns an error if one happens
|
||||
Close() error
|
||||
|
||||
// get the name of this tranport as a string
|
||||
Name() string
|
||||
}
|
Reference in New Issue
Block a user