// if an rpc comes in send to some/all servers
if an rpc comes in send to some/all servers
// if an rpc comes in send to some / all servers
func (twoway *Bilateral) rpcClientRequestHandler(item interface{}) error {
	request, ok := item.(*rpcClientRequestData)
	if !ok {
		return nil 
	}
	opCallOrCast := "CALL"
	if nil == request.done {
		opCallOrCast = "CAST"
	} else {
		twoway.rpcReturns[request.id] = request
	}
	buffer := bytes.Buffer{}
	enc := gob.NewEncoder(&buffer)
	err := enc.Encode(request.id)
	if err != nil {
		log.Errorf("id encode error: %v", err)
	}
	err = enc.Encode(request.method)
	if err != nil {
		log.Errorf("method encode error: %v", err)
	}
	err = enc.Encode(request.args)
	if err != nil {
		log.Errorf("args encode error: %v", err)
	}
	packet := buffer.Bytes()
	request.count = 0
	if nil == request.to {
		for to, c := range twoway.connections {
			switch c.state {
			case stateConnected, stateCheck:
				log.Debugf("RPC/%s →%q (%x)", opCallOrCast, to, packet)
				err := sendPacket(twoway.outgoingSocket, to, opCallOrCast, packet)
				if nil != err {
					log.Errorf("rpcClientRequestHandler send: error: %v", err)
				}
				request.count += 1
			case stateServing:
				log.Debugf("RPC/%s %q← (%x)", opCallOrCast, to, packet)
				err := sendPacket(twoway.listenSocket, to, opCallOrCast, packet)
				if nil != err {
					log.Errorf("rpcClientRequestHandler send: error: %v", err)
				}
				request.count += 1
			default:
			}
		}
	} else {
		for _, to := range request.to {
			c, ok := twoway.connections[to]
			if !ok {
				continue
			}
			switch c.state {
			case stateConnected, stateCheck:
				log.Debugf("RPC/%s →%q (%x)", opCallOrCast, to, packet)
				err := sendPacket(twoway.outgoingSocket, to, opCallOrCast, packet)
				if nil != err {
					log.Errorf("rpcClientRequestHandler send: error: %v", err)
				}
				request.count += 1
			case stateServing:
				log.Debugf("RPC/%s %q← (%x)", opCallOrCast, to, packet)
				err := sendPacket(twoway.listenSocket, to, opCallOrCast, packet)
				if nil != err {
					log.Errorf("rpcClientRequestHandler send: error: %v", err)
				}
				request.count += 1
			default:
			}
		}
	}
	if 0 == request.count || nil == request.done {
		log.Debugf("nothing was sent / no reply expected: count: %d", request.count)
		twoway.finishRequest(request)
		return nil
	}
	log.Debugf("make chan %d", request.count)
	request.reply = make(chan reflect.Value, request.count)
	twoway.insertEvent(request.id, request.timeout)
	return nil
}
func ( twoway * Bilateral ) rpcClientRequestHandler ( item interface { } ) error { request , ok := item . ( * rpcClientRequestData ) if ! ok { return nil } opCallOrCast := " " if nil == request . done { opCallOrCast = " " } else { twoway . rpcReturns [ request . id ] = request } buffer := bytes . Buffer { } enc := gob . NewEncoder ( & buffer ) err := enc . Encode ( request . id ) if err != nil { log . Errorf ( " " , err ) } err = enc . Encode ( request . method ) if err != nil { log . Errorf ( " " , err ) } err = enc . Encode ( request . args ) if err != nil { log . Errorf ( " " , err ) } packet := buffer . Bytes ( ) request . count = 0 if nil == request . to { for to , c := range twoway . connections { switch c . state { case stateConnected , stateCheck : log . Debugf ( " o CallOrCast,  t ,  p cket) err := sendPacket ( twoway . outgoingSocket , to , opCallOrCast , packet ) if nil != err { log . Errorf ( " " , err ) } request . count += 1 case stateServing : log . Debugf ( " o CallOrCast,  t ,  p cket) err := sendPacket ( twoway . listenSocket , to , opCallOrCast , packet ) if nil != err { log . Errorf ( " " , err ) } request . count += 1 default : } } } else { for _ , to := range request . to { c , ok := twoway . connections [ to ] if ! ok { continue } switch c . state { case stateConnected , stateCheck : log . Debugf ( " o CallOrCast,  t ,  p cket) err := sendPacket ( twoway . outgoingSocket , to , opCallOrCast , packet ) if nil != err { log . Errorf ( " " , err ) } request . count += 1 case stateServing : log . Debugf ( " o CallOrCast,  t ,  p cket) err := sendPacket ( twoway . listenSocket , to , opCallOrCast , packet ) if nil != err { log . Errorf ( " " , err ) } request . count += 1 default : } } } if 0 == request . count || nil == request . done { log . Debugf ( " " , request . count ) twoway . finishRequest ( request ) return nil } log . Debugf ( " " , request . count ) request . reply = make ( chan reflect . Value , request . count ) twoway . insertEvent ( request . id , request . timeout ) return nil }
if an rpc comes in send to some/all servers
if an rpc comes in send to some / all servers
// throw away invalid items // determine if CALL(return value expected) or CAST(no return value possible) // nothing sent so nothing expected, just finish the request // or if a CAST so no reply is expected // queue for processing