CPPMyth
Library to interoperate with MythTV server
mythprotobase.h
1 /*
2  * Copyright (C) 2014 Jean-Luc Barriere
3  *
4  * This Program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2, or (at your option)
7  * any later version.
8  *
9  * This Program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; see the file COPYING. If not, write to
16  * the Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
17  * MA 02110-1301 USA
18  * http://www.gnu.org/copyleft/gpl.html
19  *
20  */
21 
22 #ifndef MYTHPROTOBASE_H
23 #define MYTHPROTOBASE_H
24 
25 #include "../mythtypes.h"
26 
27 #include <string>
28 
29 #define PROTO_BUFFER_SIZE 4000
30 #define PROTO_SENDMSG_MAXSIZE 64000
31 #define PROTO_STR_SEPARATOR "[]:[]"
32 #define PROTO_STR_SEPARATOR_LEN (sizeof(PROTO_STR_SEPARATOR) - 1)
33 
34 namespace Myth
35 {
36 
37  namespace OS
38  {
39  class CMutex;
40  }
41 
42  class TcpSocket;
43 
44  class ProtoBase
45  {
46  public:
47  ProtoBase(const std::string& server, unsigned port);
48  virtual ~ProtoBase();
49 
50  typedef enum
51  {
52  ERROR_NO_ERROR = 0,
53  ERROR_SERVER_UNREACHABLE,
54  ERROR_SOCKET_ERROR,
55  ERROR_UNKNOWN_VERSION,
56  } ERROR_t;
57 
58  virtual bool Open() = 0;
59  virtual void Close();
60  virtual bool IsOpen() { return m_isOpen; }
61  virtual unsigned GetProtoVersion() const;
62  virtual std::string GetServer() const;
63  virtual unsigned GetPort() const;
64  virtual int GetSocketErrNo() const;
65  virtual int GetSocket() const;
66  virtual bool HasHanging() const;
67  virtual void CleanHanging();
68  virtual ERROR_t GetProtoError() const;
69 
70  protected:
71  OS::CMutex *m_mutex;
72  TcpSocket *m_socket;
73  unsigned m_protoVersion;
74  std::string m_server;
75  unsigned m_port;
76  bool m_hang;
77  bool m_tainted;
78  size_t m_msgLength;
79  size_t m_msgConsumed;
80 
81  bool OpenConnection(int rcvbuf);
82  void HangException();
83  bool SendCommand(const char *cmd, bool feedback = true);
84  size_t GetMessageLength() const;
85  bool ReadField(std::string& field);
86  bool IsMessageOK(const std::string& field) const;
87  size_t FlushMessage();
88  bool RcvMessageLength();
89 
90  ProgramPtr RcvProgramInfo()
91  {
92  if (m_protoVersion >= 86) return RcvProgramInfo86();
93  if (m_protoVersion >= 82) return RcvProgramInfo82();
94  if (m_protoVersion >= 79) return RcvProgramInfo79();
95  if (m_protoVersion >= 76) return RcvProgramInfo76();
96  return RcvProgramInfo75();
97  }
98 
99  void MakeProgramInfo(const Program& program, std::string& msg)
100  {
101  if (m_protoVersion >= 86) MakeProgramInfo86(program, msg);
102  else if (m_protoVersion >= 82) MakeProgramInfo82(program, msg);
103  else if (m_protoVersion >= 79) MakeProgramInfo79(program, msg);
104  else if (m_protoVersion >= 76) MakeProgramInfo76(program, msg);
105  else MakeProgramInfo75(program, msg);
106  }
107 
108  private:
109  bool m_isOpen;
110  ERROR_t m_protoError;
111 
112  bool RcvVersion(unsigned *version);
113 
114  ProgramPtr RcvProgramInfo75();
115  ProgramPtr RcvProgramInfo76();
116  ProgramPtr RcvProgramInfo79();
117  ProgramPtr RcvProgramInfo82();
118  ProgramPtr RcvProgramInfo86();
119  void MakeProgramInfo75(const Program& program, std::string& msg);
120  void MakeProgramInfo76(const Program& program, std::string& msg);
121  void MakeProgramInfo79(const Program& program, std::string& msg);
122  void MakeProgramInfo82(const Program& program, std::string& msg);
123  void MakeProgramInfo86(const Program& program, std::string& msg);
124 
125  };
126 
127 }
128 
129 #endif /* MYTHPROTOBASE_H */
bool m_hang
Connection hang: while true allow retry.
Definition: mythprotobase.h:76
This is the main namespace that encloses all public classes.
Definition: mythcontrol.h:29
bool m_tainted
Connection has hung since last reset.
Definition: mythprotobase.h:77