-
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
7c80f7c
commit ad15c00
Showing
7 changed files
with
252 additions
and
25 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
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,31 @@ | ||
#ifndef _SOCKET_H_ | ||
#define _SOCKET_H_ | ||
|
||
#include <string> | ||
#include <thread> | ||
|
||
class SocketServer; | ||
|
||
class Socket | ||
{ | ||
public: | ||
explicit Socket(int id, SocketServer *server); | ||
virtual ~Socket(void); | ||
|
||
static const int BUFFER_SIZE = 1024; | ||
|
||
void disconnect(void); | ||
void sendString(const std::string &message); | ||
|
||
inline int getId(void) const { return m_socket; }; | ||
|
||
private: | ||
void receiving(void); | ||
|
||
SocketServer *m_server; | ||
int m_socket; | ||
|
||
std::thread m_receivingThread; | ||
}; | ||
|
||
#endif // _SOCKET_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,39 @@ | ||
#ifndef _SOCKET_SERVER_H_ | ||
#define _SOCKET_SERVER_H_ | ||
|
||
#include <thread> | ||
#include <functional> | ||
#include <list> | ||
#include <string> | ||
|
||
#include "socket.h" | ||
|
||
class SocketServer | ||
{ | ||
public: | ||
explicit SocketServer(void); | ||
virtual ~SocketServer(void); | ||
|
||
void start(int port); | ||
void messageReceived(Socket *socket, const std::string &message); | ||
void sendToAll(const std::string &message); | ||
|
||
inline void setOnMessageHandler(std::function<void(Socket *socket, const std::string message)> handler) | ||
{ m_onMessageHandler = handler; }; | ||
inline void setOnClientConnectHandler(std::function<void(Socket *socket)> handler) | ||
{ m_onClientConnectHandler = handler; }; | ||
|
||
private: | ||
void run(void); | ||
|
||
std::function<void(Socket *socket, const std::string &message)> m_onMessageHandler; | ||
std::function<void(Socket *socket)> m_onClientConnectHandler; | ||
|
||
int m_port; | ||
int m_socket; | ||
std::list<std::shared_ptr<Socket>> m_clients; | ||
|
||
std::thread m_connectionThread; | ||
}; | ||
|
||
#endif // _SOCKET_SERVER_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
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,51 @@ | ||
#include "socket.h" | ||
|
||
#include <string.h> | ||
#include <sys/types.h> | ||
#include <sys/socket.h> | ||
#include <netinet/in.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
|
||
#include "exception.h" | ||
#include "socketserver.h" | ||
|
||
Socket::Socket(int id, SocketServer *server) | ||
: m_server(server), | ||
m_socket(id) | ||
{ | ||
m_receivingThread = std::thread([this]() { | ||
receiving(); | ||
}); | ||
} | ||
|
||
Socket::~Socket(void) | ||
{ | ||
disconnect(); | ||
} | ||
|
||
void Socket::disconnect(void) | ||
{ | ||
close(m_socket); | ||
} | ||
|
||
void Socket::sendString(const std::string &message) | ||
{ | ||
if(message.size() > BUFFER_SIZE) | ||
throw Exception("Meesage too big"); | ||
|
||
if(send(m_socket, message.c_str(), BUFFER_SIZE, 0) < 0) | ||
throw Exception("Can't send message"); | ||
} | ||
|
||
void Socket::receiving(void) | ||
{ | ||
while(true) | ||
{ | ||
char buffer[BUFFER_SIZE]; | ||
if(recv(m_socket, buffer, BUFFER_SIZE, 0) < 0) | ||
break; | ||
|
||
m_server->messageReceived(this, std::string(buffer)); | ||
} | ||
} |
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,85 @@ | ||
#include "socketserver.h" | ||
|
||
#include <string.h> | ||
#include <sys/types.h> | ||
#include <sys/socket.h> | ||
#include <netinet/in.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
|
||
#include "exception.h" | ||
|
||
SocketServer::SocketServer(void) | ||
: m_onMessageHandler(nullptr), | ||
m_onClientConnectHandler(nullptr), | ||
m_port(0), | ||
m_socket(0) | ||
{ | ||
} | ||
|
||
SocketServer::~SocketServer(void) | ||
{ | ||
} | ||
|
||
void SocketServer::start(int port) | ||
{ | ||
m_port = port; | ||
|
||
m_socket = socket(AF_INET, SOCK_STREAM, 0); | ||
if(m_socket < 0) | ||
throw Exception("Can't create socket"); | ||
|
||
struct sockaddr_in serverAddr; | ||
serverAddr.sin_family = AF_INET; | ||
serverAddr.sin_addr.s_addr = htons(INADDR_ANY); | ||
serverAddr.sin_port = htons(port); | ||
|
||
if(bind(m_socket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0) | ||
throw Exception("Can't bind socket"); | ||
|
||
listen(m_socket, 1); | ||
|
||
m_connectionThread = std::thread([this]() { | ||
run(); | ||
}); | ||
} | ||
|
||
void SocketServer::run(void) | ||
{ | ||
while(true) | ||
{ | ||
int client = accept(m_socket, NULL, NULL); | ||
if(client < 0) | ||
continue; | ||
|
||
Socket *clientSocket = new Socket(client, this); | ||
m_clients.push_back(std::shared_ptr<Socket>(clientSocket)); | ||
if(m_onClientConnectHandler) | ||
m_onClientConnectHandler(clientSocket); | ||
} | ||
} | ||
|
||
void SocketServer::sendToAll(const std::string &message) | ||
{ | ||
for(auto client : m_clients) | ||
client->sendString(message); | ||
} | ||
|
||
void SocketServer::messageReceived(Socket *socket, const std::string &message) | ||
{ | ||
if(message == "close") | ||
{ | ||
for(std::list<std::shared_ptr<Socket> >::iterator i = m_clients.begin(); | ||
i != m_clients.end(); i++) | ||
{ | ||
if((*i)->getId() == socket->getId()) | ||
{ | ||
m_clients.erase(i); | ||
return; | ||
} | ||
} | ||
} | ||
|
||
if(m_onMessageHandler) | ||
m_onMessageHandler(socket, message); | ||
} |