Last active
December 21, 2023 04:45
-
-
Save chrhlnd/c205be8f835a4ec7bcf95f96b2e81922 to your computer and use it in GitHub Desktop.
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
class Task | |
{ | |
public: | |
enum class Result | |
{ | |
More, | |
Error, | |
Done, | |
}; | |
}; | |
class HttpGet : public Task | |
{ | |
string m_url; | |
vector<Buffer> m_request; | |
vector<Buffer> m_response; | |
Socket m_socket; | |
bool m_error; | |
bool m_requestingResources; | |
public: | |
HttpGet(const string& url) | |
: m_url(url) | |
, m_error(false) | |
, m_requestingResources(false) | |
{} | |
Task::Result Run(TaskPool& pool) | |
{ | |
if (m_error) | |
{ | |
return Task::Error; | |
} | |
if ((m_request.empty() || !BuildRequest(m_request.last())) && !m_requestingResources) | |
{ | |
m_requestingResources = true; | |
pool.Add<BufferGet>() | |
.error( [&](const BufferGet& task) | |
{ | |
m_error = true; | |
}) | |
.success( [&](const BufferGet& task) | |
{ | |
m_request.push_back(task.BufferGet()) | |
}) | |
.finally( [&](const BufferGet& task) | |
{ | |
m_requestingResources = false; | |
}) | |
; | |
return Task::More; | |
} | |
if (!m_requestingResources && !m_socket) | |
{ | |
m_requestingResources = true; | |
pool.Add<SocketConnect>(ip(m_url), port(m_url)) | |
.error( [&](const SocketConnect& task) | |
{ | |
m_error = true; | |
}) | |
.success( [&](const SocketConnect& task) | |
{ | |
m_socket = task.GetSocket() | |
}) | |
.finally( [&](cosnt SocketConnect& task) | |
{ | |
m_requestingResources = false; | |
}); | |
; | |
} | |
// wip write request to socket | |
// wip read request from socket | |
// by getting a buffer if needed | |
// read and fill buffer | |
// see if we're done | |
// finally complete | |
return Task.More; | |
} | |
}; | |
int main(int argc, char* argv[]) | |
{ | |
TaskPool.Add<HttpGet>("www.cury.com") | |
.error( [](const HttpGet& task) | |
{ | |
}) | |
.success( [](const HttpGet& task) | |
{ | |
}) | |
.finally( [](const HttpGet& task) | |
{ | |
}) | |
; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment