C++ socketlite学习总结

重要数据类及其重要成员:

1、class SL_Socket_Epoll_Runner : public SL_Socket_Runner{

  int open(int event_mask=SL_Socket_Handler::READ_EVENT_MASK, int max_size=100000, int max_timeout=100, int thread_number=0);

  此成员主要用来创建epoll_handle_、events_、event_mask_、max_size_、max_timeout_

}

2、class SL_Socket_Handler
{
public:
    enum EVENT_MASK
    {
        NULL_EVENT_MASK     = 0x00000000,
        READ_EVENT_MASK     = 0x00000001,
        WRITE_EVENT_MASK    = 0x00000002,
        EXCEPT_EVENT_MASK   = 0x00000004,
        TIMEOUT_EVENT_MASK  = 0x00000008,
        ALL_EVENT_MASK      = READ_EVENT_MASK | WRITE_EVENT_MASK | EXCEPT_EVENT_MASK  | TIMEOUT_EVENT_MASK
    };

    inline SL_Socket_Handler()
    virtual ~SL_Socket_Handler() 
    inline SL_SOCKET get_socket() const
    inline void set_socket(SL_SOCKET fd)

  inline void set_socket_source(SL_Socket_Source *socket_source)
    inline void set_socket_runner(SL_Socket_Runner *socket_runner)
    inline SL_Socket_Source* get_socket_source() const

    inline SL_Socket_Runner* get_socket_runner() const
    virtual int handle_open(SL_SOCKET fd, SL_Socket_Source *socket_source, SL_Socket_Runner *socket_runner)

    virtual int handle_close() 
    virtual int handle_read()
    virtual int handle_write()
    virtual int handle_exception()
    //tcpclient连接前,预处理事件
    virtual int handle_connecting() 
    //tcpclient连接成功后,通知事件
    virtual int handle_connect() 

    inline void close()
    int write_data(const char *buf, int len);

protected:
    SL_SOCKET           socket_;
    SL_Socket_Source    *socket_source_;
    SL_Socket_Runner    *socket_runner_;

private:
    int                 event_mask_;    //事件码(上层不能修改)
    int                 runner_pos_;    //在socket_runner_中位置(上层不能修改)

    template <typename TSyncMutex> friend class SL_Socket_Select_Runner;
    template <typename TSyncMutex> friend class SL_Socket_Poll_Runner;
    template <typename TSyncMutex> friend class SL_Socket_Epoll_Runner;
    template <typename TSyncMutex> friend class SL_Socket_Iocp_Runner;
    template <typename TSyncMutex> friend class SL_Socket_kqueue_Runner;
    template <typename TSocketRunner> friend class SL_Socket_Runner_Group;
};

3、class SL_Socket_Message_Handler : public SL_Socket_Handler
{
public:
    SL_Socket_Message_Handler()

    virtual ~SL_Socket_Message_Handler() 
    virtual int handle_open(SL_SOCKET fd, SL_Socket_Source *socket_source, SL_Socket_Runner *socket_runner)
    //接收到数据时,回调此函数
    virtual int handle_data(const char *buf, int len, int read_times)

    //接收到完整消息时,回调此函数
    virtual int handle_message(const char *msg, int len)

    int handle_read();
    int write_message(const char *msg, int len);

    //目前没有考虑跨平台的字节顺序
    //一般字节顺序:BIG Endian,LITTLE Endian
    //1)取得消息长度
    static int get_msglen(const char *msg, int len, int msglen_bytes, int byteorder);
    //2)设置消息长度
    static int set_msglen(char *msg, int len, int msglen_bytes, int byteorder);

private:
    int             need_len_;      //组成一个消息包,还需要长度
    int8            last_left_;     //当收到的数据长度小于消息len域所占长度时有效,一般为0
    SL_ByteBuffer   msg_buffer_;    //消息缓存
};

 4、template <typename THandler, typename TObjectPool>
class SL_Socket_TcpClient : public SL_Socket_Source
{
public:
    SL_Socket_TcpClient()
     ~SL_Socket_TcpClient()
    int open(const char *server_name, ushort server_port, const char *local_name=NULL, ushort local_port=0, bool is_bind_localaddr=false, bool is_ipv6=false)

    int open()
    int close()
    int clear()
    int connect()
    int disconnect(SL_Socket_Handler *socket_handler)
    int recv(char *buf, int len, int flags)
    int send(const char *buf, int len, int flags)
    int set_config(ulong connect_timeout ,bool  is_autoconnect ,bool  is_add_runner ,uint  recvbuffer_size ,uint  msgbuffer_size , uint8 msglen_bytes,uint8 msg_byteorder,  bool  is_free_self_handler)
    int set_interface(TObjectPool *object_pool, SL_Socket_Runner *socket_runner)
    int add_serveraddr(const char *server_name, ushort server_port, bool is_ipv6=false)
    int remove_serveraddr(int pos)
    int get_serveraddr_count()
    int get_connect_serveraddr_pos()
    SL_Socket_INET_Addr* get_serveraddr(int pos)
    bool get_connected()
    bool get_autoconnect()
    void set_autoconnect(bool is_autoconnect)
    THandler* get_socket_handler()
    int put_data(SL_ByteBuffer &buf)
    int put_data(const char *buf, int len)
    SL_Socket_Handler* alloc_handler()
    void free_handler(SL_Socket_Handler *socket_handler)
    protected:
    std::vector<SL_Socket_INET_Addr> serveraddr_list_;
    std::string     local_name_;
    ushort          local_port_;
    bool            is_bind_localaddr_;

    uint            connect_serveraddr_pos_;
    ulong           connect_timeout_;
    volatile bool   is_connected_;                  //连接状态(fase:失败,true:成功)
    volatile bool   is_autoconnect_;                //是否自动重连
 SL_Sync_ThreadMutex mutex_;

    TObjectPool     *object_pool_;
};

5、class SOCKETLITE_API SL_Socket_Source
{
public:
    SL_Socket_Source()
    virtual ~SL_Socket_Source() 
    virtual SL_Socket_INET_Addr* get_local_addr()
    //SL_TcpClient的特定函数
    virtual int send(const char *buf, int len, int flag) 
    virtual int recv(char *buf, int len, int flag) 
    virtual bool get_connected()
    virtual bool get_autoconnect()
    virtual int connect()
    virtual int disconnect(SL_Socket_Handler *socket_handler)
    inline SL_Socket_Handler* get_socket_handler()
    inline SL_Socket_Runner* get_socket_runner()
    inline uint get_recvbuffer_size() const
    inline uint get_msgbuffer_size() const
    inline uint8 get_msglen_bytes() const
    inline uint8 get_msg_byteorder() const
    inline bool get_add_runner() const
    static int get_msglen_int8(const char *msg, int len)
    static int get_msglen_int16_host(const char *msg, int len)
    static int get_msglen_int16_network(const char *msg, int len)
    static int get_msglen_int32_host(const char *msg, int len)
    static int get_msglen_int32_network(const char *msg, int len)
    static void set_msglen_int8(char *msg, int len)
    static void set_msglen_int16_host(char *msg, int len)
    static void set_msglen_int16_network(char *msg, int len)
    static void set_msglen_int32_host(char *msg, int len)
    static void set_msglen_int32_network(char *msg, int len)

public:
    virtual void free_handler(SL_Socket_Handler *socket_handler)
    typedef int (*get_msglen_proc)(const char *msg, int len);
    typedef void (*set_msglen_proc)(char *msg, int len);

    get_msglen_proc     get_msglen;         //取得消息长度函数指针
    set_msglen_proc     set_msglen;         //设置消息长度函数指针

protected:
    SL_Socket_Handler   *socket_handler_;
    SL_Socket_Runner    *socket_runner_;

    uint                recvbuffer_size_;
    uint                msgbuffer_size_;
    uint8               msglen_bytes_;          //消息长度域所占字节数(一般为1,2,4)
    uint8               msg_byteorder_;         //消息的字节顺序(0:host-byte,1:big endian(network-byte) 2:little endian)
    bool                is_add_runner_;         //新连接是否加入Socket_Runner中,进行事件分派
    bool                is_free_self_handler_;  //可否释放Source本身的socket_handler_,
               //若socket_handler_加入SL_Socket_SendControl_HandlerManager时,应为false
};

6、template <typename TClientHandler, typename TObjectPool, typename TServerHandler=SL_Socket_TcpServer_Handler>
class SL_Socket_TcpServer : public SL_Socket_Source
{
public:
    SL_Socket_TcpServer()
    ~SL_Socket_TcpServer()
    int open(ushort local_port, int backlog=128, const char *local_name=NULL, bool is_ipv6=false)
    int close()
    int set_config(ushort accept_thread_num,bool   is_add_runner,uint   maxconnect_num  ,uint   recvbuffer_size,uint   msgbuffer_size ,uint8  msglen_bytes,uint8  msg_byteorder)
    int set_interface(TObjectPool *object_pool, SL_Socket_Runner *socket_runner)
    SL_Socket_TcpServer_Handler* get_socket_handler()
    SL_Socket_INET_Addr* get_local_addr()
    SL_Socket_Handler* alloc_handler()
    void free_handler(SL_Socket_Handler *socket_handler)

private:
    static void* accept_proc(void *arg)

protected:
    SL_Socket_INET_Addr local_addr_;

    uint                maxconnect_num_;
    ushort              accept_thread_num_;
    SL_Thread_Group     accept_thread_group_;
    TServerHandler      server_handler_;
    TObjectPool         *object_pool_;
};

7、template <typename T>
class SL_TLS
{   
public:
    SL_TLS()
  ~SL_TLS()
    int init()
    int clear()
    // Get the value.
    T* get() const
    T* operator->() const
    operator T*() const
    // Set the value.
    void set(T *value)
    void operator= (T *value)

private:
    pthread_key_t   tls_key_;
};//对线程存储接口的一些封装。/*链接地址:http://www.douban.com/note/162329664/*/

posted @ 2014-01-08 11:17  twin  阅读(374)  评论(0)    收藏  举报