Files
goSam/accept.go

63 lines
1.3 KiB
Go
Raw Permalink Normal View History

2014-02-12 22:14:19 +01:00
package goSam
import (
"fmt"
"net"
)
2019-07-05 03:10:01 -04:00
// AcceptI2P creates a new Client and accepts a connection on it
func (c *Client) AcceptI2P() (net.Conn, error) {
listener, err := c.Listen()
if err != nil {
return nil, err
}
return listener.Accept()
}
// Listen creates a new Client and returns a net.listener which *must* be started
// with Accept
func (c *Client) Listen() (net.Listener, error) {
2019-07-12 16:46:54 -04:00
return c.ListenI2P(c.destination)
}
// ListenI2P creates a new Client and returns a net.listener which *must* be started
// with Accept
func (c *Client) ListenI2P(dest string) (net.Listener, error) {
var err error
c.destination, err = c.CreateStreamSession(dest)
d := c.destination
2014-02-12 22:14:19 +01:00
if err != nil {
return nil, err
}
fmt.Println("Listening on destination:", c.Base32()+".b32.i2p")
c, err = c.NewClient(c.id)
if err != nil {
return nil, err
}
c.destination = d
2014-02-12 22:14:19 +01:00
if c.debug {
2019-12-08 16:57:51 -05:00
c.SamConn = WrapConn(c.SamConn)
2014-02-12 22:14:19 +01:00
}
return c, nil
2019-07-05 03:10:01 -04:00
}
// Accept accepts a connection on a listening goSam.Client(Implements net.Listener)
// or, if the connection isn't listening yet, just calls AcceptI2P for compatibility
// with older versions.
func (c *Client) Accept() (net.Conn, error) {
if c.id == 0 {
return c.AcceptI2P()
}
resp, err := c.StreamAccept()
2014-02-12 22:14:19 +01:00
if err != nil {
return nil, err
}
fmt.Println("Accept Resp:", resp)
return c.SamConn, nil
2014-02-12 22:14:19 +01:00
}