libamxp  1.4.0
Patterns C Implementation
Connection management

Functions

int amxp_connection_add (int fd, amxp_fd_cb_t reader, const char *uri, uint32_t type, void *priv)
 Adds a file descriptor (fd) to the list of fds that must be watched. More...
 
int amxp_connection_wait_write (int fd, amxp_fd_cb_t can_write_cb)
 Adds a watcher to check if a fd is ready for write. More...
 
int amxp_connection_remove (int fd)
 Removes the fd from the connection list. More...
 
amxp_connection_tamxp_connection_get (int fd)
 Gets the connection data for a file descriptor. More...
 
amxp_connection_tamxp_connection_get_first (uint32_t type)
 Gets the first connection of the given type. More...
 
amxp_connection_tamxp_connection_get_next (amxp_connection_t *con, uint32_t type)
 Gets the next connection data for a file descriptor. More...
 
int amxp_connection_set_el_data (int fd, void *el_data)
 Sets event-loop data. More...
 
amxc_llist_t * amxp_connection_get_connections (void)
 Get a list of the current connections of the application. More...
 
amxc_llist_t * amxp_connection_get_listeners (void)
 Get of the current listen sockets of the application. More...
 

Detailed Description

Function Documentation

◆ amxp_connection_add()

int amxp_connection_add ( int  fd,
amxp_fd_cb_t  reader,
const char *  uri,
uint32_t  type,
void *  priv 
)

Adds a file descriptor (fd) to the list of fds that must be watched.

This function will trigger the signal "connection-added" or "listen-added" depending on the type given.

If the type given is "AMXP_CONNECTION_LISTEN" the fd will be added to the list of listen fds and the signal "listen-added" is triggered.

A callback function must be given. This callback function is called whenever data is available for read.

Parameters
fdthe fd that must be watched
readerthe read callback function, is called when data is available for read
uri(option, can be NULL) a uri representing the fd
typeone of AMXP_CONNECTION_BUS, AMXP_CONNECTION_LISTEN, AMXP_CONNECTION_CUSTOM
privprivate data, will be passed to the callback function
Returns
Returns 0 when success, any other value indicates failure.

Definition at line 129 of file amxp_connections.c.

133  {
134  int retval = -1;
135  amxp_connection_t* con = NULL;
136 
137  when_null(reader, exit);
138  when_true(fd < 0, exit);
139 
140  retval = amxp_can_add_connection(fd, type);
141  when_failed(retval, exit);
142 
143  retval = -1;
144  con = (amxp_connection_t*) calloc(1, sizeof(amxp_connection_t));
145  when_null(con, exit);
146 
147  con->uri = uri == NULL ? NULL : strdup(uri);
148  con->fd = fd;
149  con->priv = priv;
150  con->type = type;
151 
152  retval = amxp_connection_add_impl(con, reader);
153 
154 exit:
155  if(retval != 0) {
156  free(con);
157  }
158  return retval;
159 }
static int amxp_can_add_connection(int fd, uint32_t type)
static int amxp_connection_add_impl(amxp_connection_t *con, amxp_fd_cb_t reader)
static void reader(UNUSED int fd, UNUSED void *priv)

◆ amxp_connection_get()

amxp_connection_t* amxp_connection_get ( int  fd)

Gets the connection data for a file descriptor.

Searches the connection data for the given fd. The connection data is stored in the amxp_connection_t structure.

Parameters
fdthe fd associated with the connection
Returns
returns pointer to the connection data or NULL if no data is found.

Definition at line 212 of file amxp_connections.c.

212  {
213  amxp_connection_t* con = NULL;
214 
216  if(con != NULL) {
217  goto exit;
218  }
220 
221 exit:
222  return con;
223 }
static amxc_llist_t connections
static amxc_llist_t listeners
static amxp_connection_t * amxp_connection_get_internal(amxc_llist_t *list, int fd)

◆ amxp_connection_get_connections()

amxc_llist_t* amxp_connection_get_connections ( void  )

Get a list of the current connections of the application.

At runtime an application can be connected to a number of sockets. This function retrieves a list of sockets the app is connected to.

Returns
A list with all active socket connections.

Definition at line 279 of file amxp_connections.c.

279  {
280  return &connections;
281 }

◆ amxp_connection_get_first()

amxp_connection_t* amxp_connection_get_first ( uint32_t  type)

Gets the first connection of the given type.

Parameters
typeone of AMXP_CONNECTION_BUS, AMXP_CONNECTION_LISTEN, AMXP_CONNECTION_CUSTOM
Returns
returns pointer to the connection data or NULL if no data is found.

Definition at line 225 of file amxp_connections.c.

225  {
226  amxp_connection_t* con = NULL;
227 
228  amxc_llist_for_each(it, &connections) {
229  con = amxc_llist_it_get_data(it, amxp_connection_t, it);
230  if(con->type == type) {
231  break;
232  }
233  con = NULL;
234  }
235 
236  return con;
237 }

◆ amxp_connection_get_listeners()

amxc_llist_t* amxp_connection_get_listeners ( void  )

Get of the current listen sockets of the application.

While an application is running, it can have a list of of open listen sockets that other applications can connect to.

Returns
A list with all open listen sockets.

Definition at line 283 of file amxp_connections.c.

283  {
284  return &listeners;
285 }

◆ amxp_connection_get_next()

amxp_connection_t* amxp_connection_get_next ( amxp_connection_t con,
uint32_t  type 
)

Gets the next connection data for a file descriptor.

Parameters
constarting point reference
typeone of AMXP_CONNECTION_BUS, AMXP_CONNECTION_LISTEN, AMXP_CONNECTION_CUSTOM
Returns
returns pointer to the connection data or NULL if no data is found.

Definition at line 239 of file amxp_connections.c.

240  {
241  amxc_llist_it_t* it = NULL;
242  when_null(con, exit);
243  when_true(con->it.llist != &connections, exit);
244 
245  it = amxc_llist_it_get_next(&con->it);
246  con = NULL;
247  while(it) {
248  con = amxc_llist_it_get_data(it, amxp_connection_t, it);
249  if(con->type == type) {
250  break;
251  }
252  con = NULL;
253  it = amxc_llist_it_get_next(it);
254  }
255 
256 exit:
257  return con;
258 }
amxc_llist_it_t it

◆ amxp_connection_remove()

int amxp_connection_remove ( int  fd)

Removes the fd from the connection list.

This function triggers the signal "connection-deleted".

Event-loop implementations can connect to this signal and when it is triggered remove the watchers for the given fd.

Parameters
fdthe fd that must be watched
Returns
Returns 0 when success, any other value indicates failure.

Definition at line 186 of file amxp_connections.c.

186  {
187  int retval = -1;
188  amxp_connection_t* con = NULL;
189  amxc_var_t var_fd;
190  const char* signal = "connection-deleted";
191 
192  amxc_var_init(&var_fd);
193 
195  if(con == NULL) {
196  signal = "listen-deleted";
198  }
199  when_null(con, exit);
200 
201  amxc_var_set(fd_t, &var_fd, fd);
202  amxp_sigmngr_trigger_signal(NULL, signal, &var_fd);
203  amxc_llist_it_clean(&con->it, amxp_connection_free);
204 
205  retval = 0;
206 
207 exit:
208  amxc_var_clean(&var_fd);
209  return retval;
210 }
static void amxp_connection_free(amxc_llist_it_t *it)
void amxp_sigmngr_trigger_signal(amxp_signal_mngr_t *const sig_mngr, const char *name, const amxc_var_t *const data)
Triggers a signal.
Definition: amxp_signal.c:492

◆ amxp_connection_set_el_data()

int amxp_connection_set_el_data ( int  fd,
void *  el_data 
)

Sets event-loop data.

This function is typically used by event-loop implementations to set event-loop specific data for the connection

Parameters
fdthe fd that must be watched
el_datasome event loop data
Returns
Returns 0 when success, any other value indicates failure.

Definition at line 260 of file amxp_connections.c.

261  {
262  int retval = -1;
263  amxp_connection_t* con = NULL;
264 
266  if(con == NULL) {
268  }
269 
270  when_null(con, exit);
271  con->el_data = el_data;
272 
273  retval = 0;
274 
275 exit:
276  return retval;
277 }
static char * el_data

◆ amxp_connection_wait_write()

int amxp_connection_wait_write ( int  fd,
amxp_fd_cb_t  can_write_cb 
)

Adds a watcher to check if a fd is ready for write.

It can happen that a fd is not ready for write. Most of the time the write fails with error code EAGAIN or EWOULDBLOCK. Whenever this happens it is possible to indicate to the event-loop implementation that the fd must be watched and a callback must be called as soon as the fd is available again for writing.

Another use case for this function is a asynchronous connect.

This function will trigger the signal "connection-wait-write". Event-loop implementations can connect to this signal, to add a watcher for the fd.

Typically when the fd is ready for write the watcher is removed.

Note
Before calling this function make sure the fd is added to the list of connections using amxp_connection_add
Parameters
fdthe fd that must be watched
can_write_cba callback function called when the socket is available for write
Returns
Returns 0 when success, any other value indicates failure.

Definition at line 161 of file amxp_connections.c.

162  {
163  int retval = -1;
164  amxp_connection_t* con = NULL;
165  amxc_var_t var_fd;
166  const char* signal = "connection-wait-write";
167 
168  amxc_var_init(&var_fd);
169  when_true(fd < 0, exit);
170  when_null(can_write_cb, exit);
171 
173  when_null(con, exit);
174 
175  amxc_var_set(fd_t, &var_fd, fd);
176  amxp_sigmngr_trigger_signal(NULL, signal, &var_fd);
177  con->can_write = can_write_cb;
178 
179  retval = 0;
180 
181 exit:
182  amxc_var_clean(&var_fd);
183  return retval;
184 }
amxp_fd_cb_t can_write