tractor.dev/toolkit-go@v0.0.0-20241010005851-214d91207d07/duplex/interop/callbacks.go (about)

     1  package interop
     2  
     3  import (
     4  	"io"
     5  	"log"
     6  
     7  	"tractor.dev/toolkit-go/duplex/rpc"
     8  )
     9  
    10  type CallbackService struct{}
    11  
    12  func (s CallbackService) UnaryCallback(resp rpc.Responder, call *rpc.Call) {
    13  	var params any
    14  	if err := call.Receive(&params); err != nil {
    15  		log.Println(err)
    16  		return
    17  	}
    18  	if err := resp.Return(params); err != nil {
    19  		log.Println(err)
    20  	}
    21  }
    22  
    23  func (s CallbackService) StreamCallback(resp rpc.Responder, call *rpc.Call) {
    24  	var v any
    25  	if err := call.Receive(&v); err != nil {
    26  		log.Println(err)
    27  		return
    28  	}
    29  	ch, err := resp.Continue(v)
    30  	if err != nil {
    31  		log.Println(err)
    32  		return
    33  	}
    34  	defer ch.Close()
    35  	for err == nil {
    36  		err = call.Receive(&v)
    37  		if err == nil {
    38  			err = resp.Send(v)
    39  		}
    40  	}
    41  	if err != nil && err != io.EOF {
    42  		log.Println(err)
    43  	}
    44  }
    45  
    46  func (s CallbackService) BytesCallback(resp rpc.Responder, call *rpc.Call) {
    47  	var params any
    48  	if err := call.Receive(&params); err != nil {
    49  		log.Println(err)
    50  		return
    51  	}
    52  	ch, err := resp.Continue(params)
    53  	if err != nil {
    54  		log.Println(err)
    55  		return
    56  	}
    57  	defer ch.Close()
    58  	if _, err := io.Copy(ch, call); err != nil {
    59  		log.Println(err)
    60  	}
    61  }