【发布时间】:2017-04-14 20:17:04
【问题描述】:
我正在尝试转换程序(它是 vscode 和调试之间的桥梁) 该程序是用 C# 编写的。
它基于 o vscode-mono-debug
(https://github.com/Microsoft/vscode-mono-debug/blob/master/src/Protocol.cs)
嗯, 在 C# 中,我可以将标准输入读取为流:
byte[] buffer = new byte[BUFFER_SIZE];
Stream inputStream = Console.OpenStandardInput();
_rawData = new ByteBuffer();
while (!_stopRequested) {
var read = await inputStream.ReadAsync(buffer, 0, buffer.Length);
if (read == 0) {
// end of stream
break;
}
if (read > 0) {
_rawData.Append(buffer, read);
ProcessData();
}
}
我试试这个:
#define _WIN32_WINNT 0x05017
#define BUFFER_SIZE 4096
#include<iostream>
#include<thread>
#include <sstream>
using namespace std;
class ProtocolServer
{
private:
bool _stopRequested;
ostringstream _rawData;
public:
void Start()
{
char buffer[BUFFER_SIZE];
while (!cin.eof())
{
cin.getline(buffer,BUFFER_SIZE);
if (cin.fail())
{
//error
break;
}
else
{
_rawData << buffer;
}
}
}
};
int main()
{
ProtocolServer *server = new ProtocolServer();
server->Start();
return 0;
}
输入:
Content-Length: 261\r\n\r\n{\"command\":\"initialize\",\"arguments\":{\"clientID\":\"vscode\",\"adapterID\":\"advpl\",\"pathFormat\":\"path\",\"linesStartAt1\":true,\"columnsStartAt1\":true,\"supportsVariableType\":true,\"supportsVariablePaging\":true,\"supportsRunInTerminalRequest\":true},\"type\":\"request\",\"seq\":1}
这会正确读取前 2 行。由于协议没有把\n放在最后,所以在3交互中卡在cin.getline中。
切换到 read() 会导致它停留在 cin.read() 处,并且根本不读取任何内容。
我发现了一些类似的问题: StackOverFlow Question
还有例子: Posix_chat_client
但我不需要它一定是异步的,但它适用于 windows 和 linux。
对不起我的英语
谢谢!
【问题讨论】:
-
我尝试使用 boost asio 和 istream.read ,但两者都没有取得太大的成功。 还有.. 为什么没有成功?也许你的尝试有问题?请提供minimal reproducible example 说明您尝试了什么,并解释为什么您获得的功能不符合您的期望。
-
我不是 C# 专家,但如果我没记错的话,
await会阻塞直到异步操作完成。你应该可以使用std::cin.read(buffer, sizeof(buffer));,假设缓冲区是一个静态分配的数组。 -
@user4581301,我尝试使用 cin.read,但卡在了这一行。
-
现在我看到了你想要的东西,
cin要么太愚蠢,要么太聪明,这取决于你如何看待它,去做你想做的事。它总是会寻找下一个字节、下一个分隔符或您发送它的任何内容。我很抱歉让你走上了一条坏路。我认为您将不得不使用特定于操作系统(CreateFile、ReadConsoleInput 和 WaitSingleObject 并具有短暂的超时)或使用 Boost asio,我从未使用过这两种方法,但 Google 教授可能有一些有用的提示。 -
例如:stackoverflow.com/a/19964096/4581301 看来我错了一半
标签: c++ boost boost-asio