fixing net buffer bug

This commit is contained in:
Alex Yatskov 2011-09-03 20:34:30 -07:00
parent fbba810e12
commit a77442327f
3 changed files with 26 additions and 73 deletions

View File

@ -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; printf("[S] Starting server on port %d\n", SERVER_PORT);
if (!server.start(SERVER_PORT, 1)) { 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;
} }
printf("[S] Connecting to server on port %d\n", SERVER_PORT);
Client client;
if (!client.connect("localhost", SERVER_PORT)) {
printf("[C] Unable to connect to server\n");
return 1;
}
server.binding()->bind("serverTest", serverTest);
Client client;
printf("[S] Connecting to server on port %d\n", SERVER_PORT);
if (!client.connect("localhost", SERVER_PORT)) {
printf("[C] Unable to connect to server\n");
return 1;
}
while (true) {
server.advance();
client.protocol()->invoke("serverTest");
client.advance();
}
return 0; return 0;
} }

View File

@ -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;

View File

@ -60,7 +60,6 @@ class Stream {
Socket* socket_; Socket* socket_;
Buffer buffRecv_; Buffer buffRecv_;
Buffer buffSend_; Buffer buffSend_;
Buffer buffNet_;
}; };