diff --git a/client/src/linux/linux.c b/client/src/linux/linux.c index 4cedea6..62b8d1c 100644 --- a/client/src/linux/linux.c +++ b/client/src/linux/linux.c @@ -442,12 +442,13 @@ static void processEvent(void) { static void processNetworkEvent(void) { - ENetEvent *event = NULL; + ENetEvent *event = NULL; if (_host) { while (enet_host_service(_host, event, 1)) { switch (event->type) { case ENET_EVENT_TYPE_CONNECT: + logWrite("Connected!\n"); _connected = 1; break; @@ -458,6 +459,7 @@ static void processNetworkEvent(void) { case ENET_EVENT_TYPE_DISCONNECT: case ENET_EVENT_TYPE_DISCONNECT_TIMEOUT: + logWrite("Disconnected!\n"); _connected = 0; _modemCommandMode = 1; comAddToBuffer("\13NO CARRIER\13", 12); diff --git a/client/src/main.c b/client/src/main.c index e53f356..6909cca 100644 --- a/client/src/main.c +++ b/client/src/main.c @@ -30,6 +30,7 @@ * - Find a light grey to replace white widget data areas * - No thumb in listbox scrollbar * - Layout container widgets! + * - Fix variable names. something = local; _something = file global; __something = project global */ diff --git a/server/src/client.c b/server/src/client.c index a21ad02..686e9eb 100644 --- a/server/src/client.c +++ b/server/src/client.c @@ -27,15 +27,20 @@ void *clientThread(void *data) { - ClientThreadT *client = (ClientThreadT *)data; + ENetPeer *peer = (ENetPeer *)data; + ClientThreadT *client = (ClientThreadT *)peer->data; ENetPacket *packet = NULL; int32_t r; // Send service banner. packet = enet_packet_create("KPMPGSMKII\r", 11, ENET_PACKET_FLAG_RELIABLE); - r = enet_peer_send(client->peer, 0, packet); + r = enet_peer_send(peer, 0, packet); consoleMessageQueue("Packet: %p %d\n", packet, r); + while (client->running) { + + } + pthread_exit(NULL); } diff --git a/server/src/client.h b/server/src/client.h index 8d32b99..243479c 100644 --- a/server/src/client.h +++ b/server/src/client.h @@ -30,7 +30,6 @@ typedef struct ClientThreadS { uint64_t threadIndex; pthread_t threadHandle; pthread_attr_t threadAttributes; - ENetPeer *peer; uint8_t running; } ClientThreadT; diff --git a/server/src/console.c b/server/src/console.c index d852c5b..e74e01a 100644 --- a/server/src/console.c +++ b/server/src/console.c @@ -54,7 +54,7 @@ void consoleMessageQueue(const char *message, ...) { arrput(_consoleMessageQueue, newMessage); pthread_mutex_unlock(&_messageQueueMutex); - free(newMessage); + // Do not free newMessage - we do it when we dequeue the message. } @@ -66,10 +66,12 @@ void consoleRun(void) { uint8_t commandOk = 0; struct timespec remaining = { 0, 0 }; struct timespec sleepTime = { 0, 1000000000/4 }; + size_t i = 0; if (pthread_mutex_init(&_messageQueueMutex, NULL)) utilDie("Unable to create console message queue mutex.\n"); terminalModeConioSet(); + while (_running) { if (kbhit()) { @@ -89,6 +91,16 @@ void consoleRun(void) { // Backspace case 8: case 127: + if (commandIndex > 0) { + commandIndex--; + command[commandIndex] = 0; + printf("%c %c", 8, 8); + if (commandIndex == 0) { + commandEntering = 0; + // Erase prompt, too. + printf("%c %c", 8, 8); + } + } break; // ESC @@ -150,13 +162,20 @@ void consoleRun(void) { pthread_mutex_lock(&_messageQueueMutex); if (arrlenu(_consoleMessageQueue) > 0) { sendToConsole("%s", _consoleMessageQueue[0]); + free(_consoleMessageQueue[0]); arrdel(_consoleMessageQueue, 0); } pthread_mutex_unlock(&_messageQueueMutex); } } - terminalModeOriginalSet(); + // Anything left in the message queue? + for (i=0; ithreadIndex = nextIndex++; - client->peer = event.peer; client->running = 1; - // Keep our index in the peer data for later. - event.peer->data = (void *)client->threadIndex; + // Keep our client in the peer data for later. + event.peer->data = (void *)client; // Make new thread for this client. if (pthread_attr_init(&client->threadAttributes) != 0) utilDie("Unable to create client thread attributes.\n"); pthread_attr_setdetachstate(&client->threadAttributes, PTHREAD_CREATE_JOINABLE); - if (pthread_create(&client->threadHandle, &client->threadAttributes, clientThread, (void *)client) != 0) utilDie("Unable to start client thread.\n"); - // Add to our client list. - arrput(clientList, client); + if (pthread_create(&client->threadHandle, &client->threadAttributes, clientThread, (void *)event.peer) != 0) utilDie("Unable to start client thread.\n"); // Tell the console. enet_address_get_host_ip(&event.peer->address, buffer, 2047); - consoleMessageQueue("%ld: %s connected.\n", client->threadIndex, buffer); + consoleMessageQueue("%ld: [%s] connected.\n", client->threadIndex, buffer); + // Blargh. + ENetPacket *packet = enet_packet_create("KPMPGSMKII\r", 11, ENET_PACKET_FLAG_RELIABLE); + enet_peer_send(event.peer, 0, packet); break; case ENET_EVENT_TYPE_RECEIVE: @@ -140,38 +139,28 @@ static void *serverThread(void *data) { case ENET_EVENT_TYPE_DISCONNECT: case ENET_EVENT_TYPE_DISCONNECT_TIMEOUT: - // Find our client. - for (i=0; idata == clientList[i]->threadIndex) { - // Stop client processing. - clientList[i]->running = 0; - pthread_join(clientList[i]->threadHandle, &status); - // Tell the console. - enet_address_get_host_ip(&event.peer->address, buffer, 2047); - consoleMessageQueue("%ld: %s disconnected.\n", clientList[i]->threadIndex, buffer); - // Delete client. - DEL(clientList[i]); - // Take it out of the client list. - arrdel(clientList, i); - break; - } - } + // Get our client. + client = (ClientThreadT *)event.peer->data; + // Stop client processing. + client->running = 0; + pthread_join(client->threadHandle, &status); + // Tell the console. + enet_address_get_host_ip(&event.peer->address, buffer, 2047); + consoleMessageQueue("%ld: [%s] disconnected.\n", client->threadIndex, buffer); break; } } } // Anyone still running, be rude. Nuke 'em. - for (i=0; ipeer); + for (i=0; ipeerCount; i++) { + client = (ClientThreadT *)server->peers[i].data; // Stop client processing. - clientList[i]->running = 0; - pthread_join(clientList[i]->threadHandle, &status); - // Delete client. - DEL(clientList[i]); + client->running = 0; + pthread_join(client->threadHandle, &status); + // Hang up. + enet_peer_reset(&server->peers[i]); } - arrfree(clientList); pthread_exit(NULL); } @@ -193,7 +182,7 @@ int main(int argc, char *argv[]) { memoryStartup(argv[0]); logOpenByHandle(memoryLogHandleGet()); - logWrite("%s", "Kangaroo Punch MultiPlayer DOS Game Server Mark II\n"); + logWrite("%s", "Kangaroo Punch MultiPlayer Game Server Mark II\n"); logWrite("%s", "Copyright (C) 2020-2021 Scott Duensing scott@kangaroopunch.com\n\n"); configFile = utilAppNameWithNewExtensionGet(argv[0], "ini"); configRead(configFile); @@ -210,8 +199,8 @@ int main(int argc, char *argv[]) { // Set up listening socket. address.host = ENET_HOST_ANY; address.port = settingsPortNumber; - server = enet_host_create (&address, /* the address to bind the server host to */ - settingsMaxClients, /* allow up to 32 clients and/or outgoing connections */ + server = enet_host_create(&address, /* the address to bind the server host to */ + settingsMaxClients, /* allow up to XX clients and/or outgoing connections */ 1, /* allow up to 2 channels to be used, 0 and 1 */ 0, /* assume any amount of incoming bandwidth */ 0 /* assume any amount of outgoing bandwidth */