106 lines
2.7 KiB
C
106 lines
2.7 KiB
C
/*
|
|
* Copyright (C) 2009-2012 Thomas Preud'homme <thomas.preud-homme@lip6.fr>
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in
|
|
* all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <pthread.h>
|
|
#include <unistd.h>
|
|
#include <fcntl.h>
|
|
#include <stdint.h>
|
|
|
|
/* Non standard include */
|
|
#include <commtech.h>
|
|
#include <specific_comm.h>
|
|
|
|
|
|
void *create_comm_channel(void)
|
|
{
|
|
struct channel *channel;
|
|
int flags;
|
|
|
|
channel = malloc(sizeof(channel));
|
|
if (channel != NULL)
|
|
{
|
|
if (!pipe(channel->pipefd))
|
|
{
|
|
flags = fcntl(channel->pipefd[READ_IDX], F_GETFL);
|
|
fcntl(channel->pipefd[READ_IDX], F_SETFL, flags | O_NONBLOCK);
|
|
return channel;
|
|
}
|
|
else
|
|
free(channel);
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
int end_producer(void *unused __attribute__ ((unused)))
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
int destroy_comm_channel(void *channel)
|
|
{
|
|
free(channel);
|
|
return 0;
|
|
}
|
|
|
|
void *recv_one_data(struct channel *channel)
|
|
{
|
|
void *result, **res_ptr;
|
|
int n;
|
|
unsigned int nb_read;
|
|
|
|
nb_read = 0;
|
|
res_ptr = &result;
|
|
do
|
|
{
|
|
n = read(channel->pipefd[READ_IDX], res_ptr, sizeof(void *));
|
|
if (n > 0)
|
|
{
|
|
nb_read += n;
|
|
res_ptr = (void **) ((uintptr_t) res_ptr + n);
|
|
}
|
|
} while (nb_read < sizeof(void *));
|
|
return result;
|
|
}
|
|
|
|
ssize_t recv_some_data(struct channel *channel, void **buf, size_t count)
|
|
{
|
|
int n, nb_read, nb_bytes;
|
|
|
|
nb_bytes = count * sizeof(void *);
|
|
nb_read = read(channel->pipefd[READ_IDX], buf, nb_bytes);
|
|
if (nb_read <= 0)
|
|
return 0;
|
|
buf = (void **) ((uintptr_t) buf + nb_read);
|
|
while (nb_read % sizeof(void *))
|
|
{
|
|
n = read(channel->pipefd[READ_IDX], buf, sizeof(void *) - (nb_read % sizeof(void *)));
|
|
if (n > 0)
|
|
{
|
|
nb_read += n;
|
|
buf = (void **) ((uintptr_t) buf + n);
|
|
}
|
|
}
|
|
return nb_read / sizeof(void *);
|
|
}
|