-
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.
feat: add class Accpeptor for encupsulation of newconnection function
- 寫一個新類去裝了建立新客戶端連結的流程,練習了 bind 與 function類別的用法 - 有發現 include header file的順序會造成unique_ptr的編譯錯誤,目前還不確定原因 - modify make file
- Loading branch information
1 parent
bf9c6f4
commit 053f6b1
Showing
13 changed files
with
220 additions
and
89 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,4 +1,4 @@ | ||
server: | ||
@echo "Starting server..." | ||
g++ server.cpp src/utils.cpp src/Socket.cpp src/Epoll.cpp src/Event.cpp src/EventLoop.cpp --std c++23 -o output/server | ||
g++ src/utils.cpp src/Socket.cpp src/Epoll.cpp src/Event.cpp src/EventLoop.cpp src/Acceptor.cpp server.cpp --std c++23 -o output/server | ||
g++ client.cpp src/utils.cpp src/Socket.cpp -o output/client |
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,49 +1,37 @@ | ||
#include<iostream> | ||
#include<sys/socket.h> | ||
#include<netinet/in.h> | ||
#include<arpa/inet.h> | ||
#include<string.h> | ||
#include<unistd.h> | ||
#include<assert.h> | ||
#include<errno.h> | ||
#include<sys/epoll.h> | ||
#include<fcntl.h> | ||
#include<sys/types.h> | ||
#include<unistd.h> | ||
#include<memory> | ||
#include<string> | ||
#include<functional> | ||
|
||
#include "src/utils.h" | ||
#include "src/Socket.h" | ||
#include "src/Epoll.h" | ||
#include "src/EventLoop.h" | ||
#include "src/Event.h" | ||
#include "src/Acceptor.h" | ||
using namespace std; | ||
|
||
|
||
const char * addr = "0.0.0.0"; | ||
string addr = "0.0.0.0"; | ||
uint16_t port = 8888; | ||
|
||
void test(){ | ||
auto i = inet_addr(Domain::IPv4, addr.c_str(), port); | ||
} | ||
|
||
int main(){ | ||
cout << __cplusplus << endl; | ||
Socket *server = new Socket(Domain::IPv4, Type::TCP); | ||
auto serv_addr = inet_addr(Domain::IPv4, addr, port); | ||
server->bind(*serv_addr.get()); | ||
server->listen(); | ||
|
||
Epoll *epoll = new Epoll(5); | ||
// auto ev = gen_epoll_event(server->get_fd(), EPOLLIN | EPOLLET); | ||
Event *event = new Event(epoll, server->get_fd(), EPOLLIN | EPOLLET); | ||
auto ev = event->gen_epoll_event(); | ||
auto cb = bind(accept_connection_handler, server, epoll); | ||
event->set_callback(cb); | ||
epoll->ctl_add(server->get_fd(), ev.get()); | ||
EventLoop * loop = new EventLoop(epoll); | ||
Acceptor *acceptor = new Acceptor(loop, addr, port); | ||
|
||
EventLoop * loop = new EventLoop(epoll, server); | ||
auto cb = bind(accept_connection_handler, placeholders::_1, epoll); | ||
acceptor->set_connection_handler(cb); | ||
|
||
loop->run(); | ||
|
||
delete event; | ||
delete server; | ||
|
||
delete acceptor; | ||
delete epoll; | ||
delete loop; | ||
return 0; | ||
} | ||
} |
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,82 @@ | ||
#include "Acceptor.h" | ||
|
||
#include <string> | ||
#include <functional> | ||
#include <memory> | ||
|
||
using namespace std; | ||
|
||
|
||
void et_read_event_handler(int read_fd){ | ||
while(true){ | ||
char buff[1024]; | ||
memset(buff, 0, sizeof(buff)); | ||
ssize_t read_bytes = recv(read_fd, buff, sizeof(buff), 0); | ||
// printf("read_bytes: %ld\n", read_bytes); | ||
if (read_bytes > 0){ | ||
cout <<"get message from client : "<< buff <<";"<<endl; | ||
send(read_fd, buff, sizeof(buff), 0); | ||
} else if (read_bytes == -1 && errno == EINTR){ | ||
// client normal interrupt; | ||
cout <<"interrupted "<< buff <<endl; | ||
continue; | ||
} else if (read_bytes == -1 &&(errno == EAGAIN || errno == EWOULDBLOCK)){ | ||
// data is all recived | ||
cout <<"message end! "<< buff <<endl; | ||
break; | ||
} else if (read_bytes == 0){ | ||
cout << "End of file" << endl; | ||
close(read_fd); | ||
break; | ||
} | ||
} | ||
} | ||
void accept_connection_handler(Socket* socket, Epoll *epoll){ | ||
cout << "new client connected" << endl; | ||
struct sockaddr_in clit_addr; | ||
socklen_t clit_sock_len = sizeof(clit_addr); | ||
memset(&clit_addr, 0, clit_sock_len); | ||
|
||
int clit_socket_fd = socket->accept(clit_addr); | ||
set_non_blocking(clit_socket_fd); | ||
|
||
// auto ev_c = gen_epoll_event(clit_socket_fd, EPOLLIN | EPOLLET); | ||
Event* e = new Event(epoll, clit_socket_fd, EPOLLIN | EPOLLET); // 有內存洩漏風險 | ||
auto ev_c = e->gen_epoll_event(); | ||
auto cb = bind(et_read_event_handler, clit_socket_fd); | ||
e->set_callback(cb); | ||
epoll->ctl_add(clit_socket_fd, ev_c.get()); | ||
cout << "add client" << endl; | ||
} | ||
|
||
Acceptor::Acceptor(EventLoop* ev_lp, string& addr, u_int16_t port, Domain domain, Type type): _loop(ev_lp) | ||
{ | ||
cout << "Acceptor created" << endl; | ||
_sock = new Socket(domain, type); | ||
auto serv_addr = inet_addr(domain, addr.c_str(), port); | ||
_sock->bind(*serv_addr.get()); | ||
_sock->listen(); | ||
|
||
_event = new Event(_sock->get_fd(), EPOLLIN | EPOLLET); | ||
_loop->add_event(*_event); | ||
auto cb = bind(&Acceptor::new_connection, this); | ||
_event->set_callback(cb); | ||
|
||
} | ||
Acceptor::~Acceptor() | ||
{ | ||
delete _sock; | ||
delete _event; | ||
::close(_sock->get_fd()); | ||
cout << "Acceptor destroyed" << endl; | ||
} | ||
|
||
void Acceptor::set_connection_handler(function<void (Socket*)> connection_handler) | ||
{ | ||
new_connection_callback = connection_handler; | ||
} | ||
|
||
void Acceptor::new_connection() | ||
{ | ||
new_connection_callback(_sock); | ||
} |
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 <iostream> | ||
#include <string> | ||
#include <functional> | ||
#include <memory> | ||
|
||
|
||
#include "utils.h" | ||
#include "Socket.h" // 不放在 utils.h 後面會有編譯錯誤error: ‘unique_ptr’ does not name a type,原因不明 | ||
#include "Event.h" | ||
#include "EventLoop.h" | ||
|
||
|
||
using namespace std; | ||
|
||
class Acceptor | ||
{ | ||
Socket* _sock; | ||
Event* _event; // connection accpet event | ||
EventLoop* _loop; | ||
function<void (Socket*)> new_connection_callback; | ||
|
||
public: | ||
Acceptor(EventLoop* ev_lp, string& addr, u_int16_t port, Domain domain=Domain::IPv4, Type type = Type::TCP); | ||
~Acceptor(); | ||
void set_connection_handler(function<void (Socket*)> connection_handler); | ||
void new_connection(); | ||
}; | ||
|
||
|
||
void et_read_event_handler(int read_fd); | ||
void accept_connection_handler(Socket* socket, Epoll* epoll); | ||
|
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
Oops, something went wrong.