summaryrefslogtreecommitdiff
path: root/plasma/comthread.hpp
blob: ccec05e757e19e4f3602655c3d2e9f70b59aa496 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//***************************************************************************
// Group VDR/GraphTFT
// File graphtft.hpp
// Date 28.10.06 - Jörg Wendel
// This code is distributed under the terms and conditions of the
// GNU GENERAL PUBLIC LICENSE. See the file COPYING for details.
//--------------------------------------------------------------------------
// Class ComThread
// Class TcpChannel
//***************************************************************************

#ifndef __COMTHREAD_HPP__
#define __COMTHREAD_HPP__

#include <QObject>
#include <QThread>
#include <QReadWriteLock>

#define __FRONTEND
#include <../common.h>

#include <common.hpp>
#include <../service.h>

#include <unistd.h>

//***************************************************************************
// TcpChannel
//***************************************************************************

class TcpChannel
{
   public:

      // declarations

      enum Errors
      {
         errChannel = -100,
         
         errUnknownHostname,
         errBindAddressFailed,
         errAcceptFailed,
         errListenFailed,
         errConnectFailed,
         errIOError,
         errConnectionClosed,
         errInvalidEndpoint,
         errOpenEndpointFailed,

         // Warnungen

         wrnNoEventPending,
         errUnexpectedEvent,
         wrnChannelBlocked,
         wrnNoConnectIndication,
         wrnNoResponseFromServer,
         wrnNoDataAvaileble,
         wrnSysInterrupt,
         wrnTimeout
      };

      struct Header
      {
         int command;
         int size;
      };

      // object

      TcpChannel();
      ~TcpChannel();
      
      // api function

      int open(const char* aHost, int aPort);
      int close();
      int look(int aTimeout);
      int read(char* buf, int bufLen);
      int write(int command, const char* buf = 0, int bufLen = 0);
      int isConnected()    { return handle != 0; }

   private:

      int checkErrno();

      int handle;
      unsigned short port;
      char localHost[100];
      char remoteHost[100];
      long localAddr;
      long remoteAddr;
      long timeout;
      int lookAheadChar;
      int lookAhead;
      int nTtlReceived;
      int nTtlSent;
};

//***************************************************************************
// Communication Thread
//***************************************************************************

class ComThread : public QThread, public cGraphTftComService
{
   Q_OBJECT 

   public:
      
      enum Misc
      {
         maxBuffer = 1024*1024
      };

      ComThread();
      virtual ~ComThread();

      void stop()                        { running = false; }
      int mouseEvent(int x, int y, int button, int flag, int data = 0);
      int keyEvent(int key, int flag);

      const char* getBuffer()            { return buffer; }
      int getSize()                      { return header->size; }

      void setHost(const char* aHost)    { strcpy(host, aHost); }
      void setPort(unsigned short aPort) { port = aPort; }
      QReadWriteLock* getBufferLock()    { return &bufferLock; }

   signals:

      void updateImage(unsigned char* buffer, int size);

   protected:
      
      void update(unsigned char* buffer, int size);
      void run();
      int read();

      TcpChannel* line;

      char* buffer;
      int bufferSize;
      QReadWriteLock bufferLock;

      long timeout;
      int running;
      TcpChannel::Header* header;
      unsigned short port;
      char host[100];
};

//***************************************************************************
#endif  // __COMTHREAD_HPP__