~ubuntu-branches/ubuntu/raring/freerdp/raring

« back to all changes in this revision

Viewing changes to channels/common/wait_obj.c

  • Committer: Bazaar Package Importer
  • Author(s): Otavio Salvador
  • Date: 2010-06-23 21:39:09 UTC
  • Revision ID: james.westby@ubuntu.com-20100623213909-bb9pvvv03913tdv6
Tags: upstream-0.7.1
ImportĀ upstreamĀ versionĀ 0.7.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   Copyright (c) 2009-2010 Jay Sorg
 
3
 
 
4
   Permission is hereby granted, free of charge, to any person obtaining a
 
5
   copy of this software and associated documentation files (the "Software"),
 
6
   to deal in the Software without restriction, including without limitation
 
7
   the rights to use, copy, modify, merge, publish, distribute, sublicense,
 
8
   and/or sell copies of the Software, and to permit persons to whom the
 
9
   Software is furnished to do so, subject to the following conditions:
 
10
 
 
11
   The above copyright notice and this permission notice shall be included
 
12
   in all copies or substantial portions of the Software.
 
13
 
 
14
   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 
15
   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
16
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
17
   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
18
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 
19
   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 
20
   DEALINGS IN THE SOFTWARE.
 
21
 
 
22
*/
 
23
 
 
24
#include <stdio.h>
 
25
#include <stdlib.h>
 
26
#include <string.h>
 
27
#include <netdb.h>
 
28
#include <unistd.h>
 
29
#include "wait_obj.h"
 
30
 
 
31
#define LOG_LEVEL 1
 
32
#define LLOG(_level, _args) \
 
33
  do { if (_level < LOG_LEVEL) { printf _args ; } } while (0)
 
34
#define LLOGLN(_level, _args) \
 
35
  do { if (_level < LOG_LEVEL) { printf _args ; printf("\n"); } } while (0)
 
36
 
 
37
struct wait_obj
 
38
{
 
39
        int pipe_fd[2];
 
40
};
 
41
 
 
42
struct wait_obj *
 
43
wait_obj_new(const char * name)
 
44
{
 
45
        struct wait_obj * obj;
 
46
 
 
47
        obj = (struct wait_obj *) malloc(sizeof(struct wait_obj));
 
48
 
 
49
        obj->pipe_fd[0] = -1;
 
50
        obj->pipe_fd[1] = -1;
 
51
        if (pipe(obj->pipe_fd) < 0)
 
52
        {
 
53
                LLOGLN(0, ("init_wait_obj: pipe failed"));
 
54
                free(obj);
 
55
                return NULL;
 
56
        }
 
57
        return obj;
 
58
}
 
59
 
 
60
int
 
61
wait_obj_free(struct wait_obj * obj)
 
62
{
 
63
        if (obj)
 
64
        {
 
65
                if (obj->pipe_fd[0] != -1)
 
66
                {
 
67
                        close(obj->pipe_fd[0]);
 
68
                        obj->pipe_fd[0] = -1;
 
69
                }
 
70
                if (obj->pipe_fd[1] != -1)
 
71
                {
 
72
                        close(obj->pipe_fd[1]);
 
73
                        obj->pipe_fd[1] = -1;
 
74
                }
 
75
                free(obj);
 
76
        }
 
77
        return 0;
 
78
}
 
79
 
 
80
int
 
81
wait_obj_is_set(struct wait_obj * obj)
 
82
{
 
83
        fd_set rfds;
 
84
        int num_set;
 
85
        struct timeval time;
 
86
 
 
87
        FD_ZERO(&rfds);
 
88
        FD_SET(obj->pipe_fd[0], &rfds);
 
89
        memset(&time, 0, sizeof(time));
 
90
        num_set = select(obj->pipe_fd[0] + 1, &rfds, 0, 0, &time);
 
91
        return (num_set == 1);
 
92
}
 
93
 
 
94
int
 
95
wait_obj_set(struct wait_obj * obj)
 
96
{
 
97
        int len;
 
98
 
 
99
        if (wait_obj_is_set(obj))
 
100
        {
 
101
                return 0;
 
102
        }
 
103
        len = write(obj->pipe_fd[1], "sig", 4);
 
104
        if (len != 4)
 
105
        {
 
106
                LLOGLN(0, ("set_wait_obj: error"));
 
107
                return 1;
 
108
        }
 
109
        return 0;
 
110
}
 
111
 
 
112
int
 
113
wait_obj_clear(struct wait_obj * obj)
 
114
{
 
115
        int len;
 
116
 
 
117
        while (wait_obj_is_set(obj))
 
118
        {
 
119
                len = read(obj->pipe_fd[0], &len, 4);
 
120
                if (len != 4)
 
121
                {
 
122
                        LLOGLN(0, ("chan_man_clear_ev: error"));
 
123
                        return 1;
 
124
                }
 
125
        }
 
126
        return 0;
 
127
}
 
128
 
 
129
int
 
130
wait_obj_select(struct wait_obj ** listobj, int numobj, int * listr, int numr,
 
131
        int timeout)
 
132
{
 
133
        int max;
 
134
        int rv;
 
135
        int index;
 
136
        int sock;
 
137
        struct timeval time;
 
138
        struct timeval * ptime;
 
139
        fd_set fds;
 
140
 
 
141
        ptime = 0;
 
142
        if (timeout >= 0)
 
143
        {
 
144
                time.tv_sec = timeout / 1000;
 
145
                time.tv_usec = (timeout * 1000) % 1000000;
 
146
                ptime = &time;
 
147
        }
 
148
        max = 0;
 
149
        FD_ZERO(&fds);
 
150
        if (listobj)
 
151
        {
 
152
                for (index = 0; index < numobj; index++)
 
153
                {
 
154
                        sock = listobj[index]->pipe_fd[0];
 
155
                        FD_SET(sock, &fds);
 
156
                        if (sock > max)
 
157
                        {
 
158
                                max = sock;
 
159
                        }
 
160
                }
 
161
        }
 
162
        if (listr)
 
163
        {
 
164
                for (index = 0; index < numr; index++)
 
165
                {
 
166
                        sock = listr[index];
 
167
                        FD_SET(sock, &fds);
 
168
                        if (sock > max)
 
169
                        {
 
170
                                max = sock;
 
171
                        }
 
172
                }
 
173
        }
 
174
        rv = select(max + 1, &fds, 0, 0, ptime);
 
175
        return rv;
 
176
}
 
177