private async Task RunWorker(CancellationToken ct) { TaskUtilities.AssertIsOnBackgroundThread(); try { var message = await ReceiveMessageAsync(ct); if (message.Name != "Microsoft.R.Host" || message.RequestId != null) { throw ProtocolError($"Microsoft.R.Host handshake expected:", message); } var protocolVersion = message.GetInt32(0, "protocol_version"); if (protocolVersion != 1) { throw ProtocolError($"Unsupported RHost protocol version:", message); } var rVersion = message.GetString(1, "R_version"); await _callbacks.Connected(rVersion); message = await RunLoop(ct, allowEval : true); if (message != null) { throw ProtocolError($"Unexpected host response message:", message); } } finally { await _callbacks.Disconnected(); } }
private async Task RunWorker(CancellationToken ct) { TaskUtilities.AssertIsOnBackgroundThread(); // Spin until the worker task is registered. while (_runTask == null) { await Task.Yield(); } try { var message = await ReceiveMessageAsync(ct); if (message == null) { // Socket is closed before connection is established. Just exit. return; } if (!message.IsNotification || message.Name != "!Microsoft.R.Host") { throw ProtocolError($"Microsoft.R.Host handshake expected:", message); } var protocolVersion = message.GetInt32(0, "protocol_version"); if (protocolVersion != 1) { throw ProtocolError($"Unsupported RHost protocol version:", message); } var rVersion = message.GetString(1, "R_version"); await _callbacks.Connected(rVersion); message = await RunLoop(ct); if (message != null) { throw ProtocolError($"Unexpected host response message:", message); } } finally { // Signal cancellation to any callbacks that haven't returned yet. _cts.Cancel(); await _callbacks.Disconnected(); } }
private async Task RunWorker(CancellationToken ct) { TaskUtilities.AssertIsOnBackgroundThread(); // Spin until the worker task is registered. while (_runTask == null) { await Task.Yield(); } // Create cancellation tasks before proceeding with anything else, to avoid race conditions in usage of those tasks. _cancelEvaluationAfterRunTask = _runTask.ContinueWith(t => RhostDisconnectedEvaluationResult).Unwrap(); _cancelCreateBlobAfterRunTask = _runTask.ContinueWith(t => RhostDisconnectedULongResult).Unwrap(); _cancelBlobReadAfterRunTask = _runTask.ContinueWith(t => RhostDisconnectedByteArrayResult).Unwrap(); _cancelBlobWriteOrSizeAfterRunTask = _runTask.ContinueWith(t => RhostDisconnectedLongResult).Unwrap(); try { var message = await ReceiveMessageAsync(ct); if (message == null || !message.IsNotification || message.Name != "!Microsoft.R.Host") { throw ProtocolError($"Microsoft.R.Host handshake expected:", message); } var protocolVersion = message.GetInt32(0, "protocol_version"); if (protocolVersion != 1) { throw ProtocolError($"Unsupported RHost protocol version:", message); } var rVersion = message.GetString(1, "R_version"); await _callbacks.Connected(rVersion); message = await RunLoop(ct); if (message != null) { throw ProtocolError($"Unexpected host response message:", message); } } finally { // Signal cancellation to any callbacks that haven't returned yet. _cts.Cancel(); await _callbacks.Disconnected(); } }