基于Tars高并发IM系统的设计与实现-实战篇6
客户端设计实现
客户端采用跨平台SDK+原生UI的方案。
 此处重点介绍SDK实现,UI用户可以根据自己需求进行开发。
方案架构图:

方案实现:
-  TCP链接 
 CTcpSocket
 CSSLSocket
-  长链接维护、数据收发 
 CTNSendRecvThread
-  OMTP数据报文处理 
 CTNProcessThread
-  日志记录 
 CLog
-  SDK对外接口及回调接口 
 ITnImSdkCallback
 /**
     * OMTP回调接口
     */
    class IOtimSdkCallback
    {
    public:
        virtual ~IOtimSdkCallback(){}
        
    
        virtual void netStatusChanged(int32_t status) = 0;
   
        virtual void loginResp(int32_t code, const std::string &clientId) = 0;
    
   
        virtual void kickOut() = 0;
   
        virtual void hotSessionResp(otim::HotSessionResp* hotSessions) = 0;
     
        virtual void msgRecv(int type, const std::string &packId, otim::MsgReq* req) = 0;
   
        virtual void msgAck(const std::string &packId, otim::MsgAck* ack) = 0;     
    };
       
    class IOtimSdk
    {
    public:
        virtual ~IOtimSdk(){}
        
        /**
         * 增加IM 服务器ip地址和端口
         * @param host ip地址
         * @param port 端口
         * @param isSSL 是否支持ssl连接
         */
        virtual void addHostInfo(const std::string&  host, int32_t port, bool isSSL) = 0;
        
        /**
         * 设置SDK 回调指针
         * @param callback 回调接口指针
         */
        virtual void setCallback(IOtimSdkCallback* callback) = 0;
        /**
         * 登录
         * @param name 用户名,可以不填写
         * @param password 密码,必须填写token
         */
        virtual int32_t login (const std::string&  name, const std::string&  password) = 0;
        /**
         * 登出
         * @param notifyServer 是否向server发送数据报文
         */
        virtual int32_t logout (bool notifyServer) = 0;
        /**
         * 发送消息
         * @param message 消息体
         */
        virtual int32_t sendMessage(int type, otim::MsgReq& req) = 0;
        /**
         * 获取网络状态
         */
        virtual int32_t getNetStatus() = 0;
        
        //获取当前clientId
        virtual std::string getClientId() = 0;
        
        /**
            * 请求用户信息
            * @param userIds 用户Id列表
            */
        virtual void reqUserinfosFromServer(std::vector<std::string> userIds) = 0;
        
     };
    
    
    /**
     * 日志记录接口
     */
    class IOtimLog{
    public:
        virtual ~IOtimLog(){}
        /**
         * 写日志
         * @param logs 日志内容
         */
        virtual void writeLog(const std::string& logs) = 0;
        
        /**
         * 获取日志文件名称及路径
         * @return 日志文件路径及名称 外部必须释放 返回字符串内存
         */
        virtual const std::string& getLogFileName() = 0;
        
    };
    
    /**
     * 初始化日志模块
     * @param appPath 日志保存路径
     */
    void initLog(const std::string& appPath);
    /**
     * 获取日志接口实例,调用此接口前必须调用 initLog
     */
    IOtimLog* getLogInstance();
    
    /**
     * 初始化IM 模块
     * @param clientInfo IM 所需要的参数
     */
    IOtimSdk* initIm(TNClientInfo &clientInfo);
    
  
    /**
     * 获取IMSDK IM模块实例
     * 调用此函数前必须调用 initIm接口
     */
    IOtimSdk* getImSDK();
源码说明
源码地址
https://github.com/lanhy/otim.git
目录说明
- doc 
  - 文档目录,目前有两个文档著作
- 《基于Tars高并发IM系统的设计与实现》
- 《OMTP协议说明文档》
 
- client 
  - 客户端SDK及示例代码
 
- server 
  - 服务端代码,基于Tars微服务架构的IM系统相关子服务;
 
- test 
  - 自动测试代码;
 
服务端源码结构
- BrokerServer 
 接入服务
- HttpServer
 开放平台及接口服务
- AuthServer 
 认证服务
- GroupChatServer 
 群聊服务
- PushServer
 离线push服务
- BizMsgServer 
 业务通知消息服务
- HistoryMsgServer 
 离线(历史)消息服务
- SingleChatServer
 单聊服务
- UserFriendServer
 用户好友服务开放平台及接口服务
- MsgOperatorServer 
 消息操作服务
- OlapServer 
 Mysql冷存储服务
- third
 第三方库
- Common 
 公共模块
- CommonTars 
 OMTP协议Tars文件
客户端SDK源码结构
- lib 
 第三方库
- source
 客户端SDK代码
- source
 客户端SDK代码
- OMTP
- TestSDK 
 SDK调用实例



















