Created
August 21, 2012 07:47
-
-
Save kevinlynx/3413212 to your computer and use it in GitHub Desktop.
chat server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Entity { | |
public: | |
enum { CLIENT, SERVER }; | |
Entity(const CGUID &id, int type, const std::string &brief); | |
// send text to this entity, this function will preprocess `text` | |
bool Send(const std::string &text); | |
private: | |
CGUID m_id; | |
int m_type; | |
std::string m_brief; | |
}; | |
class EntityList { | |
public: | |
typedef std::map<CGUID, Entity*> EntityTable; | |
// create an entity from message, and add it to entity table, client entity will use the player id to identify it | |
// server entity does not care the id. | |
bool Create(int type, const CGUID &id, const std::string &brief); | |
Entity *Find(const CGUID &id) const; | |
// send a text to an entity | |
bool SendText(const CGUID &id, Entity *sender, const std::string); | |
private: | |
// auth whether an entity is valid, used by `SERVER` entity, check in ip list | |
bool AuthEntity(); | |
private: | |
EntityTable m_entities; | |
}; | |
class Group { | |
public: | |
typedef std::list<CGUID> MemberList; | |
Group(const CGUID &id, const std::string &brief); | |
Group(const CGUID &id, const MemberList &mems, const std::string &brief); | |
bool AddMember(const CGUID &entity); | |
bool RemoveMember(const CGUID &entity); | |
void BroadcastText(const std::string &text); | |
private: | |
CGUID m_id; | |
MemberList m_members; | |
std::string m_breif; | |
}; | |
class GroupList { | |
public: | |
typedef std::map<CGUID, Group*> GroupTable; | |
// create a group from message | |
bool Create(const CGUID &id, const std::string &brief, const Group::MemberList &mems); | |
// add a member to a group from message | |
bool AddMember(const CGUID &id, const CGUID &entity); | |
bool RemoveMember(const CGUID &id, const CGUID &entity); | |
// send a text to a group | |
bool SendText(const CGUID &id, Entity *sender, const std::string &text); | |
private: | |
GroupTable m_groups; | |
}; | |
void CreateEntity(ReadBuffer &buf) { | |
... | |
GetInst(EntityList).Create(type, id, brief) | |
} | |
// requirement: associate message with entity | |
void CreateGroup(Entity *entity, ReadBuffer &buf) { | |
if (entity->Type() == Entity::SERVER) { | |
int cnt = buf.ReadNumber<int>(); | |
Group::MemberList mems; | |
for (int i = 0; i < cnt; ++i) { | |
mems.push_back(buf.ReadGUID()); | |
} | |
GetInst(GroupList).Create(id, brief, mems); | |
} | |
} | |
// entity send a text | |
void SendText(Entity *entity, ReadBuffer &buf) { | |
char type = buf.ReadNumber<char>(); | |
CGUID id = buf.ReadGUID(); | |
std::string text = buf.ReadString(); | |
if (type == GROUP) { | |
GetInst(GroupList).SendText(id, entity, text); | |
} else if (type == ENTITY) { | |
GetInst(EntityList).SendText(id, entity, text); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment