async
This commit is contained in:
@@ -90,7 +90,7 @@ void Ollama::run() {
|
|||||||
//curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L);
|
//curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L);
|
||||||
curl_easy_setopt(curl, CURLOPT_POST, 1L);
|
curl_easy_setopt(curl, CURLOPT_POST, 1L);
|
||||||
curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, sendObj.dump().c_str());
|
curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, sendObj.dump().c_str());
|
||||||
result = std::async(curl_easy_perform, curl);
|
result = std::async(std::launch::async, curl_easy_perform, curl);
|
||||||
inFlight = true;
|
inFlight = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -120,26 +120,37 @@ void Terminal::onLoad() {
|
|||||||
|
|
||||||
void Terminal::run() {
|
void Terminal::run() {
|
||||||
|
|
||||||
static char input[150];
|
static char input[150] = "";
|
||||||
static char output[150];
|
static std::vector<char> output(150);
|
||||||
|
static std::string op = "";
|
||||||
static int returnCode;
|
static int returnCode;
|
||||||
static timeval timeout; timeout = { 0, 0 };
|
static std::future<int> rc;
|
||||||
fd_set fd_in;
|
static fd_set fd_in;
|
||||||
|
|
||||||
// Wait for data from standard input and master side of PTY
|
// Wait for data from standard input and master side of PTY
|
||||||
FD_ZERO(&fd_in);
|
FD_ZERO(&fd_in);
|
||||||
FD_SET(master, &fd_in);
|
FD_SET(master, &fd_in);
|
||||||
|
|
||||||
returnCode = select(master + 1, &fd_in, NULL, NULL, &timeout);
|
//returnCode = select(master + 1, &fd_in, NULL, NULL, NULL);
|
||||||
switch(returnCode) {
|
rc = std::async(std::launch::async, [this](fd_set* f) -> int {
|
||||||
|
return select(master + 1, f, NULL, NULL, NULL);
|
||||||
|
}, &fd_in);
|
||||||
|
|
||||||
|
if(rc.wait_for(std::chrono::seconds(0)) == std::future_status::ready) {
|
||||||
|
switch(rc.get()) {
|
||||||
case -1: fprintf(stderr, "Error %d on select()\n", errno);
|
case -1: fprintf(stderr, "Error %d on select()\n", errno);
|
||||||
exit(1);
|
exit(1);
|
||||||
|
|
||||||
default: {
|
default: {
|
||||||
// If data on master side of PTY
|
// If data on master side of PTY
|
||||||
if (FD_ISSET(master, &fd_in)) {
|
if(FD_ISSET(master, &fd_in)) {
|
||||||
returnCode = read(master, output, sizeof(output));
|
returnCode = read(master, output.data(), 150);
|
||||||
if (returnCode > 0) {
|
if (returnCode > 0) {
|
||||||
|
write(1, output.data(), returnCode);
|
||||||
|
op += "\n";
|
||||||
|
op += output.data();
|
||||||
|
op += "\n";
|
||||||
|
output.clear();
|
||||||
} else {
|
} else {
|
||||||
if (returnCode < 0) {
|
if (returnCode < 0) {
|
||||||
fprintf(stderr, "Error %d on read master PTY\n", errno);
|
fprintf(stderr, "Error %d on read master PTY\n", errno);
|
||||||
@@ -149,16 +160,18 @@ void Terminal::run() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} // End switch
|
} // End switch
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
ImGui::Begin("Terminal Module");
|
ImGui::Begin("Terminal Module");
|
||||||
|
|
||||||
ImGui::Text("%s", output);
|
ImGui::TextWrapped("%s", op.c_str());
|
||||||
ImGui::SameLine();
|
|
||||||
ImGui::InputText("input", input, 150);
|
ImGui::InputText("input", input, 150);
|
||||||
|
|
||||||
if(ImGui::Button("send"))
|
if(ImGui::Button("send")) {
|
||||||
write(master, input, returnCode);
|
static std::string s; s = input; s += "\n";
|
||||||
|
write(master, s.c_str(), s.length());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
ImGui::End();
|
ImGui::End();
|
||||||
|
|||||||
Reference in New Issue
Block a user