File: ipc.c

package info (click to toggle)
vdr-plugin-console 0.6.0-25
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 400 kB
  • ctags: 373
  • sloc: ansic: 7,282; sh: 143; makefile: 99
file content (191 lines) | stat: -rw-r--r-- 2,825 bytes parent folder | download | duplicates (2)
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
/*
 * ipc.c: A plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id$
 */


#include <errno.h>
#include <signal.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>

#include <vdr/tools.h>

#include "ipc.h"





// --- cConsSimplePipe -------------------------------------------------------



cConsSimplePipe::cConsSimplePipe() {
  _pipe[0] = -1;
  _pipe[1] = -1;
}



cConsSimplePipe::~cConsSimplePipe() {
  Close();
}



bool cConsSimplePipe::Open() {

  if (IsOpen())
    return true;

  return (pipe(_pipe) == 0);
}



void cConsSimplePipe::Close() {

  for (int i = 0; i < 2; ++i) {

    if (_pipe[i] >= 0) {
      close(_pipe[i] );
      _pipe[i] = -1;
    }
  }
}







// --- cConsWaitableList -----------------------------------------------------



cConsWaitableList::cConsWaitableList()
: _list(NULL),
  _listCount(0)
{
  FD_ZERO(&_wait);
}



cConsWaitableList::~cConsWaitableList() {
  free(_list);
}



void cConsWaitableList::Add(IConsWaitable* pObj) {

  for (int i = 0; i < _listCount; ++i) {
    if (_list[i] == pObj)
      return;
  }

  _list = (IConsWaitable**) realloc((void*) _list, sizeof(IConsWaitable*) * ++_listCount);
  _list[_listCount - 1] = pObj;
}



void cConsWaitableList::Remove(IConsWaitable* pObj) {

  for (int i = 0; i < _listCount; ++i) {
    if (_list[i] == pObj) {
      // Object found
      for (int j = i + 1; j < _listCount; ++j) {
        _list[j - 1] = _list[j];
      }
      _list = (IConsWaitable**) realloc((void*) _list, sizeof(IConsWaitable*) * --_listCount);
      return;
    }
  }

  // object not found
}



bool cConsWaitableList::Wait(int timeoutMs) {

  int maxHandle = 0;

  FD_ZERO(&_wait);
  for (int i = 0; i < _listCount; ++i) {

    int handle = _list[i]->SignalToWaitFor();
    if (handle >= 0) {

      FD_SET(handle, &_wait);

      if (handle > maxHandle)
        maxHandle = handle;
    }
  }

  if (timeoutMs >= 0) {
    timeval timeout;
    timeout.tv_sec  = timeoutMs / 1000;
    timeout.tv_usec = (timeoutMs % 1000) * 1000;

    return (select(maxHandle + 1, &_wait, NULL, NULL, &timeout) > 0);

  } else {
    return (select(maxHandle + 1, &_wait, NULL, NULL, NULL) > 0);
  }
}







// --- cConsSignal -----------------------------------------------------------



cConsSignal::cConsSignal() {
  Open();
}



void cConsSignal::Signal() {

  // If the pipe is empty then put a char into the pipe
  // so that the receiver will be signalled.
  if (! IsSignalled())
    write(getWriter(), " ", 1);
}



void cConsSignal::Reset() {

  // remove the pending char from the pipe
  char buf;

  if (IsSignalled())
    read(getReader(), &buf, 1);
}



bool cConsSignal::IsSignalled() {

  cPoller poll(getReader());
  return poll.Poll(0);
}