Skip to content

Commit

Permalink
Add system message method. Add inband connect and disconnect messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
kev009 committed Dec 18, 2010
1 parent bbbbd19 commit 89bc312
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 12 deletions.
5 changes: 4 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ DISTCLEANFILES = craftd.conf.dist
crafdconfdir = ${sysconfdir}/craftd/
crafdconf_DATA = craftd.conf.dist motd.conf.dist


# bstring extas
bstrdir = third-party/bstring
EXTRA_DIST += $(bstrdir)/license.txt $(bstrdir)/security.txt \
$(bstrdir)/bstrlib.txt $(bstrdir)/porting.txt

# Jansson extras
jsondir = third-party/jansson
Expand Down
4 changes: 3 additions & 1 deletion README
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ You should have a good understanding of libevent-2.
http://www.wangafu.net/~nickm/libevent-book/
http://mc.kev009.com/libevent - Up to date doxygen

Bundled JSON API (used for web GUI and config):
Bundled Libs:
JSON API (used for web GUI and config):
Jansson (Git head) - https://github.com/akheron/jansson
bstring - Safer C strings - http://bstring.sourceforge.net/

Protocol reference:
http://mc.kev009.com/wiki/
Expand Down
23 changes: 18 additions & 5 deletions src/craftd.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,7 @@ errorcb(struct bufferevent *bev, short error, void *ctx)

if (error & BEV_EVENT_EOF)
{
/* Connection closed, remove client from tables here */
if (player->username != NULL)
LOG(LOG_INFO, "Connection closed for: %s", player->username->data);
else
LOG(LOG_INFO, "Connection closed ip: %s", player->ip);
/* Connection closed */
finished = 1;
}
else if (error & BEV_EVENT_ERROR)
Expand All @@ -119,6 +115,23 @@ errorcb(struct bufferevent *bev, short error, void *ctx)

if (finished)
{
if (player->username != NULL)
{
/* In-band disconnect message */
bstring dconmsg = bformat("%s has disconnected",
player->username->data);
send_syschat(dconmsg);
bstrFree(dconmsg);

/* System log message */
LOG(LOG_INFO, "Connection closed for: %s", player->username->data);
}
else
{
/* The user hasn't gotten far enough to register a username */
LOG(LOG_INFO, "Connection closed ip: %s", player->ip);
}

//TODO: Add mutual exclusion so a worker doesn't get a null ptr
//TODO: Convert this to a SLIST_FOREACH
//XXXX Grab a rdlock until player is found, wrlock delete, free
Expand Down
1 change: 1 addition & 0 deletions src/network/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ void *run_worker(void *arg);
/* Public protocol functions that can be exposed to APIs */
void send_directchat(struct PL_entry *player, bstring message);
void send_chat(struct PL_entry *player, bstring message);
void send_syschat(bstring message);
void send_prechunk(struct PL_entry *player, int32_t x, int32_t z, bool mode);
void send_chunk(struct PL_entry *player, int32_t x, int16_t y, int32_t z,
uint8_t sizex, uint8_t sizey, uint8_t sizez);
Expand Down
33 changes: 28 additions & 5 deletions src/network/send.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ process_login(struct PL_entry *player, bstring username, uint32_t ver)
pthread_rwlock_unlock(&player->rwlock);

send_loginresp(player);
send_prechunk(player, -1, -1, true); // TODO: pull spwan position from file
//send_prechunk(player, 0, -1, true); // TODO: pull spwan position from file
//send_chunk(player, 0, 0, 0, 16, 128, 16); // TODO: pull spawn position

for(int i = -4; i < 5; i++)
Expand All @@ -87,6 +87,15 @@ process_login(struct PL_entry *player, bstring username, uint32_t ver)
send_spawnpos(player, 32, 260, 32); // TODO: pull spawn position from file
//send inv
send_movelook(player, 0, 128.1, 128.2, 0, 0, 0, false); //TODO: pull position from file

//send_directchat(player, motd);

/* Login message */
bstring loginmsg = bformat("Player %s has joined the game!",
player->username->data);
send_syschat(loginmsg);
bstrFree(loginmsg);

return;
}

Expand Down Expand Up @@ -200,10 +209,7 @@ send_chat(struct PL_entry *player, bstring message)
{
struct PL_entry *player_iter;

bstring newmsg = bfromcstr("<");
bconcat(newmsg, player->username);
bcatcstr(newmsg, "> ");
bconcat(newmsg, message);
bstring newmsg = bformat("<%s> %s", player->username->data, message->data);

LOG(LOG_INFO, "Chat: %s", newmsg->data);

Expand All @@ -219,6 +225,23 @@ send_chat(struct PL_entry *player, bstring message)
return;
}

void
send_syschat(bstring message)
{
struct PL_entry *player_iter;

LOG(LOG_INFO, "Syschat: %s", message->data);

pthread_rwlock_rdlock(&PL_rwlock);
SLIST_FOREACH(player_iter, &PL_head, PL_entries)
{
send_directchat(player_iter, message);
}
pthread_rwlock_unlock(&PL_rwlock);

return;
}

/**
* Send a prechunk packet to the player
*
Expand Down

0 comments on commit 89bc312

Please sign in to comment.