| 首页 | 新闻 | 网页 | 设计 | 色彩 | 原创 | 视觉 | 素材 | 动漫 | 酷站 | 策划 | 文案 | 访谈 | 运营 | 编程 | 数据库 | 服务器 | 下载 | 图库 | 
您的位置: 幽幽天空 > 网页 > 编程开发 > Visual C++教程 > 文章正文 用户登录
T2Click已升级 请
T2click:我爱卡(
T2click:商机热线
DoubleClick终将花
推荐:不用手机IC
免费5G/PHP/mysql
谈谈CNNIC 域名投
Flash2k4+CF制作留
CFMX For F2K4 We
在flash中构建asp

myicq-1.0a1服务器代码分析(一)           

myicq-1.0a1服务器代码分析(一)

作者:佚名 来源:CSDN 作者: solarsoft 更新:2006-8-25 21:05:35 错误报告 我要投稿

myicq代码的公布已经有一段时间了,听说作者张勇已经不再公开的服务器端的代码了,不尤觉得可惜,拜读他的作品已经有一段时间了,今天来发表一下自己的意见。

我现在就从整体的构架来谈谈他服务器端的代码,服务器代码可分成数据库、upd服务、服务器群组、插件四块。

我这里对upd服务的实现进行一些讨论,

一、数据缓冲包的类管理

先定义了抽象类OutPacket与InPacket,代码如下:

class OutPacket {

public:

       virtual OutPacket &operator <<(uint8 b) = 0;

       virtual OutPacket &operator <<(uint16 w) = 0;

       virtual OutPacket &operator <<(uint32 dw) = 0;

       virtual OutPacket &operator <<(ICQ_STR &str) = 0;

};

 

 

class InPacket {

public:

       virtual InPacket &operator >>(uint8 &b) = 0;

       virtual InPacket &operator >>(uint16 &w) = 0;

       virtual InPacket &operator >>(uint32 &dw) = 0;

       virtual InPacket &operator >>(ICQ_STR &str) = 0;

};

在实现其派生类IcqOutPacket与IcqInPacket,并定义数据缓冲区uint8 data[UDP_PACKET_SIZE],大小为1024,主要是处理数据的输入与输出。

再派生其类UdpOutPacket与UdpInPacket,此时进行了DES的数据加密与解密的处理,并加入数据头,定义如下:

struct UDP_CLI_HDR {

       uint16 ver;

       uint32 reserved;

       uint32 uin;

       uint32 sid;

       uint16 cmd;

       uint16 seq;

       uint16 cc;              // check code

};

 

struct UDP_SRV_HDR {

       uint16 ver;

       uint32 reserved;

       uint32 uin;

       uint32 sid;

       uint16 cmd;

       uint16 seq;

       uint16 ackseq;

};

此时缓冲区的包类已经结束。

二、socket服务代码的编写

先定义2个类RefObject与 Session,其中RefObject为记数类,两代码如下:

class RefObject {

public:

       RefObject() {

              refCount = 1;

       }

 

       int addRef() {

              return ++refCount;

       }

       int release() {

              register int ret = --refCount;

              if (!ret)

                     delete this;

              return ret;

       }

 

protected:

       virtual ~RefObject() {}

 

       int refCount;

};

class Session {

public:

       uint32 uin;

       uint32 status;

       uint32 ip;

       uint16 msgport;

       uint32 realip;

};

现在我们来看一下udp服务类的定义

class UdpSession : public RefObject, public Session {

public:

       UdpSession(UdpInPacket &in, uint32 ip, uint16 port);

       ~UdpSession();

 

       UdpOutPacket *createPacket(uint16 cmd, uint16 ackseq = 0);

       void sendPacket(UdpOutPacket *p);

 

       void sendOnline(Session *s, ICQ_STR &domain);

       void sendOffline(uint32 uin, ICQ_STR &domain);

       void sendStatusChanged(Session *s, ICQ_STR &domain);

       void sendMessage(uint8 type, QID &src, uint32 when, ICQ_STR &text);

       void sendGroupTypes();

       void updateContactReply(uint16 seq, uint8 *data, int n, Server *server = NULL);

       void searchRandomReply(uint16 seq, uint8 *data, int n);

       void searchUINReply(uint16 seq, uint8 *data, int n);

 

       void dead();

       void logout();

 

       void onlineNotify();

       void offlineNotify();

       void statusNotify(uint32 newStatus);

 

       static bool init();

       static void destroy();

       static bool onReceive();

       static void checkSendQueue();

       static void checkKeepAlive();

 

       static void sendMessage(uint8 type, QID &dst, QID &src, uint32 when, ICQ_STR &text);

       static void addFriend(uint16 seq, QID &dst, uint32 src, Server *server = NULL);

       static void addFriendAuth(uint16 seq, QID &dst, uint32 src, Server *server, uint8 auth, bool sendAuthMsg = true);

       static void updateContact(uint16 seq, uint32 dst, uint32 src, Server *server = NULL);

       static void searchUIN(uint16 seq, uint32 dst, uint32 src, Server *server = NULL);

 

       uint8 auth;

       uint8 face;

       char nickname[MAX_NICK + 1];

       char province[MAX_PROVINCE + 1];

 

       uint16 tcpver;

       uint16 port;

       uint32 oldMsgID, lastMsgID;

       char subkey[128];

 

       ListHead uinItem;

       ListHead ipportItem;

       ListHead listItem;

 

       static int sock;

       static uint32 sessionCount;

 

private:

       void createPacket(UdpOutPacket &out, uint16 cmd, uint16 seq, uint16 ackseq);

       void sendDirect(UdpOutPacket *p);

       void sendAckPacket(uint16 seq);

       bool setWindow(uint16 seq);

       void notify(DB_CALLBACK cb1, DB_CALLBACK cb2);

 

       void onAck(uint16 seq);

       void onKeepAlive(UdpInPacket &in);

       void onNewUIN(UdpInPacket &in);

       void onGetContactList(UdpInPacket &in);

       void onGetRemoteContactList(UdpInPacket &in);

       void onLogin(UdpInPacket &in);

       void onLogout(UdpInPacket &in);

       void onChangeStatus(UdpInPacket &in);

       void onUpdateContact(UdpInPacket &in);

       void onModifyUser(UdpInPacket &in);

       void onUpdateUser(UdpInPacket &in);

       void onSendMessage(UdpInPacket &in);

       void onSearchRandom(UdpInPacket &in);

       void onSearchCustom(UdpInPacket &in);

       void onAddFriend(UdpInPacket &in);

       void onDelFriend(UdpInPacket &in);

       void onSendBCMsg(UdpInPacket &in);

       void onGetServerList(UdpInPacket &in);

       void onGetGroupList(UdpInPacket &in);

       void onSearchGroup(UdpInPacket &in);

 

       void onCreateGroup(UdpInPacket &in);

       void onEnterGroup(UdpInPacket &in);

       void onExitGroup(UdpInPacket &in);

       void onGroupStart(UdpInPacket &in);

       void onGroupMessage(UdpInPacket &in);

       void onGroupCmd(UdpInPacket &in);

 

       bool onPacketReceived(UdpInPacket &in);

 

       uint16 udpver;

       uint32 sid;

       uint16 sendSeq;

       uint16 recvSeq;

       uint32 window;

       time_t expire;

 

       uint8 isDead : 1;

 

       IcqGroup *group;

 

       ListHead sendQueue;

 

       DECLARE_SLAB(UdpSession)

};

都是一些与客户端通信的接口。

我的评论与建议:这样的数据缓冲区的设计,代码执行效率不是很高,而且可能造成服务器缓冲区溢出的错误,应该设计时加入一些防范措施。

对UdpSession的设计不太合理,模块化不高,代码零乱。我到认为这里到可以用插件机制,也就是一个插件来管理部分的通信消息命令。这样可提高程序的可读性,还便于今后通信消息命令的扩展。这点可以学学入侵检测系统Snort的规则处理模块与插件的管理。

 

 

文章录入:skyuu    责任编辑:skyuu 
  • 上一篇文章:

  • 下一篇文章:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    发表评论:
    姓名:  评 分: 1分 2分 3分 4分 5分
     
  • 严禁发表危害国家安全、政治、黄色淫秽等内容的评论。
  • 用户需对自己在使用幽幽天空服务过程中的行为承担法律责任。
  • 本站管理员有权保留或删除评论内容。
  • 评论内容只代表机友个人观点,与本网站立场无关。