贴三段代码,一起分析一下优劣。

[复制链接]
3299|28
dong_abc 发表于 2015-8-15 13:58 | 显示全部楼层
本帖最后由 dong_abc 于 2015-8-15 14:00 编辑

最近看了不少这种类似的代码, 比较复杂,要能快速找到关键点。 有几段视频传输的开源代码写得不错,easyhls , easypusher之类的。
 楼主| keer_zu 发表于 2015-8-16 12:16 | 显示全部楼层
dong_abc 发表于 2015-8-15 13:58
最近看了不少这种类似的代码, 比较复杂,要能快速找到关键点。 有几段视频传输的开源代码写得不错,easyhl ...

像live555,darwin都不错的,好的C++范例
 楼主| keer_zu 发表于 2015-8-17 11:35 | 显示全部楼层
本帖最后由 keer_zu 于 2015-8-17 11:37 编辑

第2段代码头文件,类的定义在里面:@yyy71cj  @dong_abc

  1. #ifndef __CDYCONNBASE_H__
  2. #define __CDYCONNBASE_H__

  3. #include <string>

  4. namespace cdy{

  5. class Connection;
  6. class IConnectorAcceptor;

  7. //User should implemention this interface to get the new connection notify.
  8. class IConnectorAcceptorSink
  9. {
  10. public:
  11.         //@param conn is the new connection if succeed, else null.
  12.         //@ca shows the Connector or Acceptor object which created the new connection.
  13.         virtual void OnConnection(Connection *conn, IConnectorAcceptor *ca) = 0;
  14. };

  15. //User should implemention this interface to get the notification of data incoming
  16. //or the buffer is not full so user can write more data, and the event of connection
  17. //been disconnected.
  18. class IConnectionSink
  19. {
  20. public:
  21.         // called when new data income.
  22.         virtual void OnData(const char *buf, int length, Connection *conn) = 0;
  23.         // called when user can write data to network.
  24.         virtual void OnWrite(Connection *conn) = 0;
  25.         // called when the connection is broken, if user call Connection::Close(),
  26.         // will not get this callback.
  27.         virtual void OnDisconnect(int reason, Connection *conn) = 0;
  28. };

  29. class IConnectorAcceptor
  30. {
  31. protected:
  32.         int m_error;
  33. public:
  34.         IConnectorAcceptor() : m_error(0) {}
  35.         virtual ~IConnectorAcceptor() {}
  36.         
  37.         virtual bool IsConnector() = 0;

  38.         virtual int GetError() { return m_error; }
  39. };

  40. class Connector : public IConnectorAcceptor
  41. {
  42. public:
  43.         Connector(const std::string &host, short port, IConnectorAcceptorSink *sink);

  44.         ~Connector();
  45.         
  46.         //@param timeout means the connect timeout value in millesecond.
  47.         int Connect(long timeout);
  48.         
  49.         void Cancel();
  50.         
  51.         bool IsConnector() { return true; }
  52. private:
  53.         class ConnectorImp *m_imp;
  54. };

  55. class Acceptor : public IConnectorAcceptor
  56. {
  57. public:
  58.         Acceptor(const std::string &addr, short port, IConnectorAcceptorSink *sink);

  59.         ~Acceptor();
  60.         
  61.         int StartListen();
  62.         
  63.         void Stop();
  64.         
  65.         virtual bool IsConnector() { return false; }
  66. private:
  67.         class AcceptorImp *m_imp;
  68. };

  69. class Connection
  70. {
  71. public:
  72.         virtual ~Connection();
  73.         
  74.         virtual void SetConnectionSink(IConnectionSink *sink) = 0;
  75.         
  76.         virtual int Send(const char *data, int len) = 0;
  77.         
  78.         virtual void Close() = 0;

  79.         virtual const std::string& GetPeerAddress() = 0;

  80.         virtual short GetPeerPort() = 0;
  81. };

  82. };
  83. #endif //__CDYCONNBASE_H__


 楼主| keer_zu 发表于 2015-8-17 11:38 | 显示全部楼层
第三段代码头文件:@dong_abc @yyy71cj
  1. #ifndef __INTERFACEAPIIMP_H__
  2. #define __INTERFACEAPIIMP_H__

  3. //#include <string>
  4. #include "conn_base.h"
  5. //#include "SessionBase.h"
  6. #include "event_looper.h"
  7. #include "interface_api.h"
  8. #include <vector>
  9. #include <list>
  10. #include <map>
  11. #include <stdarg.h>

  12. #include <log4cxx/logger.h>
  13. #include <log4cxx/logstring.h>
  14. #include <log4cxx/propertyconfigurator.h>
  15. #include <log4cxx/helpers/exception.h>  

  16. using namespace cdy;
  17. using namespace std;
  18. using namespace log4cxx;

  19. class SessionImp;



  20. typedef enum
  21. {
  22.         SESSION_TS = 0,
  23.                
  24.         SESSION_APP_SERVER = 1,

  25.         SESSION_APP_CLIENT = 2

  26. } SessionType;



  27. class SessionImp : public IConnectionSink
  28. {
  29. public:
  30.         //SessionImp(ITsSessionUser *sessionUser,TsSession *tssession);

  31.         virtual void SendMessage(TsMessageBase &msg){}

  32.         virtual void SendMessage(AppMessageBase &msg){}

  33.         void SendMessage(string &str);
  34.         
  35.         virtual void Close() = 0;

  36. //        virtual void SetSessionType() = 0;

  37.         virtual void SetConnection(Connection *connection) = 0;

  38. protected:
  39.         void ShowReadBuf();

  40.         void ProcessFrame(const char *buf, int length);

  41.         virtual bool OnFrame(string &frame) = 0;
  42.         
  43.         Connection *m_connection;
  44.         
  45.         SessionType m_sessionType;
  46.         
  47.         vector<char> m_rdBuffer; //initial size set 1024.
  48.         
  49.         long m_idleTime;

  50.         TimerID m_timeout;

  51.         friend class SessionManagerImp;
  52. };


  53. class TsSessionImp : public SessionImp
  54. {
  55. public:
  56.         TsSessionImp(ITsSessionUser *sessionUser,TsSession *tssession);
  57.         
  58.         void SetSessionUser(ITsSessionUser *sessionUser);
  59.         
  60.         virtual void SendMessage(TsMessageBase &msg);

  61.         virtual void Close();

  62. //        virtual void SetSessionType(){m_sessionType = SESSION_TS;}
  63.         
  64.         virtual void SetConnection(Connection *connection){m_connection = connection;}
  65. protected:
  66.         virtual void OnData(const char *buf, int length, Connection *conn);
  67.         // called when user can write data to network.
  68.         virtual void OnWrite(Connection *conn);
  69.         // called when the connection is broken, if user call Connection::Close(), //&&&
  70.         // will not get this callback.
  71.         virtual void OnDisconnect(int reason, Connection *conn);  //

  72.         virtual bool OnFrame(string &frame);
  73. private:
  74.         ITsSessionUser *m_sessionUser;

  75.         class TsSession* m_tssession;

  76. };

  77. class AppServerSessionImp : public SessionImp
  78. {
  79. public:
  80.         AppServerSessionImp(Connection *connection,IAppEventListener *event_listener)
  81.                 : m_eventListener(event_listener)
  82.         {
  83.                 m_connection = connection;
  84.                 m_sessionType = SESSION_APP_SERVER;
  85.         }
  86.         
  87.         void SetEventListener(IAppEventListener *eventListener);
  88.         
  89.         virtual void SendMessage(AppMessageBase &msg);

  90.         virtual void Close();

  91.         //virtual void SetSessionType(){m_sessionType = SESSION_APP_LISTEN;}

  92.         virtual void SetConnection(Connection *connection){m_connection = connection;}
  93. protected:
  94.         virtual void OnData(const char *buf, int length, Connection *conn);
  95.         // called when user can write data to network.
  96.         virtual void OnWrite(Connection *conn);
  97.         // called when the connection is broken, if user call Connection::Close(), //&&&
  98.         // will not get this callback.
  99.         virtual void OnDisconnect(int reason, Connection *conn);  //

  100.         virtual bool OnFrame(string &frame);
  101. private:
  102.         IAppEventListener *m_eventListener;

  103. };


  104. class AppClientSessionImp : public SessionImp
  105. {
  106. public:
  107.         AppClientSessionImp(Connection *connection)
  108.         {
  109.                 if(connection != NULL)
  110.                         m_connection = connection;
  111.                 m_sessionType = SESSION_APP_CLIENT;
  112.         }
  113.         virtual void SendMessage(AppMessageBase &msg);

  114.         virtual void Close();

  115.         //virtual void SetSessionType(){m_sessionType = SESSION_APP_CONNECT;}

  116.         virtual void SetConnection(Connection *connection){m_connection = connection;}
  117. protected:
  118.         virtual void OnData(const char *buf, int length, Connection *conn);
  119.         // called when user can write data to network.
  120.         virtual void OnWrite(Connection *conn);
  121.         // called when the connection is broken, if user call Connection::Close(), //&&&
  122.         // will not get this callback.
  123.         virtual void OnDisconnect(int reason, Connection *conn);  //

  124.         virtual bool OnFrame(string &frame);
  125. private:

  126. };




  127. struct TsSessionInfo{
  128.         TsSession *session;
  129.         bool timeOut;
  130. };


  131. class SessionManagerImp : public IConnectorAcceptorSink, public ITimerUserSink
  132. {
  133.         //SessionManager &sessionmanager;
  134.         SessionManagerImp(/*SessionManager &wrapper*/);
  135.         
  136. public:
  137.         
  138.         ~SessionManagerImp();
  139. //-------- for test ----------------        
  140.         void ShowTsSessionList();
  141.         void ShowConnectors();
  142.         void ShowAppServerSessionList();
  143.         void ShowAppClientSessionList();
  144. //-------------------------------


  145.          static SessionManagerImp& GetInstance();

  146.         bool StartListenForAppServer(IAppEventListener &listener);

  147.         bool StartListenForTS(ITsSessionUser &sessionUser);

  148.         void SendAppMessage(AppMessageBase &appMessage);

  149.         void OnNewSession(SessionImp &session);

  150.         void OnNewSession(TsSession &session);

  151.         void OnSessionClose(SessionImp &session);

  152.         void OnSessionClose(TsSession &session);

  153.         IAppEventListener* GetAppEventListener() const;
  154.         ITsSessionUser* GetSessionUser() const;

  155.         void NewTimer(unsigned int dwInterval,TimerHandler pTimerHander );
  156.         
  157.         void KillTimer();        
  158. protected:
  159.         virtual void OnConnection(Connection *conn, IConnectorAcceptor *ca);
  160.         virtual void OnTimer(TimerID tid);
  161. private:
  162.         
  163.         IAppEventListener *m_appEventListener;
  164.         ITsSessionUser *m_sessionUser;

  165.         Acceptor *m_appListener;
  166.         Acceptor *m_tsListener;
  167.         
  168.         //list<SessionImp*> m_sessions;
  169.         struct ConnectorInfo{
  170.                 //AppMessageBase *msg;
  171.                 string msg;
  172.                 unsigned int re_connect_times;
  173.         };

  174.         map<IConnectorAcceptor*,struct ConnectorInfo *> m_connectors;

  175.         typedef list<struct TsSessionInfo*>TsSessionList;

  176.         TsSessionList m_tsSessionList;

  177.         typedef list<SessionImp*> SessionImpList;
  178.         
  179.         SessionImpList m_appServerSessionList;

  180.         SessionImpList m_appClientSessionList;

  181.         
  182.         //timer
  183.         TimerID m_idleConnectionChecker;

  184.         TimerID m_connectionTimeout;

  185.         TimerID m_timerForApp;

  186.         TimerHandler m_timerHandler;

  187.         friend class TsSessionImp;

  188. };

  189. typedef enum{
  190.         DISCON_RECV_RESPONSE = 0,
  191.         DISCON_APP_CLOSE = 1
  192. }DisconnectReason;

  193. #if 0
  194. #define         BuildMsgVa(m_message,strFormat) \
  195. { \
  196.         int ret; \
  197.         va_list ap; \
  198.         va_start(ap, strFormat); \
  199.         ret = vsprintf(m_message, strFormat, ap); \
  200.         if(ret < 0){ \
  201.                 return 0; \
  202.         } \
  203. }

  204. class LoggerUser{
  205.         LoggerPtr m_logger;
  206.         char m_message[300];
  207.         LoggerUser()
  208.         {
  209.                 memset(m_message,0,300);
  210.                 PropertyConfigurator::configure("log4cxx.cfg");
  211.                 m_logger = Logger::getLogger("");
  212.         }
  213. public:
  214.         static LoggerUser& GetInstance()
  215.         {
  216.                 static LoggerUser instance_;
  217.                 return instance_;
  218.         }
  219.         bool Trace(const char * strFormat,...)
  220.         {/*
  221.                 BuildMsgVa(m_message,strFormat);
  222.                 LOG4CXX_TRACE(m_logger, m_message);
  223.                 */
  224.                 return 1;
  225.         }
  226.         bool Warn(char *strFormat,...)
  227.         {/*
  228.                 BuildMsgVa(m_message,strFormat);
  229.                 LOG4CXX_WARN(m_logger, m_message);
  230.                 */
  231.                 return 1;
  232.         }
  233.         bool Debug(char *strFormat,...)
  234.         {/*
  235.                 BuildMsgVa(m_message,strFormat);
  236.                 LOG4CXX_DEBUG(m_logger, m_message);
  237.                 */
  238.                 return 1;
  239.         }
  240.         bool Fatal(char *strFormat,...)
  241.         {/*
  242.                 BuildMsgVa(m_message,strFormat);
  243.                 LOG4CXX_FATAL(m_logger, m_message);
  244.                 */
  245.                 return 1;
  246.         }

  247.         bool Error(char *strFormat,...)
  248.         {/*
  249.                 BuildMsgVa(m_message,strFormat);
  250.                 LOG4CXX_ERROR(m_logger, m_message);
  251.                 */
  252.                 return 1;
  253.         }
  254. };
  255. #endif

  256. #endif





dong_abc 发表于 2015-8-17 22:22 | 显示全部楼层
像live555,darwin这种太过复杂包了一层又一层,看得吐血。
dong_abc 发表于 2015-8-17 22:41 | 显示全部楼层
yyy71cj 发表于 2015-8-17 22:33
我也有一种很害怕的感觉……

虽然复杂,但是很好用啊,海量的代码比较完善,基本运用只需找到输入输出,继承下来就可以重载自己的应用。
 楼主| keer_zu 发表于 2015-8-25 09:42 | 显示全部楼层
yyy71cj 发表于 2015-8-18 08:01
我没用过这方面的东西,也没精力来分析这方面的东西,所以看了之后总要问一个问题:这里是做啥的?
问了 ...

在libevent上,做了封装,然后实现了会话层和应用。
guojun97 发表于 2015-8-30 09:58 | 显示全部楼层
好长,看了半天
 楼主| keer_zu 发表于 2015-9-2 09:37 | 显示全部楼层
dong_abc 发表于 2015-8-17 22:41
虽然复杂,但是很好用啊,海量的代码比较完善,基本运用只需找到输入输出,继承下来就可以重载自己的应用 ...

@yyy71cj 我发现好多流媒体开源软件基本都倾向用c++,只要稍微大一点的软件都是这样(linux内核除外)。小一点的还是C多一点,比如:libosip libevent 等
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 在线客服 返回列表 返回顶部