2 * FreeRDP: A Remote Desktop Protocol client.
3 * Virtual Channel Manager
5 * Copyright 2009-2011 Jay Sorg
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
24 #include <freerdp/utils/memory.h>
25 #include <freerdp/utils/wait_obj.h>
52 obj = xnew(struct wait_obj);
56 obj->event = CreateEvent(NULL, TRUE, FALSE, NULL);
60 if (pipe(obj->pipe_fd) < 0)
62 printf("wait_obj_new: pipe failed\n");
71 struct wait_obj* wait_obj_new_with_fd(void* fd)
75 obj = xnew(struct wait_obj);
81 obj->pipe_fd[0] = (int)(long)fd;
89 wait_obj_free(struct wait_obj* obj)
94 if (obj->attached == 0)
99 CloseHandle(obj->event);
103 if (obj->pipe_fd[0] != -1)
105 close(obj->pipe_fd[0]);
106 obj->pipe_fd[0] = -1;
108 if (obj->pipe_fd[1] != -1)
110 close(obj->pipe_fd[1]);
111 obj->pipe_fd[1] = -1;
121 wait_obj_is_set(struct wait_obj* obj)
124 return (WaitForSingleObject(obj->event, 0) == WAIT_OBJECT_0);
131 FD_SET(obj->pipe_fd[0], &rfds);
132 memset(&time, 0, sizeof(time));
133 num_set = select(obj->pipe_fd[0] + 1, &rfds, 0, 0, &time);
134 return (num_set == 1);
139 wait_obj_set(struct wait_obj* obj)
142 SetEvent(obj->event);
146 if (wait_obj_is_set(obj))
148 len = write(obj->pipe_fd[1], "sig", 4);
150 printf("wait_obj_set: error\n");
155 wait_obj_clear(struct wait_obj* obj)
158 ResetEvent(obj->event);
162 while (wait_obj_is_set(obj))
164 len = read(obj->pipe_fd[0], &len, 4);
166 printf("wait_obj_clear: error\n");
172 wait_obj_select(struct wait_obj** listobj, int numobj, int timeout)
182 struct timeval* ptime;
187 time.tv_sec = timeout / 1000;
188 time.tv_usec = (timeout * 1000) % 1000000;
197 for (index = 0; index < numobj; index++)
199 sock = listobj[index]->pipe_fd[0];
206 status = select(max + 1, &fds, 0, 0, ptime);
208 status = select(0, &fds, 0, 0, ptime);
214 void wait_obj_get_fds(struct wait_obj* obj, void** fds, int* count)
217 fds[*count] = (void*) obj->event;
219 if (obj->pipe_fd[0] == -1)
222 fds[*count] = (void*)(long) obj->pipe_fd[0];