github.com/cmd-stream/base-go@v0.0.0-20230813145615-dd6ac24c16f5/delegate.go (about)

     1  package base
     2  
     3  import (
     4  	"context"
     5  	"net"
     6  	"time"
     7  )
     8  
     9  // ClientReconnectDelegate defines the Reconnect method.
    10  //
    11  // This delegate can be used if you want the client to reconnect to the server
    12  // in case of a connection loss.
    13  type ClientReconnectDelegate[T any] interface {
    14  	ClientDelegate[T]
    15  	Reconnect() error
    16  }
    17  
    18  // ClientDelegate helps the client to send commands and receive results.
    19  type ClientDelegate[T any] interface {
    20  	LocalAddr() net.Addr
    21  	RemoteAddr() net.Addr
    22  
    23  	SetSendDeadline(deadline time.Time) error
    24  	Send(seq Seq, cmd Cmd[T]) error
    25  	Flush() error
    26  
    27  	SetReceiveDeadline(deadline time.Time) error
    28  	Receive() (seq Seq, result Result, err error)
    29  
    30  	Close() error
    31  }
    32  
    33  // ServerDelegate helps the server handle incoming connections.
    34  //
    35  // Handle method should return the context.Canceled error if a context is done.
    36  type ServerDelegate interface {
    37  	Handle(ctx context.Context, conn net.Conn) error
    38  }