1fd4e5da5Sopenharmony_ci// Copyright 2019 The Go Authors. 2fd4e5da5Sopenharmony_ci// 3fd4e5da5Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License"); 4fd4e5da5Sopenharmony_ci// you may not use this file except in compliance with the License. 5fd4e5da5Sopenharmony_ci// You may obtain a copy of the License at 6fd4e5da5Sopenharmony_ci// 7fd4e5da5Sopenharmony_ci// http://www.apache.org/licenses/LICENSE-2.0 8fd4e5da5Sopenharmony_ci// 9fd4e5da5Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software 10fd4e5da5Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS, 11fd4e5da5Sopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fd4e5da5Sopenharmony_ci// See the License for the specific language governing permissions and 13fd4e5da5Sopenharmony_ci// limitations under the License. 14fd4e5da5Sopenharmony_ci 15fd4e5da5Sopenharmony_cipackage jsonrpc2 16fd4e5da5Sopenharmony_ci 17fd4e5da5Sopenharmony_ciimport ( 18fd4e5da5Sopenharmony_ci "context" 19fd4e5da5Sopenharmony_ci) 20fd4e5da5Sopenharmony_ci 21fd4e5da5Sopenharmony_ci// Handler is the interface used to hook into the message handling of an rpc 22fd4e5da5Sopenharmony_ci// connection. 23fd4e5da5Sopenharmony_citype Handler interface { 24fd4e5da5Sopenharmony_ci // Deliver is invoked to handle incoming requests. 25fd4e5da5Sopenharmony_ci // If the request returns false from IsNotify then the Handler must eventually 26fd4e5da5Sopenharmony_ci // call Reply on the Conn with the supplied request. 27fd4e5da5Sopenharmony_ci // Handlers are called synchronously, they should pass the work off to a go 28fd4e5da5Sopenharmony_ci // routine if they are going to take a long time. 29fd4e5da5Sopenharmony_ci // If Deliver returns true all subsequent handlers will be invoked with 30fd4e5da5Sopenharmony_ci // delivered set to true, and should not attempt to deliver the message. 31fd4e5da5Sopenharmony_ci Deliver(ctx context.Context, r *Request, delivered bool) bool 32fd4e5da5Sopenharmony_ci 33fd4e5da5Sopenharmony_ci // Cancel is invoked for cancelled outgoing requests. 34fd4e5da5Sopenharmony_ci // It is okay to use the connection to send notifications, but the context will 35fd4e5da5Sopenharmony_ci // be in the cancelled state, so you must do it with the background context 36fd4e5da5Sopenharmony_ci // instead. 37fd4e5da5Sopenharmony_ci // If Cancel returns true all subsequent handlers will be invoked with 38fd4e5da5Sopenharmony_ci // cancelled set to true, and should not attempt to cancel the message. 39fd4e5da5Sopenharmony_ci Cancel(ctx context.Context, conn *Conn, id ID, cancelled bool) bool 40fd4e5da5Sopenharmony_ci 41fd4e5da5Sopenharmony_ci // Log is invoked for all messages flowing through a Conn. 42fd4e5da5Sopenharmony_ci // direction indicates if the message being received or sent 43fd4e5da5Sopenharmony_ci // id is the message id, if not set it was a notification 44fd4e5da5Sopenharmony_ci // elapsed is the time between a call being seen and the response, and is 45fd4e5da5Sopenharmony_ci // negative for anything that is not a response. 46fd4e5da5Sopenharmony_ci // method is the method name specified in the message 47fd4e5da5Sopenharmony_ci // payload is the parameters for a call or notification, and the result for a 48fd4e5da5Sopenharmony_ci // response 49fd4e5da5Sopenharmony_ci 50fd4e5da5Sopenharmony_ci // Request is called near the start of processing any request. 51fd4e5da5Sopenharmony_ci Request(ctx context.Context, conn *Conn, direction Direction, r *WireRequest) context.Context 52fd4e5da5Sopenharmony_ci // Response is called near the start of processing any response. 53fd4e5da5Sopenharmony_ci Response(ctx context.Context, conn *Conn, direction Direction, r *WireResponse) context.Context 54fd4e5da5Sopenharmony_ci // Done is called when any request is fully processed. 55fd4e5da5Sopenharmony_ci // For calls, this means the response has also been processed, for notifies 56fd4e5da5Sopenharmony_ci // this is as soon as the message has been written to the stream. 57fd4e5da5Sopenharmony_ci // If err is set, it implies the request failed. 58fd4e5da5Sopenharmony_ci Done(ctx context.Context, err error) 59fd4e5da5Sopenharmony_ci // Read is called with a count each time some data is read from the stream. 60fd4e5da5Sopenharmony_ci // The read calls are delayed until after the data has been interpreted so 61fd4e5da5Sopenharmony_ci // that it can be attributed to a request/response. 62fd4e5da5Sopenharmony_ci Read(ctx context.Context, bytes int64) context.Context 63fd4e5da5Sopenharmony_ci // Wrote is called each time some data is written to the stream. 64fd4e5da5Sopenharmony_ci Wrote(ctx context.Context, bytes int64) context.Context 65fd4e5da5Sopenharmony_ci // Error is called with errors that cannot be delivered through the normal 66fd4e5da5Sopenharmony_ci // mechanisms, for instance a failure to process a notify cannot be delivered 67fd4e5da5Sopenharmony_ci // back to the other party. 68fd4e5da5Sopenharmony_ci Error(ctx context.Context, err error) 69fd4e5da5Sopenharmony_ci} 70fd4e5da5Sopenharmony_ci 71fd4e5da5Sopenharmony_ci// Direction is used to indicate to a logger whether the logged message was being 72fd4e5da5Sopenharmony_ci// sent or received. 73fd4e5da5Sopenharmony_citype Direction bool 74fd4e5da5Sopenharmony_ci 75fd4e5da5Sopenharmony_ciconst ( 76fd4e5da5Sopenharmony_ci // Send indicates the message is outgoing. 77fd4e5da5Sopenharmony_ci Send = Direction(true) 78fd4e5da5Sopenharmony_ci // Receive indicates the message is incoming. 79fd4e5da5Sopenharmony_ci Receive = Direction(false) 80fd4e5da5Sopenharmony_ci) 81fd4e5da5Sopenharmony_ci 82fd4e5da5Sopenharmony_cifunc (d Direction) String() string { 83fd4e5da5Sopenharmony_ci switch d { 84fd4e5da5Sopenharmony_ci case Send: 85fd4e5da5Sopenharmony_ci return "send" 86fd4e5da5Sopenharmony_ci case Receive: 87fd4e5da5Sopenharmony_ci return "receive" 88fd4e5da5Sopenharmony_ci default: 89fd4e5da5Sopenharmony_ci panic("unreachable") 90fd4e5da5Sopenharmony_ci } 91fd4e5da5Sopenharmony_ci} 92fd4e5da5Sopenharmony_ci 93fd4e5da5Sopenharmony_citype EmptyHandler struct{} 94fd4e5da5Sopenharmony_ci 95fd4e5da5Sopenharmony_cifunc (EmptyHandler) Deliver(ctx context.Context, r *Request, delivered bool) bool { 96fd4e5da5Sopenharmony_ci return false 97fd4e5da5Sopenharmony_ci} 98fd4e5da5Sopenharmony_ci 99fd4e5da5Sopenharmony_cifunc (EmptyHandler) Cancel(ctx context.Context, conn *Conn, id ID, cancelled bool) bool { 100fd4e5da5Sopenharmony_ci return false 101fd4e5da5Sopenharmony_ci} 102fd4e5da5Sopenharmony_ci 103fd4e5da5Sopenharmony_cifunc (EmptyHandler) Request(ctx context.Context, conn *Conn, direction Direction, r *WireRequest) context.Context { 104fd4e5da5Sopenharmony_ci return ctx 105fd4e5da5Sopenharmony_ci} 106fd4e5da5Sopenharmony_ci 107fd4e5da5Sopenharmony_cifunc (EmptyHandler) Response(ctx context.Context, conn *Conn, direction Direction, r *WireResponse) context.Context { 108fd4e5da5Sopenharmony_ci return ctx 109fd4e5da5Sopenharmony_ci} 110fd4e5da5Sopenharmony_ci 111fd4e5da5Sopenharmony_cifunc (EmptyHandler) Done(ctx context.Context, err error) { 112fd4e5da5Sopenharmony_ci} 113fd4e5da5Sopenharmony_ci 114fd4e5da5Sopenharmony_cifunc (EmptyHandler) Read(ctx context.Context, bytes int64) context.Context { 115fd4e5da5Sopenharmony_ci return ctx 116fd4e5da5Sopenharmony_ci} 117fd4e5da5Sopenharmony_ci 118fd4e5da5Sopenharmony_cifunc (EmptyHandler) Wrote(ctx context.Context, bytes int64) context.Context { 119fd4e5da5Sopenharmony_ci return ctx 120fd4e5da5Sopenharmony_ci} 121fd4e5da5Sopenharmony_ci 122fd4e5da5Sopenharmony_cifunc (EmptyHandler) Error(ctx context.Context, err error) {} 123fd4e5da5Sopenharmony_ci 124fd4e5da5Sopenharmony_citype defaultHandler struct{ EmptyHandler } 125fd4e5da5Sopenharmony_ci 126fd4e5da5Sopenharmony_cifunc (defaultHandler) Deliver(ctx context.Context, r *Request, delivered bool) bool { 127fd4e5da5Sopenharmony_ci if delivered { 128fd4e5da5Sopenharmony_ci return false 129fd4e5da5Sopenharmony_ci } 130fd4e5da5Sopenharmony_ci if !r.IsNotify() { 131fd4e5da5Sopenharmony_ci r.Reply(ctx, nil, NewErrorf(CodeMethodNotFound, "method %q not found", r.Method)) 132fd4e5da5Sopenharmony_ci } 133fd4e5da5Sopenharmony_ci return true 134fd4e5da5Sopenharmony_ci} 135