Class GrpcServer
java.lang.Object
com.codename1.backend.grpc.GrpcServer
- All Implemented Interfaces:
HttpServer.Handler
Serves gRPC: native gRPC over HTTP/2, and gRPC-Web over HTTP/1.1 or HTTP/2
for browsers and for the Codename One client's GrpcWeb.
The build creates one from the module's @GrpcService beans and the HTTP
server routes every application/grpc* request on an HTTP/2 stream to it.
It is also an ordinary HttpServer.Handler, which is how gRPC-Web reaches it
over HTTP/1.1.
Rules it keeps, each of which a real client depends on:
- The answer is always HTTP 200 with the outcome in
grpc-status; a call that fails before its first message is a "trailers-only" response. - An unknown method is
UNIMPLEMENTED, agrpc-encodingother thanidentityorgzipisUNIMPLEMENTEDwith the accepted encodings named. - A unary or server streaming call that sends no request, or more than one,
is
INTERNAL, as in grpc-java. - The
grpc-timeoutdeadline is checked before the handler runs, before each response message and after the handler returns. A handler blocked in its own code is not interrupted;GrpcContext.isDeadlineExceeded()lets it stop. - An exception that is not a
GrpcExceptionis logged here and reported asUNKNOWNwith no message, so an internal error text never reaches a peer.
Not supported: application/grpc-web-text (base64 framing), and server
reflection. Calls on one HTTP/2 connection are served one at a time, in the
connection's turn, as the server serves every HTTP/2 request; a client that
wants concurrent streaming calls opens more connections.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionadd(GrpcMethod method) Adds a method.handle(HttpServer.Request request) Answers gRPC-Web requests arriving as ordinary HTTP requests; anything else is left to the next handler.booleanstatic booleanWhether a content type is one this server answers.voidServes one call over a transport.setMaxMessageSize(int bytes) The largest request message accepted, compressed or not.
-
Constructor Details
-
GrpcServer
public GrpcServer()
-
-
Method Details
-
add
Adds a method. A second method on the same path replaces the first.- Parameters:
method- the method- Returns:
- this
-
setMaxMessageSize
The largest request message accepted, compressed or not.- Parameters:
bytes- the limit; 4 MB by default- Returns:
- this
-
has
- Parameters:
path-/package.Service/Method- Returns:
- whether this server has the method
-
isGrpc
Whether a content type is one this server answers.- Parameters:
contentType- the request's content type, may be null- Returns:
- true for
application/grpcand its variants
-
handle
Answers gRPC-Web requests arriving as ordinary HTTP requests; anything else is left to the next handler.- Specified by:
handlein interfaceHttpServer.Handler- Throws:
Exception
-
serve
Serves one call over a transport. Never throws: every outcome becomes a gRPC status, and a transport that fails ends the call silently.- Parameters:
transport- the HTTP sidetarget- the request target,/package.Service/Methodheaders- the request headers, lower-case names
-