forked from yuesong-feng/30dayMakeCppServer
-
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
1 parent
1add0b4
commit 9710356
Showing
24 changed files
with
303 additions
and
59 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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
build/ | ||
*__pycache__/ | ||
.vscode/ | ||
.vscode/ | ||
.cache/ | ||
.DS_store |
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
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,46 @@ | ||
/** | ||
* @file Exception.h | ||
* @author 冯岳松 ([email protected]) | ||
* @brief | ||
* @version 0.1 | ||
* @date 2022-02-07 | ||
* | ||
* @copyright Copyright (冯岳松) 2022 | ||
* | ||
*/ | ||
#pragma once | ||
#include <iostream> | ||
#include <stdexcept> | ||
#include <string> | ||
|
||
enum ExceptionType { | ||
INVALID = 0, | ||
INVALID_SOCKET = 1, | ||
}; | ||
|
||
class Exception : public std::runtime_error { | ||
public: | ||
explicit Exception(const std::string &message) : std::runtime_error(message), type_(ExceptionType::INVALID) { | ||
std::string exception_message = "Message :: " + message + "\n"; | ||
std::cerr << exception_message; | ||
} | ||
|
||
Exception(ExceptionType type, const std::string &message) : std::runtime_error(message), type_(type) { | ||
std::string exception_message = | ||
"Exception Type :: " + ExceptionTypeToString(type_) + "\nMessage :: " + message + "\n"; | ||
std::cerr << exception_message; | ||
} | ||
static std::string ExceptionTypeToString(ExceptionType type) { | ||
switch (type) { | ||
case ExceptionType::INVALID: | ||
return "Invalid"; | ||
case ExceptionType::INVALID_SOCKET: | ||
return "Invalid socket"; | ||
default: | ||
return "Unknoen"; | ||
} | ||
} | ||
|
||
private: | ||
ExceptionType type_; | ||
}; |
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,28 @@ | ||
/** | ||
* @file Log.h | ||
* @author 冯岳松 ([email protected]) | ||
* @brief | ||
* @version 0.1 | ||
* @date 2022-02-07 | ||
* | ||
* @copyright Copyright (冯岳松) 2022 | ||
* | ||
*/ | ||
#pragma once | ||
class Log | ||
{ | ||
private: | ||
/* data */ | ||
public: | ||
Log(/* args */); | ||
~Log(); | ||
}; | ||
|
||
Log::Log(/* args */) | ||
{ | ||
} | ||
|
||
Log::~Log() | ||
{ | ||
} | ||
|
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,26 @@ | ||
/** | ||
* @file Signal.h | ||
* @author 冯岳松 ([email protected]) | ||
* @brief | ||
* @version 0.1 | ||
* @date 2022-02-07 | ||
* | ||
* @copyright Copyright (冯岳松) 2022 | ||
* | ||
*/ | ||
#pragma once | ||
#include <signal.h> | ||
#include <functional> | ||
#include <map> | ||
|
||
std::map<int, std::function<void()>> handlers_; | ||
void signal_handler(int sig) { | ||
handlers_[sig](); | ||
} | ||
|
||
struct Signal { | ||
static void signal(int sig, const std::function<void()> &handler) { | ||
handlers_[sig] = handler; | ||
::signal(sig, signal_handler); | ||
} | ||
}; |
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,6 @@ | ||
#include "Server.h" | ||
#include "Buffer.h" | ||
#include "Connection.h" | ||
#include "EventLoop.h" | ||
#include "Socket.h" | ||
#include "SignalHandler.h" |
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,24 @@ | ||
#include <Connection.h> | ||
#include <Socket.h> | ||
#include <iostream> | ||
|
||
int main() { | ||
Socket *sock = new Socket(); | ||
sock->Connect("127.0.0.1", 1234); | ||
|
||
Connection *conn = new Connection(nullptr, sock); | ||
while(true){ | ||
conn->Read(); | ||
std::cout << "Message from server: " << conn->ReadBuffer() << std::endl; | ||
} | ||
// conn->Read(); | ||
|
||
// if (conn->GetState() == Connection::State::Connected) { | ||
// std::cout << conn->ReadBuffer() << std::endl; | ||
//} | ||
//conn->SetSendBuffer("Hello server!"); | ||
//conn->Write(); | ||
|
||
delete conn; | ||
return 0; | ||
} |
Oops, something went wrong.