/// <summary> /// Initializes a new instance of the <see cref="GRPCServer"/> class. /// </summary> /// <param name="host">The <see cref="IPEndPoint"/></param> /// <param name="chunkSender">The chunkSender<see cref="IChunkSender{Command, FetchRequest, GrpcResponse}"/></param> /// <param name="chunkReceiver">The chunkReceiver<see cref="IChunkReceiver{Command}"/></param> internal GRPCServer(IPEndPoint host, IChunkSender <Command, FetchRequest, GrpcResponse> chunkSender, IChunkReceiver <Command> chunkReceiver) { this.Host = host; this.chunkSender = chunkSender; this.chunkReceiver = chunkReceiver; this.chunkReceiver.FileReceived = async(cmd, streamId, token, header) => { // This creates a dummy stream only to receive the File and also because the main stream already exists in the subscription handler // The stream will be closed as soon as the file is being successfully received or a timeout happend IStream <Command> stream = new GrpcStream(null, null, streamId); (stream as GrpcStream).Connected = true; await this.OnCommand?.Invoke(cmd, streamId, token, header); }; }
/// <inheritdoc /> public override async Task OpenStream(IAsyncStreamReader <Command> requestStream, IServerStreamWriter <Command> responseStream, ServerCallContext context) { try { while (await requestStream.MoveNext()) { this.Log(this.ToString(), "Received streaming package from client", LogLevel.Trace); if (!context.RequestHeaders.Any(p => p.Key == NetworkHeader.TokenHeader) || !context.RequestHeaders.Any(p => p.Key == NetworkHeader.StreamIdHeader)) { throw new Exception("Missing header information"); } string streamId = context.RequestHeaders.FirstOrDefault(p => p.Key == NetworkHeader.StreamIdHeader).Value; string token = context.RequestHeaders.FirstOrDefault(p => p.Key == NetworkHeader.TokenHeader).Value; try { if (requestStream.Current.Type == CommandType.File) { this.chunkReceiver.ChunkReceived(requestStream.Current, streamId, token, context.RequestHeaders); continue; } IStream <Command> stream = new GrpcStream(responseStream, requestStream, streamId); (stream as GrpcStream).Connected = true; await this.OnStreaming?.Invoke(requestStream.Current, stream, token, context.RequestHeaders); } catch (Exception ex) { this.Log("Grpc stream error", ex); } } } catch { } this.OnClientDisconnected?.Invoke(context.RequestHeaders.FirstOrDefault(p => p.Key == NetworkHeader.StreamIdHeader).Value); this.Log(this.ToString(), "Client connection closed", LogLevel.Info); }