summaryrefslogtreecommitdiff
path: root/plasma/comthread.cc
blob: 52ef1b8a2a2220f1f848cfb7f7a4efe7022ff955 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//***************************************************************************
// Group VDR/GraphTFT
// File comthread.cc
// 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
//***************************************************************************

#include <arpa/inet.h>

#include "comthread.hpp"

//***************************************************************************
// Object
//***************************************************************************

ComThread::ComThread()
   : QThread()
{
   line = new TcpChannel();

   bufferSize = maxBuffer;

   buffer = new char[bufferSize+1];
   header = new TcpChannel::Header;

   timeout = 1;
   port = -1;
   *host = 0;
}

ComThread::~ComThread()
{
   if (line->isConnected())
   {
      tell(eloAlways, "Logout from server, closing tcp connection");
      line->write(cGraphTftComService::cmdLogout);
      line->close();
   }

   delete line;
   delete header;
   delete[] buffer;
}

//***************************************************************************
// Run
//***************************************************************************

void ComThread::run()
{
   const int checkTimeout = 2;

   int status;
   time_t lastCheck = time(0);

   running = true;

   while (running)
   {
      if (!line->isConnected())
      {
         tell(eloAlways, "Trying connecting to '%s' at port (%d)", host, port);

         if (line->open(host, port) == success)
            tell(eloAlways, "Connection to '%s' established", host);
         else
            tell(eloAlways, "Connecting to '%s' failed", host);
      }

      while (line->isConnected() && running)
      {
         if (lastCheck+checkTimeout < time(0))
         {
            line->write(cGraphTftComService::cmdCheck);
            lastCheck = time(0);
         }

         if ((status = line->look(1)) != success)
         {
            if (status != TcpChannel::wrnNoEventPending)
            {
               line->close();
               tell(eloAlways, "Error: Communication problems, "
                    "look failed, status was (%d)", status);

               break;
            }
            
            continue;
         }
         
         if ((status = read()) != success)
         {
            line->close();
            tell(eloAlways, "Error: Communication problems, "
                 "read failed, status was (%d)", status);

            break;
         }
      }

      if (!running) break;

      tell(eloAlways, "Retrying in %ld seconds", timeout);

      for (int i = 0; i < timeout && running; i++)
         sleep(1);
   }
}

//***************************************************************************
// Transmit events
//***************************************************************************

int ComThread::mouseEvent(int x, int y, int button, int flag, int data)
{
   GraphTftTouchEvent m;

   m.x = x;
   m.y = y;
   m.button = button;
   m.flag = flag;
   m.data = data;
   line->write(cGraphTftComService::cmdMouseEvent, (char*)&m, sizeof(GraphTftTouchEvent));

   return 0;
}

//***************************************************************************
// ...
//***************************************************************************

int ComThread::keyEvent(int key, int flag)
{
   GraphTftTouchEvent m;

   m.x = 0;
   m.y = 0;
   m.button = key;
   m.flag = flag | efKeyboard;
   line->write(cGraphTftComService::cmdMouseEvent, (char*)&m, sizeof(GraphTftTouchEvent));

   return 0;
}

//***************************************************************************
// Read
//***************************************************************************

int ComThread::read()
{
   int status;
   TcpChannel::Header tmp;

   // es stehen Daten an, erst einmal den Header abholen ..

   if ((status = line->read((char*)&tmp, sizeof(TcpChannel::Header))) == 0)
   {
      header->command = ntohl(tmp.command);
      header->size = ntohl(tmp.size);

      switch (header->command)
      {
         case cGraphTftComService::cmdWelcome:
         {
            tell(eloAlways, "Got welcome");

            break;
         }

         case cGraphTftComService::cmdLogout:
         {
            tell(eloAlways, "Got logout from client, closing line");
            line->close();

            break;
         }

         case cGraphTftComService::cmdData:
         {
            tell(eloDebug, "Debug: Received %d bytes", header->size);
          
            bufferLock.lockForWrite();
            status = line->read(buffer, header->size);
            bufferLock.unlock();

            if (status == success) 
               update((unsigned char*)buffer, header->size);
            
            break;
         }

         case cGraphTftComService::cmdMouseEvent:
         {
            GraphTftTouchEvent ev;

            status = line->read((char*)&ev, header->size);
            tell(eloAlways, "Got mouse event, button (%d) at (%d/%d)", ev.button, ev.x, ev.y); 

            break;
         }

         default:
         {
            tell(eloAlways, "Got unexpected protocol (%d), aborting", header->command); 
            status = -1;

            break;
         }
      }
   }   

   return status;
}

//***************************************************************************
// Update Image
//***************************************************************************

void ComThread::update(unsigned char* buffer, int size)
{
   emit updateImage(buffer, size);
}