github.com/geniusesgroup/libgo@v0.0.0-20220713101832-828057a9d3d4/srpc/handler.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package srpc
     4  
     5  import (
     6  	"../protocol"
     7  )
     8  
     9  // Read more about this protocol : https://github.com/GeniusesGroup/RFCs/blob/master/sRPC.md
    10  type SRPCHandler struct{}
    11  
    12  // HandleIncomeRequest handle incoming sRPC request streams that carry on Syllab codec!
    13  func (srpc *SRPCHandler) HandleIncomeRequest(stream protocol.Stream) (err protocol.Error) {
    14  	var service protocol.Service
    15  	service, err = stream.Service()
    16  	if err != nil {
    17  		stream.SetError(err)
    18  		return
    19  	}
    20  
    21  	// call request service
    22  	err = service.ServeSRPC(stream)
    23  	if err != nil {
    24  		stream.SetError(err)
    25  	}
    26  
    27  	stream.SendResponse()
    28  	stream.Close()
    29  	return
    30  }
    31  
    32  // HandleOutcomeRequest use to handle outcoming sRPC request.
    33  // It block caller until get response or error.
    34  func HandleOutcomeRequest(conn protocol.Connection, service protocol.Service, payload protocol.Codec) (stream protocol.Stream, err protocol.Error) {
    35  	stream, err = conn.OutcomeStream(service)
    36  	if err != nil {
    37  		return
    38  	}
    39  
    40  	// stream.SetOutcomeData(syllab.NewCodec(payload))
    41  	stream.SetOutcomeData(payload)
    42  
    43  	err = stream.SendRequest()
    44  	if err != nil {
    45  		return
    46  	}
    47  
    48  	err = stream.Error()
    49  	return
    50  }