@@ -10,9 +10,7 @@ import (
10
10
11
11
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
12
12
"github.com/lightninglabs/lightning-node-connect/gbn"
13
- "github.com/lightninglabs/lightning-node-connect/hashmailrpc"
14
13
"google.golang.org/protobuf/encoding/protojson"
15
- "nhooyr.io/websocket"
16
14
)
17
15
18
16
var (
@@ -74,16 +72,6 @@ const (
74
72
// gbnPongTimout is the time after sending the pong message that we will
75
73
// timeout if we do not receive any message from our peer.
76
74
gbnPongTimeout = 3 * time .Second
77
-
78
- // webSocketRecvLimit is used to set the websocket receive limit. The
79
- // default value of 32KB is enough due to the fact that grpc has a
80
- // default packet maximum of 32KB which we then further wrap in gbn and
81
- // hashmail messages.
82
- webSocketRecvLimit int64 = 100 * 1024 // 100KB
83
-
84
- // sendSocketTimeout is the timeout used for context cancellation on the
85
- // send socket.
86
- sendSocketTimeout = 1000 * time .Millisecond
87
75
)
88
76
89
77
// ClientStatus is a description of the connection status of the client.
@@ -121,11 +109,9 @@ func (c ClientStatus) String() string {
121
109
type ClientConn struct {
122
110
* connKit
123
111
124
- receiveSocket * websocket.Conn
125
- receiveMu sync.Mutex
126
-
127
- sendSocket * websocket.Conn
128
- sendMu sync.Mutex
112
+ transport ClientConnTransport
113
+ receiveMu sync.Mutex
114
+ sendMu sync.Mutex
129
115
130
116
gbnConn * gbn.GoBackNConn
131
117
gbnOptions []gbn.Option
@@ -147,12 +133,18 @@ func NewClientConn(ctx context.Context, sid [64]byte, serverHost string,
147
133
148
134
receiveSID := GetSID (sid , true )
149
135
sendSID := GetSID (sid , false )
136
+ mailBoxInfo := & mailboxInfo {
137
+ addr : serverHost ,
138
+ recvSID : receiveSID [:],
139
+ sendSID : sendSID [:],
140
+ }
150
141
151
142
log .Debugf ("New client conn, read_stream=%x, write_stream=%x" ,
152
143
receiveSID [:], sendSID [:])
153
144
154
145
ctxc , cancel := context .WithCancel (ctx )
155
146
c := & ClientConn {
147
+ transport : newWebsocketTransport (mailBoxInfo ),
156
148
gbnOptions : []gbn.Option {
157
149
gbn .WithTimeout (gbnTimeout ),
158
150
gbn .WithHandshakeTimeout (gbnHandshakeTimeout ),
@@ -203,6 +195,7 @@ func RefreshClientConn(ctx context.Context, c *ClientConn) (*ClientConn,
203
195
c .receiveSID [:], c .sendSID [:])
204
196
205
197
cc := & ClientConn {
198
+ transport : c .transport .Refresh (),
206
199
status : ClientStatusNotConnected ,
207
200
onNewStatus : c .onNewStatus ,
208
201
gbnOptions : c .gbnOptions ,
@@ -275,10 +268,11 @@ func statusFromError(err error) ClientStatus {
275
268
// independently of the ClientConn.
276
269
func (c * ClientConn ) recv (ctx context.Context ) ([]byte , error ) {
277
270
c .receiveMu .Lock ()
278
- if c .receiveSocket == nil {
271
+ defer c .receiveMu .Unlock ()
272
+
273
+ if ! c .transport .ReceiveConnected () {
279
274
c .createReceiveMailBox (ctx , 0 )
280
275
}
281
- c .receiveMu .Unlock ()
282
276
283
277
for {
284
278
select {
@@ -289,38 +283,22 @@ func (c *ClientConn) recv(ctx context.Context) ([]byte, error) {
289
283
default :
290
284
}
291
285
292
- c .receiveMu .Lock ()
293
- _ , msg , err := c .receiveSocket .Read (ctx )
286
+ msg , retry , errStatus , err := c .transport .Recv (ctx )
294
287
if err != nil {
295
- log .Debugf ("Client: got failure on receive socket, " +
296
- "re-trying: %v" , err )
288
+ if ! retry {
289
+ return nil , err
290
+ }
297
291
298
- c .setStatus (ClientStatusNotConnected )
299
- c .createReceiveMailBox (ctx , retryWait )
300
- c .receiveMu .Unlock ()
301
- continue
302
- }
303
- unwrapped , err := stripJSONWrapper (string (msg ))
304
- if err != nil {
305
- log .Debugf ("Client: got error message from receive " +
306
- "socket: %v" , err )
292
+ log .Debugf ("Client: got failure on receive " +
293
+ "socket/stream, re-trying: %v" , err )
307
294
308
- c .setStatus (statusFromError ( err ) )
295
+ c .setStatus (errStatus )
309
296
c .createReceiveMailBox (ctx , retryWait )
310
- c .receiveMu .Unlock ()
311
297
continue
312
298
}
313
- c .receiveMu .Unlock ()
314
-
315
- mailboxMsg := & hashmailrpc.CipherBox {}
316
- err = defaultMarshaler .Unmarshal ([]byte (unwrapped ), mailboxMsg )
317
- if err != nil {
318
- return nil , err
319
- }
320
299
321
300
c .setStatus (ClientStatusConnected )
322
-
323
- return mailboxMsg .Msg , nil
301
+ return msg , nil
324
302
}
325
303
}
326
304
@@ -329,12 +307,13 @@ func (c *ClientConn) recv(ctx context.Context) ([]byte, error) {
329
307
// cancellation of a context so that the gbn connection is able to close
330
308
// independently of the ClientConn.
331
309
func (c * ClientConn ) send (ctx context.Context , payload []byte ) error {
332
- // Set up the send-socket if it has not yet been initialized.
333
310
c .sendMu .Lock ()
334
- if c .sendSocket == nil {
311
+ defer c .sendMu .Unlock ()
312
+
313
+ // Set up the send-socket if it has not yet been initialized.
314
+ if ! c .transport .SendConnected () {
335
315
c .createSendMailBox (ctx , 0 )
336
316
}
337
- c .sendMu .Unlock ()
338
317
339
318
// Retry sending the payload to the hashmail server until it succeeds.
340
319
for {
@@ -346,34 +325,21 @@ func (c *ClientConn) send(ctx context.Context, payload []byte) error {
346
325
default :
347
326
}
348
327
349
- sendInit := & hashmailrpc.CipherBox {
350
- Desc : & hashmailrpc.CipherBoxDesc {
351
- StreamId : c .sendSID [:],
352
- },
353
- Msg : payload ,
354
- }
355
-
356
- sendInitBytes , err := defaultMarshaler .Marshal (sendInit )
357
- if err != nil {
358
- return err
359
- }
360
-
361
- c .sendMu .Lock ()
362
- ctxt , cancel := context .WithTimeout (ctx , sendSocketTimeout )
363
- err = c .sendSocket .Write (
364
- ctxt , websocket .MessageText , sendInitBytes ,
328
+ retry , errStatus , err := c .transport .Send (
329
+ ctx , c .sendSID [:], payload ,
365
330
)
366
- cancel ()
367
331
if err != nil {
368
- log .Debugf ("Client: got failure on send socket, " +
369
- "re-trying: %v" , err )
332
+ if ! retry {
333
+ return err
334
+ }
335
+
336
+ log .Debugf ("Client: got failure on send " +
337
+ "socket/stream, re-trying: %v" , err )
370
338
371
- c .setStatus (statusFromError ( err ) )
339
+ c .setStatus (errStatus )
372
340
c .createSendMailBox (ctx , retryWait )
373
- c .sendMu .Unlock ()
374
341
continue
375
342
}
376
- c .sendMu .Unlock ()
377
343
378
344
return nil
379
345
}
@@ -400,38 +366,9 @@ func (c *ClientConn) createReceiveMailBox(ctx context.Context,
400
366
401
367
waiter .Wait ()
402
368
403
- receiveAddr := fmt .Sprintf (
404
- addrFormat , c .serverAddr , receivePath ,
405
- )
406
- receiveSocket , _ , err := websocket .Dial (ctx , receiveAddr , nil )
407
- if err != nil {
408
- log .Debugf ("Client: error creating receive socket %v" ,
409
- err )
410
-
411
- continue
412
- }
413
- receiveSocket .SetReadLimit (webSocketRecvLimit )
414
- c .receiveSocket = receiveSocket
415
-
416
- receiveInit := & hashmailrpc.CipherBoxDesc {
417
- StreamId : c .receiveSID [:],
418
- }
419
- receiveInitBytes , err := defaultMarshaler .Marshal (receiveInit )
420
- if err != nil {
421
- log .Debugf ("Client: error marshaling receive init " +
422
- "bytes %w" , err )
423
-
424
- continue
425
- }
426
-
427
- ctxt , cancel := context .WithTimeout (ctx , sendSocketTimeout )
428
- err = c .receiveSocket .Write (
429
- ctxt , websocket .MessageText , receiveInitBytes ,
430
- )
431
- cancel ()
432
- if err != nil {
433
- log .Debugf ("Client: error creating receive stream " +
434
- "%v" , err )
369
+ if err := c .transport .ConnectReceive (ctx ); err != nil {
370
+ log .Errorf ("Client: error connecting to receive " +
371
+ "socket/stream: %v" )
435
372
436
373
continue
437
374
}
@@ -459,17 +396,14 @@ func (c *ClientConn) createSendMailBox(ctx context.Context,
459
396
460
397
waiter .Wait ()
461
398
462
- log .Debugf ("Client: Attempting to create send socket" )
463
- sendAddr := fmt .Sprintf (addrFormat , c .serverAddr , sendPath )
464
- sendSocket , _ , err := websocket .Dial (ctx , sendAddr , nil )
465
- if err != nil {
466
- log .Debugf ("Client: error creating send socket %v" , err )
399
+ log .Debugf ("Client: Attempting to create send socket/stream" )
400
+ if err := c .transport .ConnectSend (ctx ); err != nil {
401
+ log .Debugf ("Client: error connecting to send " +
402
+ "stream/socket %v" , err )
467
403
continue
468
404
}
469
405
470
- c .sendSocket = sendSocket
471
-
472
- log .Debugf ("Client: Send socket created" )
406
+ log .Debugf ("Client: Connected to send socket/stream" )
473
407
return
474
408
}
475
409
}
@@ -528,31 +462,18 @@ func (c *ClientConn) Close() error {
528
462
}
529
463
530
464
c .receiveMu .Lock ()
531
- if c .receiveSocket != nil {
532
- log .Debugf ("sending bye on receive socket" )
533
- err := c .receiveSocket .Close (
534
- websocket .StatusNormalClosure , "bye" ,
535
- )
536
- if err != nil {
537
- log .Errorf ("Error closing receive socket: %v" ,
538
- err )
539
-
540
- returnErr = err
541
- }
465
+ log .Debugf ("closing receive stream/socket" )
466
+ if err := c .transport .CloseReceive (); err != nil {
467
+ log .Errorf ("Error closing receive stream/socket: %v" , err )
468
+ returnErr = err
542
469
}
543
470
c .receiveMu .Unlock ()
544
471
545
472
c .sendMu .Lock ()
546
- if c .sendSocket != nil {
547
- log .Debugf ("sending bye on send socket" )
548
- err := c .sendSocket .Close (
549
- websocket .StatusNormalClosure , "bye" ,
550
- )
551
- if err != nil {
552
- log .Errorf ("Error closing send socket: %v" , err )
553
-
554
- returnErr = err
555
- }
473
+ log .Debugf ("closing send stream/socket" )
474
+ if err := c .transport .CloseSend (); err != nil {
475
+ log .Errorf ("Error closing send stream/socket: %v" , err )
476
+ returnErr = err
556
477
}
557
478
c .sendMu .Unlock ()
558
479
0 commit comments