Skip to content

Commit

Permalink
Interactive chat client with rlutil
Browse files Browse the repository at this point in the history
  • Loading branch information
cxong committed Jan 23, 2016
1 parent a9265d1 commit ce4bdf7
Show file tree
Hide file tree
Showing 5 changed files with 826 additions and 8 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Expand Up @@ -13,5 +13,5 @@ ADD_SUBDIRECTORY(enet)

add_executable(server server.c)
target_link_libraries(server ${ENet_LIBRARIES})
add_executable(client client.c)
add_executable(client client.c rlutil.h)
target_link_libraries(client ${ENet_LIBRARIES})
60 changes: 58 additions & 2 deletions client.c
@@ -1,6 +1,12 @@
#include <stdio.h>

#include <enet/enet.h>
#include "rlutil.h"


// Simple LAN chat client
// The client sends string messages to the server, and the server passes
// them on to all clients


ENetHost *host;
Expand Down Expand Up @@ -57,9 +63,59 @@ int main(int argc, char *argv[])

// Send a greeting
send_string(peer, "Hello, my name is Inigo Montoya");
enet_host_service(host, &event, 0);

// TODO: event/input loop
memset(buf, 0, sizeof buf);
int check;
do
{
Sleep(1);
const int k = nb_getch();
if (k == KEY_ENTER || k == '\r')
{
// Exit the client on text command
if (strcmp(buf, "quit") == 0 || strcmp(buf, "exit") == 0)
{
break;
}
// If we have something to say, say it to the server
if (strlen(buf) > 0)
{
send_string(peer, buf);
}
memset(buf, 0, sizeof buf);
printf("\n");
}
else if (k > 0 && strlen(buf) < 255)
{
// Hold on to our message until we press enter
buf[strlen(buf)] = (char)k;
printf("%c", k);
}

// Check for new messages from the server; if there are any
// just print them out
ENetEvent event;
check = enet_host_service(host, &event, 0);
if (check > 0)
{
switch (event.type)
{
case ENET_EVENT_TYPE_RECEIVE:
printf("%s\n", event.packet->data);
break;
case ENET_EVENT_TYPE_DISCONNECT:
printf("Lost connection with server\n");
break;
default:
break;
}
}
else if (check < 0)
{
fprintf(stderr, "Error servicing host\n");
}
}
while (check >= 0);

// Shut down client
printf("Client closing\n");
Expand Down
15 changes: 15 additions & 0 deletions rlutil.License.txt
@@ -0,0 +1,15 @@
Title: License

> DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
> Version 2, December 2004
>
>Copyright (C) 2010 Tapio Vierros
>
>Everyone is permitted to copy and distribute verbatim or modified
>copies of this license document, and changing it is allowed as long
>as the name is changed.
>
> DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
>TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
>
>0. You just DO WHAT THE FUCK YOU WANT TO.

0 comments on commit ce4bdf7

Please sign in to comment.