sdk: extract method in types

Kim committed Nov 19, 2025 at 14:20 UTC de5050228dde02e817f0d341d2d1f0596af0e4a2
2 files changed +115 -121
sdk/client.go
+115
@@ -5,6 +5,7 @@ import (
5 "encoding/json"
6 "fmt"
7 "io"
8 + "net"
9 "strings"
10 "sync"
11 "time"
@@ -651,3 +652,117 @@ func (g *Client) LookupName(name string) (*rdverb.Lease, error) {
652 }
653 return nil, ErrNoAvailableRelay
654 }
655 +
656 +type listener struct {
657 + mu sync.Mutex
658 +
659 + cred *cryptoops.Credential
660 + lease *rdverb.Lease
661 +
662 + conns map[*connection]struct{}
663 +
664 + connCh chan *connection
665 + closed bool
666 +}
667 +
668 +// Implement net.Listener interface for Listener
669 +func (l *listener) Accept() (net.Conn, error) {
670 + conn, ok := <-l.connCh
671 + if !ok {
672 + return nil, net.ErrClosed
673 + }
674 + return conn, nil
675 +}
676 +
677 +func (l *listener) Close() error {
678 + l.mu.Lock()
679 + defer l.mu.Unlock()
680 +
681 + if l.closed {
682 + return nil
683 + }
684 +
685 + l.closed = true
686 +
687 + // Close the connection channel first to prevent new connections
688 + close(l.connCh)
689 +
690 + // Close all active connections
691 + for conn := range l.conns {
692 + if err := conn.Close(); err != nil {
693 + log.Error().Err(err).Msg("[SDK] Error closing connection")
694 + }
695 + delete(l.conns, conn)
696 + }
697 +
698 + // Clear the connections map
699 + l.conns = make(map[*connection]struct{})
700 +
701 + return nil
702 +}
703 +
704 +func (l *listener) Addr() net.Addr {
705 + return addr(l.cred.ID())
706 +}
707 +
708 +type connRelay struct {
709 + addr string
710 + client *portal.RelayClient
711 + dialer func(context.Context, string) (io.ReadWriteCloser, error)
712 + stop chan struct{}
713 + stopOnce sync.Once // Ensure stop channel is closed only once
714 + mu sync.Mutex
715 +}
716 +
717 +var _ net.Conn = (*connection)(nil)
718 +
719 +type connection struct {
720 + via *connRelay
721 + localAddr string
722 + remoteAddr string
723 + conn *cryptoops.SecureConnection
724 +}
725 +
726 +func (r *connection) Read(b []byte) (n int, err error) {
727 + return r.conn.Read(b)
728 +}
729 +
730 +func (r *connection) Write(b []byte) (n int, err error) {
731 + return r.conn.Write(b)
732 +}
733 +
734 +func (r *connection) Close() error {
735 + return r.conn.Close()
736 +}
737 +
738 +func (r *connection) LocalAddr() net.Addr {
739 + return addr(r.localAddr)
740 +}
741 +
742 +func (r *connection) RemoteAddr() net.Addr {
743 + return addr(r.remoteAddr)
744 +}
745 +
746 +func (r *connection) SetDeadline(t time.Time) error {
747 + return r.conn.SetDeadline(t)
748 +}
749 +
750 +func (r *connection) SetReadDeadline(t time.Time) error {
751 + return r.conn.SetReadDeadline(t)
752 +}
753 +
754 +func (r *connection) SetWriteDeadline(t time.Time) error {
755 + return r.conn.SetWriteDeadline(t)
756 +}
757 +
758 +var _ net.Addr = (*addr)(nil)
759 +
760 +type addr string
761 +
762 +func (a addr) Network() string {
763 + return "portal"
764 +}
765 +
766 +func (a addr) String() string {
767 + return string(a)
768 +}
sdk/types.go
-121
@@ -4,14 +4,7 @@ import (
4 "context"
5 "errors"
6 "io"
7 - "net"
8 - "sync"
7 "time"
10 -
11 - "github.com/rs/zerolog/log"
12 - "gosuda.org/portal/portal"
13 - "gosuda.org/portal/portal/core/cryptoops"
14 - "gosuda.org/portal/portal/core/proto/rdverb"
8 )
9
10 var (
@@ -111,117 +104,3 @@ func WithHide(hide bool) MetadataOption {
104 m.Hide = hide
105 }
106 }
114 -
115 -type listener struct {
116 - mu sync.Mutex
117 -
118 - cred *cryptoops.Credential
119 - lease *rdverb.Lease
120 -
121 - conns map[*connection]struct{}
122 -
123 - connCh chan *connection
124 - closed bool
125 -}
126 -
127 -// Implement net.Listener interface for Listener
128 -func (l *listener) Accept() (net.Conn, error) {
129 - conn, ok := <-l.connCh
130 - if !ok {
131 - return nil, net.ErrClosed
132 - }
133 - return conn, nil
134 -}
135 -
136 -func (l *listener) Close() error {
137 - l.mu.Lock()
138 - defer l.mu.Unlock()
139 -
140 - if l.closed {
141 - return nil
142 - }
143 -
144 - l.closed = true
145 -
146 - // Close the connection channel first to prevent new connections
147 - close(l.connCh)
148 -
149 - // Close all active connections
150 - for conn := range l.conns {
151 - if err := conn.Close(); err != nil {
152 - log.Error().Err(err).Msg("[SDK] Error closing connection")
153 - }
154 - delete(l.conns, conn)
155 - }
156 -
157 - // Clear the connections map
158 - l.conns = make(map[*connection]struct{})
159 -
160 - return nil
161 -}
162 -
163 -func (l *listener) Addr() net.Addr {
164 - return addr(l.cred.ID())
165 -}
166 -
167 -type connRelay struct {
168 - addr string
169 - client *portal.RelayClient
170 - dialer func(context.Context, string) (io.ReadWriteCloser, error)
171 - stop chan struct{}
172 - stopOnce sync.Once // Ensure stop channel is closed only once
173 - mu sync.Mutex
174 -}
175 -
176 -var _ net.Conn = (*connection)(nil)
177 -
178 -type connection struct {
179 - via *connRelay
180 - localAddr string
181 - remoteAddr string
182 - conn *cryptoops.SecureConnection
183 -}
184 -
185 -func (r *connection) Read(b []byte) (n int, err error) {
186 - return r.conn.Read(b)
187 -}
188 -
189 -func (r *connection) Write(b []byte) (n int, err error) {
190 - return r.conn.Write(b)
191 -}
192 -
193 -func (r *connection) Close() error {
194 - return r.conn.Close()
195 -}
196 -
197 -func (r *connection) LocalAddr() net.Addr {
198 - return addr(r.localAddr)
199 -}
200 -
201 -func (r *connection) RemoteAddr() net.Addr {
202 - return addr(r.remoteAddr)
203 -}
204 -
205 -func (r *connection) SetDeadline(t time.Time) error {
206 - return r.conn.SetDeadline(t)
207 -}
208 -
209 -func (r *connection) SetReadDeadline(t time.Time) error {
210 - return r.conn.SetReadDeadline(t)
211 -}
212 -
213 -func (r *connection) SetWriteDeadline(t time.Time) error {
214 - return r.conn.SetWriteDeadline(t)
215 -}
216 -
217 -var _ net.Addr = (*addr)(nil)
218 -
219 -type addr string
220 -
221 -func (a addr) Network() string {
222 - return "portal"
223 -}
224 -
225 -func (a addr) String() string {
226 - return string(a)
227 -}