Files
IOT_SIM/runtime/Sim.Runner/ConsoleForwarder.cs
T
xuyueandClaude Opus 5.5 98be8331bc 第 0 阶段:教材项目 2 任务 1 在网页中原样运行
- runtime:仿真 WinForms 子集(Form、Button、Label、TextBox、ComboBox、
  GroupBox、Timer、MessageBox、界面线程消息循环、跨线程检查)、
  教材设备类库(DeviceCtrlHelper、LED 控件)、运行器(常驻编译服务 + 运行模式)
- server:Bun + Hono,WebSocket 会话,每次运行一个进程,消息转发
- web:Vue + Naive UI,WinForms 风格窗体渲染、MessageBox、实训台 LED 屏
- 端到端测试与 100 个程序并发的压力测试
- 教材逐任务摘要(docs/教材摘要)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 10:15:14 +08:00

45 lines
1018 B
C#

using System.IO;
using System.Text;
using Sim.Host;
namespace Sim.Runner;
/// <summary>把学生程序的 Console 输出按行转发给浏览器(标准输出本身留给协议消息使用)</summary>
internal sealed class ConsoleForwarder : TextWriter
{
private readonly StringBuilder _buffer = new();
public override Encoding Encoding => Encoding.UTF8;
public override void Write(char value)
{
lock (_buffer)
{
_buffer.Append(value);
if (value == '\n') FlushBuffer();
}
}
public override void Write(string? value)
{
if (value == null) return;
lock (_buffer)
{
_buffer.Append(value);
if (value.Contains('\n')) FlushBuffer();
}
}
public override void Flush()
{
lock (_buffer) FlushBuffer();
}
private void FlushBuffer()
{
if (_buffer.Length == 0) return;
SimHost.SendConsole(_buffer.ToString());
_buffer.Clear();
}
}