-
Notifications
You must be signed in to change notification settings - Fork 0
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
12 changed files
with
310 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,4 @@ cmake-build-debug/ | |
*.xml | ||
.idea/ | ||
*/.ipynb_checkpoints/ | ||
.vscode/ | ||
.vscode/ |
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
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 |
---|---|---|
@@ -1,12 +1,12 @@ | ||
CMAKE_PROGRESS_1 = 11 | ||
CMAKE_PROGRESS_2 = 12 | ||
CMAKE_PROGRESS_3 = 13 | ||
CMAKE_PROGRESS_4 = 14 | ||
CMAKE_PROGRESS_5 = 15 | ||
CMAKE_PROGRESS_6 = 16 | ||
CMAKE_PROGRESS_7 = 17 | ||
CMAKE_PROGRESS_8 = 18 | ||
CMAKE_PROGRESS_9 = 19 | ||
CMAKE_PROGRESS_10 = 20 | ||
CMAKE_PROGRESS_11 = 21 | ||
CMAKE_PROGRESS_1 = 15 | ||
CMAKE_PROGRESS_2 = 16 | ||
CMAKE_PROGRESS_3 = 17 | ||
CMAKE_PROGRESS_4 = 18 | ||
CMAKE_PROGRESS_5 = 19 | ||
CMAKE_PROGRESS_6 = 20 | ||
CMAKE_PROGRESS_7 = 21 | ||
CMAKE_PROGRESS_8 = 22 | ||
CMAKE_PROGRESS_9 = 23 | ||
CMAKE_PROGRESS_10 = 24 | ||
CMAKE_PROGRESS_11 = 25 | ||
|
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
CMAKE_PROGRESS_1 = 29 | ||
CMAKE_PROGRESS_2 = 30 | ||
CMAKE_PROGRESS_1 = 33 | ||
CMAKE_PROGRESS_2 = 34 | ||
|
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 |
---|---|---|
@@ -1 +1 @@ | ||
30 | ||
34 |
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,84 @@ | ||
|
||
#include <unistd.h> | ||
#include <cstring> | ||
#include <fstream> | ||
#include <iostream> | ||
#include <memory> | ||
#include "core/connection.h" | ||
#include "core/net_addr.h" | ||
#include "core/socket.h" | ||
#include "core/thread_pool.h" | ||
#include "http/http_utils.h" | ||
|
||
#define BUF_SIZE 2048 | ||
|
||
namespace Next { | ||
class File_Client { | ||
public: | ||
explicit File_Client(NetAddress &server_address) { | ||
auto client_socket = std::make_unique<Socket>(); | ||
client_socket->Connect(server_address); | ||
client_conn = std::make_unique<Connection>(std::move(client_socket)); | ||
std::cout << "File_Client: Connected with " << server_address.ToString() << std::endl; | ||
} | ||
void Begin() { | ||
Usage(); | ||
char buf[BUF_SIZE + 1]; | ||
memset(buf, 0, sizeof(buf)); | ||
int fd = client_conn->GetFd(); | ||
while (true) { | ||
// scan form user keyboard | ||
auto actual_read = read(STDIN_FILENO, buf, BUF_SIZE); | ||
if (strncmp(buf, "quit", strlen("quit")) == 0 || strncmp(buf, "q", strlen("q")) == 0) { | ||
// client indicates to exit | ||
exit(0); | ||
} | ||
auto tokens = Next::Http::Spilt(buf, "\n"); | ||
if (tokens.empty()) { | ||
std::cout << "input error, try again." << std::endl; | ||
continue; | ||
} | ||
std::string filename = tokens[0]; | ||
std::ifstream file(filename, std::ios::binary | std::ios::ate); | ||
if (!file) { | ||
std::cout << "can't open file, try again." << std::endl; | ||
continue; | ||
} | ||
int file_size = file.tellg(); | ||
file.seekg(0, std::ios::beg); | ||
|
||
int send_size = 0; | ||
// std::cout << "file :" << filename << ", size : " << file_size << std::endl; | ||
while (send_size < file_size) { | ||
memset(buf, 0, sizeof(buf)); | ||
int bufferSize = (file_size - send_size) < BUFSIZ + 1 ? (file_size - send_size) : BUFSIZ + 1; | ||
file.read(buf, bufferSize); | ||
send(fd, buf, bufferSize, 0); | ||
send_size += bufferSize; | ||
} | ||
memset(buf, 0, sizeof(buf)); | ||
// blocking wait for server's response | ||
auto actual_recv = recv(fd, buf, BUF_SIZE, 0); | ||
write(STDOUT_FILENO, buf, actual_recv); | ||
// scan next user input | ||
memset(buf, 0, sizeof(buf)); | ||
} | ||
} | ||
void Usage() { | ||
std::cout << "Usage:\n" | ||
<< "\tfilename\n" | ||
<< "\tquit(q)\n\n"; | ||
} | ||
|
||
private: | ||
std::unique_ptr<Connection> client_conn; | ||
}; | ||
|
||
} // namespace Next | ||
|
||
int main(int argc, char *argv[]) { | ||
Next::NetAddress server_addr{"0.0.0.0", 10080}; | ||
Next::File_Client dict_client{server_addr}; | ||
dict_client.Begin(); | ||
return 0; | ||
} |
Oops, something went wrong.