Пример #1
0
        /// <summary>
        /// Writes the serialized Python request to the named pipe. Waits from the request to be read on Python side.
        /// Then reads the response from python(json) an deserializes it to PythonRespnse
        /// </summary>
        /// <param name="request"></param>
        /// <param name="ct"></param>
        /// <returns></returns>
        public PythonResponse RequestAsync(PythonRequest request, CancellationToken ct)
        {
            using (CancellationTokenRegistration ctr = ct.Register(() => OnCancellationRequested()))
            {
                using (var streamWriter = new StreamWriter(ClientStream, _utf8Encoding, _defaultBufferSize,
                                                           leaveOpen: true)
                {
                    AutoFlush = true
                })
                {
                    Trace.TraceInformation("Sending information to Python.");
                    streamWriter.WriteLine(request.Serialize());
                }

                ct.ThrowIfCancellationRequested();
                bool isWindows = true;
#if NETCOREAPP
                if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    isWindows = false;
                }
#endif
                if (isWindows)
                {
                    ClientStream.WaitForPipeDrain();
                }

                using (var streamReader = new StreamReader(ClientStream, _utf8Encoding, false, _defaultBufferSize,
                                                           leaveOpen: true))
                {
                    return(PythonResponse.Deserialize(streamReader.ReadLine()));
                }
            }
        }