can accept multiple connection / server can send data back to client

This commit is contained in:
Benjamin Sergeant 2018-12-30 21:16:05 -08:00
parent 0ee71e9a09
commit 266cf93584
3 changed files with 33 additions and 14 deletions

View File

@ -30,10 +30,8 @@ namespace ix
std::pair<bool, std::string> WebSocketServer::run() std::pair<bool, std::string> WebSocketServer::run()
{ {
// https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.3.0/com.ibm.zos.v2r3.hala001/server.htm // https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.3.0/com.ibm.zos.v2r3.hala001/server.htm
struct sockaddr_in client; /* client address information */
struct sockaddr_in server; /* server address information */ struct sockaddr_in server; /* server address information */
int s; /* socket for accepting connections */ int s; /* socket for accepting connections */
int ns; /* socket connected to client */
/* /*
* Get a socket for accepting connections. * Get a socket for accepting connections.
@ -67,20 +65,35 @@ namespace ix
return std::make_pair(false, errMsg); return std::make_pair(false, errMsg);
} }
/* for (;;)
* Accept a connection.
*/
socklen_t address_len = sizeof(socklen_t);
if ((ns = accept(s, (struct sockaddr *)&client, &address_len)) == -1)
{ {
std::string errMsg = "Accept()"; /*
return std::make_pair(false, errMsg); * Accept a connection.
*/
struct sockaddr_in client; /* client address information */
int clientFd; /* socket connected to client */
socklen_t address_len = sizeof(socklen_t);
if ((clientFd = accept(s, (struct sockaddr *)&client, &address_len)) == -1)
{
std::string errMsg = "Accept()";
return std::make_pair(false, errMsg);
}
_workers.push_back(std::thread(&WebSocketServer::handleConnection, this, clientFd));
// handleConnection(clientFd);
} }
return std::make_pair(true, "");
}
void WebSocketServer::handleConnection(int fd)
{
// We only handle one connection so far, and we just 'print received message from it' // We only handle one connection so far, and we just 'print received message from it'
ix::WebSocketTransport webSocketTransport; ix::WebSocketTransport webSocketTransport;
SocketConnect::configure(ns); // We could/should do this inside initFromSocket SocketConnect::configure(fd); // We could/should do this inside initFromSocket
webSocketTransport.initFromSocket(ns); webSocketTransport.initFromSocket(fd);
for (;;) for (;;)
{ {
@ -88,7 +101,7 @@ namespace ix
// 1. Dispatch the incoming messages // 1. Dispatch the incoming messages
webSocketTransport.dispatch( webSocketTransport.dispatch(
[this](const std::string& msg, [&webSocketTransport](const std::string& msg,
size_t wireSize, size_t wireSize,
bool decompressionError, bool decompressionError,
WebSocketTransport::MessageKind messageKind) WebSocketTransport::MessageKind messageKind)
@ -122,12 +135,11 @@ namespace ix
// WebSocket::invokeTrafficTrackerCallback(msg.size(), true); // WebSocket::invokeTrafficTrackerCallback(msg.size(), true);
std::cout << "received: " << msg << std::endl; std::cout << "received: " << msg << std::endl;
webSocketTransport.sendBinary(msg);
}); });
std::chrono::duration<double, std::milli> wait(10); std::chrono::duration<double, std::milli> wait(10);
std::this_thread::sleep_for(wait); std::this_thread::sleep_for(wait);
} }
return std::make_pair(true, "");
} }
} }

View File

@ -8,6 +8,8 @@
#include <utility> // pair #include <utility> // pair
#include <string> #include <string>
#include <vector>
#include <thread>
namespace ix namespace ix
{ {
@ -18,7 +20,11 @@ namespace ix
std::pair<bool, std::string> run(); std::pair<bool, std::string> run();
void handleConnection(int fd);
private: private:
int _port; int _port;
std::vector<std::thread> _workers;
}; };
} }

View File

@ -444,6 +444,7 @@ namespace ix
return WebSocketInitResult(false, 0, std::string("Failed sending response to ") + dest); return WebSocketInitResult(false, 0, std::string("Failed sending response to ") + dest);
} }
setReadyState(OPEN);
std::cout << "initFromSocket::end" << std::endl; std::cout << "initFromSocket::end" << std::endl;
return WebSocketInitResult(true, 200, "", headers); return WebSocketInitResult(true, 200, "", headers);