forked from cryptix/goSam
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathaccept.go
62 lines (52 loc) · 1.33 KB
/
accept.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package gosam
import (
"fmt"
"net"
)
// 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) {
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
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
if c.debug {
c.SamConn = WrapConn(c.SamConn)
}
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()
if err != nil {
return nil, err
}
fmt.Println("Accept Resp:", resp)
return c.SamConn, nil
}