package rpc import "sync" type LuxRpcServer struct { controllers map[string]LuxRpcController lock sync.Mutex } func NewLuxRpcServer() LuxRpcServer { return LuxRpcServer{ controllers: make(map[string]LuxRpcController, 0), } } func (rpc *LuxRpcServer) RegisterController(ctrl LuxRpcController) { rpc.controllers[ctrl.GetRpcName()] = ctrl } func (rpc *LuxRpcServer) HandleRequest(request LuxRpcRequest, rpcType LuxRpcType) (LuxRpcResponse, LuxRpcError, bool) { // lock rpc rpc.lock.Lock() defer rpc.lock.Unlock() // find controller ctrl, ok := rpc.controllers[request.Controller] if !ok { return LuxRpcResponse{}, LuxRpcError{ RequestID: request.RequestID, ErrorCode: 1, Message: "unknown controller", }, false } return ctrl.Handle(request, rpcType) }