-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
135 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include "Utils/IOStream/IOStream.h" | ||
|
||
#include "Utils/Time.hpp" | ||
|
||
MAA_NS_BEGIN | ||
|
||
std::string IOStream::read(duration_t timeout) | ||
{ | ||
return read_some(std::numeric_limits<size_t>::max(), timeout); | ||
} | ||
|
||
std::string IOStream::read_some(size_t count, duration_t timeout) | ||
{ | ||
auto start_time = std::chrono::steady_clock::now(); | ||
std::string result; | ||
|
||
while (is_open() && result.size() < count && duration_since(start_time) < timeout) { | ||
auto data = read_once(count - result.size()); | ||
result.append(data); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
std::string IOStream::read_until(std::string_view delimiter, duration_t timeout) | ||
{ | ||
auto start_time = std::chrono::steady_clock::now(); | ||
|
||
std::string result; | ||
|
||
while (!result.ends_with(delimiter)) { | ||
auto sub_timeout = timeout - duration_since<duration_t>(start_time); | ||
if (sub_timeout < duration_t::zero()) { | ||
break; | ||
} | ||
|
||
auto sub_str = read_some(1, sub_timeout); | ||
result.append(std::move(sub_str)); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
MAA_NS_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#pragma once | ||
|
||
#include <chrono> | ||
#include <string> | ||
#include <string_view> | ||
|
||
#include "MaaFramework/MaaPort.h" | ||
#include "Utils/NonCopyable.hpp" | ||
|
||
MAA_NS_BEGIN | ||
|
||
class MAA_UTILS_API IOStream : public NonCopyButMovable | ||
{ | ||
using duration_t = std::chrono::milliseconds; | ||
|
||
public: | ||
virtual ~IOStream() = default; | ||
|
||
public: | ||
virtual bool write(std::string_view data) = 0; | ||
|
||
virtual std::string read(duration_t timeout = duration_t::max()); | ||
virtual std::string read_some(size_t count, duration_t timeout = duration_t::max()); | ||
virtual std::string read_until(std::string_view delimiter, duration_t timeout = duration_t::max()); | ||
|
||
virtual bool release() = 0; | ||
virtual bool is_open() const = 0; | ||
|
||
protected: | ||
virtual std::string read_once(size_t max_count) = 0; | ||
}; | ||
|
||
MAA_NS_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters