X-Git-Url: http://git.cascardo.info/?p=cascardo%2Ff2fchat.git;a=blobdiff_plain;f=friend.c;h=03d306da94479620cb797d389e20cefd590ed0c6;hp=68deccdf67b681c70ef9ae7fabc9c97af3c1866e;hb=f7c4d6ccc1fd3c452481a2d085486f8dec3860fb;hpb=123b3a7a3989a0f040da5cc16f997ff5961fe593 diff --git a/friend.c b/friend.c index 68deccd..03d306d 100644 --- a/friend.c +++ b/friend.c @@ -27,11 +27,18 @@ #include #include "message.h" +enum { + STATE_OFFLINE, + STATE_PINGED, + STATE_ONLINE, +}; + struct friend { char *name; char *address; uint16_t port; GInetSocketAddress *saddr; + int state; }; static GSocket *usock; @@ -64,6 +71,22 @@ int friend_send_message(struct friend *friend, char *buffer, size_t len) return 0; } +void friend_timeout(struct friend *friend) +{ + if (friend->state == STATE_PINGED) { + friend->state = STATE_OFFLINE; + } +} + +void friend_got_message(struct friend *friend, char *buffer, size_t len) +{ + if (len >= 4 && !strncmp(buffer, "PING", 4)) { + pong(friend); + } else if (len >= 4 && !strncmp(buffer, "PONG", 4)) { + friend->state = STATE_ONLINE; + } +} + struct cache { GList *friends; }; @@ -123,6 +146,7 @@ int cache_add_friend(struct cache *cache, char *name, char *address, uint16_t po g_object_unref(addr); cache->friends = g_list_append(cache->friends, friend); ping(friend); + friend->state = STATE_PINGED; return 0; }