1 /* GStreamer
2  * Copyright (C) <2009> Collabora Ltd
3  *  @author: Olivier Crete <olivier.crete@collabora.co.uk
4  * Copyright (C) <2009> Nokia Inc
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 /*
26  * None of this code is thread safe, if you want to use it in a
27  * multi-threaded context, please protect it with a mutex.
28  *
29  * First, create a writer with sp_writer_create(), then select() on
30  * the socket returned by sp_get_fd(). If the socket is closed or any
31  * function returns an error, the app should call sp_close() and
32  * assume the other side is dead. The writer calls
33  * sp_writer_accept_client() when there is something to read from the
34  * main server fd. This returns a new ShmClient (representing a client
35  * connection), the writer needs to do a select() on the socket
36  * returned by sp_writer_get_client_fd(). If it gets an error on that
37  * socket, it calls sp_writer_close_client(). If there is something to
38  * read, it calls sp_writer_recv().
39  *
40  * The writer allocates a block containing a free buffer with
41  * sp_writer_alloc_block(), then writes something in the buffer
42  * (retrieved with sp_writer_block_get_buf(), then calls
43  * sp_writer_send_buf() to send the buffer or a subsection to the
44  * other side. When it is done with the block, it calls
45  * sp_writer_free_block().  If alloc fails, then the server must wait
46  * for events on the client fd (the ones where sp_writer_recv() is
47  * called), and then try to re-alloc.
48  *
49  * The reader (client) connect to the writer with sp_client_open() And
50  * select()s on the fd from sp_get_fd() until there is something to
51  * read.  Then they must read using sp_client_recv() which will return
52  * the size of the buffer (positive) if there is a valid buffer (which
53  * is read only).  It will return 0 if it is an internal message and a
54  * negative number if there was an error.  If there was an error, the
55  * application must close the pipe with sp_close() and assume that all
56  * buffers are no longer valid. If was valid buffer was received, the
57  * client must release it with sp_client_recv_finish() when it is done
58  * reading from it.
59  */
60 
61 
62 #ifndef __SHMPIPE_H__
63 #define __SHMPIPE_H__
64 
65 #include <stdlib.h>
66 #include <stdint.h>
67 #include <sys/types.h>
68 #include <sys/stat.h>
69 #include <fcntl.h>
70 
71 
72 #ifdef __cplusplus
73 extern "C" {
74 #endif
75 
76 typedef struct _ShmClient ShmClient;
77 typedef struct _ShmPipe ShmPipe;
78 typedef struct _ShmBlock ShmBlock;
79 typedef struct _ShmBuffer ShmBuffer;
80 
81 typedef void (*sp_buffer_free_callback) (void * tag, void * user_data);
82 
83 ShmPipe *sp_writer_create (const char *path, size_t size, mode_t perms);
84 const char *sp_writer_get_path (ShmPipe *pipe);
85 void sp_writer_close (ShmPipe * self, sp_buffer_free_callback callback,
86     void * user_data);
87 void *sp_get_data (ShmPipe * self);
88 void sp_set_data (ShmPipe * self, void *data);
89 
90 int sp_writer_setperms_shm (ShmPipe * self, mode_t perms);
91 int sp_writer_resize (ShmPipe * self, size_t size);
92 
93 int sp_get_fd (ShmPipe * self);
94 const char *sp_get_shm_area_name (ShmPipe *self);
95 int sp_writer_get_client_fd (ShmClient * client);
96 
97 ShmBlock *sp_writer_alloc_block (ShmPipe * self, size_t size);
98 void sp_writer_free_block (ShmBlock *block);
99 int sp_writer_send_buf (ShmPipe * self, char *buf, size_t size, void * tag);
100 char *sp_writer_block_get_buf (ShmBlock *block);
101 ShmPipe *sp_writer_block_get_pipe (ShmBlock *block);
102 size_t sp_writer_get_max_buf_size (ShmPipe * self);
103 
104 ShmClient * sp_writer_accept_client (ShmPipe * self);
105 void sp_writer_close_client (ShmPipe *self, ShmClient * client,
106     sp_buffer_free_callback callback, void * user_data);
107 int sp_writer_recv (ShmPipe * self, ShmClient * client, void ** tag);
108 
109 int sp_writer_pending_writes (ShmPipe * self);
110 
111 ShmBuffer *sp_writer_get_pending_buffers (ShmPipe * self);
112 ShmBuffer *sp_writer_get_next_buffer (ShmBuffer * buffer);
113 void *sp_writer_buf_get_tag (ShmBuffer * buffer);
114 
115 ShmPipe *sp_client_open (const char *path);
116 long int sp_client_recv (ShmPipe * self, char **buf);
117 int sp_client_recv_finish (ShmPipe * self, char *buf);
118 void sp_client_close (ShmPipe * self);
119 
120 #ifdef __cplusplus
121 }
122 #endif
123 
124 #endif /* __SHMPIPE_H__ */
125