fixing net buffer bug
This commit is contained in:
parent
fbba810e12
commit
a77442327f
69
main.cpp
69
main.cpp
@ -37,63 +37,12 @@ static const int SERVER_PORT = 1338;
|
|||||||
static const int SERVER_MAX_CLIENTS = 1;
|
static const int SERVER_MAX_CLIENTS = 1;
|
||||||
|
|
||||||
|
|
||||||
//
|
|
||||||
// Local variables
|
|
||||||
//
|
|
||||||
|
|
||||||
static bool executeServer = true;
|
|
||||||
|
|
||||||
|
|
||||||
//
|
|
||||||
// Local functions (bound)
|
|
||||||
//
|
|
||||||
|
|
||||||
static void serverStop() {
|
|
||||||
printf("[S] Server stop request received\n");
|
|
||||||
executeServer = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// Local functions
|
// Local functions
|
||||||
//
|
//
|
||||||
|
|
||||||
static void server() {
|
static void serverTest() {
|
||||||
Server server;
|
printf("Server test!\n");
|
||||||
|
|
||||||
printf("[S] Starting server on port %d\n", SERVER_PORT);
|
|
||||||
if (!server.start(SERVER_PORT, SERVER_MAX_CLIENTS)) {
|
|
||||||
perror("[S] Unable to start server");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("[S] Binding server functions\n");
|
|
||||||
server.binding()->bind("serverStop", serverStop);
|
|
||||||
|
|
||||||
printf("[S] Beginning client updates\n");
|
|
||||||
while (executeServer) {
|
|
||||||
server.advance();
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("[S] Server shutting down");
|
|
||||||
server.stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
static void client() {
|
|
||||||
Client client;
|
|
||||||
|
|
||||||
printf("[C] Connecting to server on port %d...\n", SERVER_PORT);
|
|
||||||
if (!client.connect("localhost", SERVER_PORT)) {
|
|
||||||
perror("[C] Unable to connect to server\n");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("[C] Sending server shutdown request\n");
|
|
||||||
client.protocol()->invoke("serverStop");
|
|
||||||
|
|
||||||
while (client.connected()) {
|
|
||||||
client.advance();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -102,23 +51,27 @@ static void client() {
|
|||||||
//
|
//
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
printf("[S] Starting server on port %d\n", SERVER_PORT);
|
|
||||||
Server server;
|
Server server;
|
||||||
if (!server.start(SERVER_PORT, 1)) {
|
printf("[S] Starting server on port %d\n", SERVER_PORT);
|
||||||
|
if (!server.start(SERVER_PORT, SERVER_MAX_CLIENTS)) {
|
||||||
printf("[S] Unable to start server\n");
|
printf("[S] Unable to start server\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
server.binding()->bind("serverTest", serverTest);
|
||||||
|
|
||||||
printf("[S] Connecting to server on port %d\n", SERVER_PORT);
|
|
||||||
Client client;
|
Client client;
|
||||||
|
printf("[S] Connecting to server on port %d\n", SERVER_PORT);
|
||||||
if (!client.connect("localhost", SERVER_PORT)) {
|
if (!client.connect("localhost", SERVER_PORT)) {
|
||||||
printf("[C] Unable to connect to server\n");
|
printf("[C] Unable to connect to server\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
server.advance();
|
||||||
|
client.protocol()->invoke("serverTest");
|
||||||
|
client.advance();
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -57,16 +57,17 @@ Stream::State Stream::advance() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (socket_->wait(Socket::MASK_READ, 0)) {
|
if (socket_->wait(Socket::MASK_READ, 0)) {
|
||||||
const int bytesReceived = socket_->receive(
|
byte buffRecv[1024];
|
||||||
buffNet_.data(),
|
const int bytesRecv = socket_->receive(
|
||||||
buffNet_.bytes()
|
buffRecv,
|
||||||
|
sizeof(buffRecv)
|
||||||
);
|
);
|
||||||
|
|
||||||
if (bytesReceived <= 0) {
|
if (bytesRecv <= 0) {
|
||||||
return STATE_ERROR_CONNECTION;
|
return STATE_ERROR_CONNECTION;
|
||||||
}
|
}
|
||||||
|
|
||||||
buffRecv_.addToBack(buffNet_.data(), bytesReceived);
|
buffRecv_.addToBack(buffRecv, bytesRecv);
|
||||||
}
|
}
|
||||||
|
|
||||||
return STATE_READY;
|
return STATE_READY;
|
||||||
|
@ -60,7 +60,6 @@ class Stream {
|
|||||||
Socket* socket_;
|
Socket* socket_;
|
||||||
Buffer buffRecv_;
|
Buffer buffRecv_;
|
||||||
Buffer buffSend_;
|
Buffer buffSend_;
|
||||||
Buffer buffNet_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user