/src/ftk_pipe_socket.c
C | 103 lines | 54 code | 20 blank | 29 comment | 12 complexity | 8e8cf7a3772b3b9cf5ac6f9ba0e2959c MD5 | raw file
1/* 2 * File: ftk_pipe.c 3 * Author: Li XianJing <xianjimli@hotmail.com> 4 * Brief: pipe 5 * 6 * Copyright (c) 2009 - 2010 Li XianJing <xianjimli@hotmail.com> 7 * 8 * Licensed under the Academic Free License version 2.1 9 * 10 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License as published by 12 * the Free Software Foundation; either version 2 of the License, or 13 * (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with this program; if not, write to the Free Software 22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 */ 24 25/* 26 * History: 27 * ================================================================ 28 * 2010-01-20 Li XianJing <xianjimli@hotmail.com> created 29 * 30 */ 31 32#include "ftk_log.h" 33#include "ftk_pipe.h" 34#include "ftk_allocator.h" 35 36struct _FtkPipe 37{ 38 int read_fd; 39 int write_fd; 40}; 41 42static Ret make_sock_pipe(int pipes[2]) 43{ 44 int pipe_ret = 0; 45 46 pipe_ret = ftk_pipe_pair(pipes); 47 48 return pipe_ret ? RET_FAIL : RET_OK; 49} 50 51FtkPipe* ftk_pipe_create(void) 52{ 53 int pipes[2] = {0}; 54 FtkPipe* thiz = FTK_NEW(FtkPipe); 55 56 if(thiz != NULL) 57 { 58 make_sock_pipe(pipes); 59 thiz->read_fd = pipes[0]; 60 thiz->write_fd = pipes[1]; 61 } 62 63 return thiz; 64} 65 66int ftk_pipe_read(FtkPipe* thiz, void* buff, size_t length) 67{ 68 return_val_if_fail(thiz != NULL && buff != NULL, -1); 69 70 return ftk_pipe_recv(thiz->read_fd, buff, length); 71} 72 73int ftk_pipe_write(FtkPipe* thiz, const void* buff, size_t length) 74{ 75 return_val_if_fail(thiz != NULL && buff != NULL, -1); 76 77 return ftk_pipe_send(thiz->write_fd, buff, length); 78} 79 80int ftk_pipe_get_read_handle(FtkPipe* thiz) 81{ 82 return thiz != NULL ? thiz->read_fd : -1; 83} 84 85int ftk_pipe_get_write_handle(FtkPipe* thiz) 86{ 87 return thiz != NULL ? thiz->write_fd : -1; 88} 89 90void ftk_pipe_destroy(FtkPipe* thiz) 91{ 92 if(thiz != NULL) 93 { 94 ftk_pipe_close(thiz->read_fd); 95 ftk_pipe_close(thiz->write_fd); 96 97 FTK_ZFREE(thiz, sizeof(*thiz)); 98 } 99 100 return; 101} 102 103