Files
goSam/accept.go

66 lines
1.3 KiB
Go
Raw Normal View History

2014-02-12 22:14:19 +01:00
package goSam
import (
"fmt"
"net"
2015-03-25 21:37:41 +01:00
"github.com/eyedeekay/goSam/debug"
2014-02-12 22:14:19 +01:00
)
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
var id int32
2019-07-05 03:10:01 -04:00
c.id = c.NewID()
2019-07-12 16:46:54 -04:00
c.destination, err = c.CreateStreamSession(id, dest)
2014-02-12 22:14:19 +01:00
if err != nil {
return nil, err
}
fmt.Println("destination:", c.destination)
2014-02-12 22:14:19 +01:00
c, err = c.NewClient()
2014-02-12 22:14:19 +01:00
if err != nil {
return nil, err
}
if c.debug {
c.SamConn = debug.WrapConn(c.SamConn)
2014-02-12 22:14:19 +01:00
}
2019-07-05 03:10:01 -04:00
return c, nil
}
// 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(c.id)
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
}