Asterisk - The Open Source Telephony Project GIT-master-7e7a603
Data Structures | Functions
transport.h File Reference
#include <stdint.h>
#include "asterisk/res_aeap.h"
Include dependency graph for transport.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  aeap_transport
 Asterisk external application transport structure to be "derived" by specific transport implementation types. More...
 
struct  aeap_transport_vtable
 Asterisk external application transport virtual table. More...
 

Functions

int aeap_transport_connect (struct aeap_transport *transport, const char *url, const char *protocol, int timeout)
 Connect a transport. More...
 
struct aeap_transportaeap_transport_create (const char *type)
 Create an Asterisk external application transport. More...
 
struct aeap_transportaeap_transport_create_and_connect (const char *type, const char *url, const char *protocol, int timeout)
 Create an Asterisk external application transport, and connect it. More...
 
void aeap_transport_destroy (struct aeap_transport *transport)
 Destroy a transport. More...
 
int aeap_transport_disconnect (struct aeap_transport *transport)
 Disconnect a transport. More...
 
int aeap_transport_is_connected (struct aeap_transport *transport)
 Whether or not the transport is in a connected state. More...
 
intmax_t aeap_transport_read (struct aeap_transport *transport, void *buf, intmax_t size, enum AST_AEAP_DATA_TYPE *rtype)
 Read data from the transport. More...
 
intmax_t aeap_transport_write (struct aeap_transport *transport, const void *buf, intmax_t size, enum AST_AEAP_DATA_TYPE wtype)
 Write data to the transport. More...
 

Function Documentation

◆ aeap_transport_connect()

int aeap_transport_connect ( struct aeap_transport transport,
const char *  url,
const char *  protocol,
int  timeout 
)

Connect a transport.

Parameters
transportThe transport to connect
urlThe URL to connect to
protocolThe connection protocol to use if applicable
timeoutHow long (in milliseconds) to attempt to connect (-1 equals infinite)
Returns
0 on success, or -1 on error

Definition at line 48 of file transport.c.

50{
51 int res;
52
53 SCOPED_MUTEX(rlock, &transport->read_lock);
54 SCOPED_MUTEX(wlock, &transport->write_lock);
55
56 if (aeap_transport_is_connected(transport)) {
57 return 0;
58 }
59
60 res = transport->vtable->connect(transport, url, protocol, timeout);
61 if (!res) {
62 transport->connected = 1;
63 }
64
65 return res;
66}
#define SCOPED_MUTEX(varname, lock)
scoped lock specialization for mutexes
Definition: lock.h:589
static char url[512]
int(* connect)(struct aeap_transport *self, const char *url, const char *protocol, int timeout)
Connect a transport.
Definition: transport.h:44
ast_mutex_t read_lock
Definition: transport.h:104
struct aeap_transport_vtable * vtable
Definition: transport.h:100
unsigned int connected
Definition: transport.h:102
ast_mutex_t write_lock
Definition: transport.h:106
int aeap_transport_is_connected(struct aeap_transport *transport)
Whether or not the transport is in a connected state.
Definition: transport.c:85

References aeap_transport_is_connected(), aeap_transport_vtable::connect, aeap_transport::connected, aeap_transport::read_lock, SCOPED_MUTEX, url, aeap_transport::vtable, and aeap_transport::write_lock.

Referenced by aeap_transport_create_and_connect(), ast_aeap_connect(), and AST_TEST_DEFINE().

◆ aeap_transport_create()

struct aeap_transport * aeap_transport_create ( const char *  type)

Create an Asterisk external application transport.

Parameters
typeThe type of transport to create
Returns
An Asterisk external application transport, or NULL on error

Definition at line 27 of file transport.c.

28{
29 struct aeap_transport *transport = NULL;
30
31 if (!strncasecmp(type, "ws", 2)) {
32 transport = (struct aeap_transport *)aeap_transport_websocket_create();
33 }
34
35 if (!transport) {
36 ast_log(LOG_ERROR, "AEAP transport: failed to create for type '%s'\n", type);
37 return NULL;
38 }
39
40 ast_mutex_init(&transport->read_lock);
41 ast_mutex_init(&transport->write_lock);
42
43 transport->connected = 0;
44
45 return transport;
46}
#define ast_log
Definition: astobj2.c:42
static const char type[]
Definition: chan_ooh323.c:109
#define LOG_ERROR
#define ast_mutex_init(pmutex)
Definition: lock.h:186
#define NULL
Definition: resample.c:96
Asterisk external application transport structure to be "derived" by specific transport implementatio...
Definition: transport.h:98
struct aeap_transport_websocket * aeap_transport_websocket_create(void)
Creates (heap allocated), and initializes a transport websocket.

References aeap_transport_websocket_create(), ast_log, ast_mutex_init, aeap_transport::connected, LOG_ERROR, NULL, aeap_transport::read_lock, type, and aeap_transport::write_lock.

Referenced by aeap_transport_create_and_connect(), ast_aeap_create(), and AST_TEST_DEFINE().

◆ aeap_transport_create_and_connect()

struct aeap_transport * aeap_transport_create_and_connect ( const char *  type,
const char *  url,
const char *  protocol,
int  timeout 
)

Create an Asterisk external application transport, and connect it.

Parameters
typeThe type of transport to create
urlThe URL to connect to
protocolThe connection protocol to use if applicable
timeoutHow long (in milliseconds) to attempt to connect (-1 equals infinite)
Returns
An Asterisk external application transport, or NULL on error

Definition at line 68 of file transport.c.

70{
71 struct aeap_transport *transport = aeap_transport_create(type);
72
73 if (!transport) {
74 return NULL;
75 }
76
77 if (aeap_transport_connect(transport, url, protocol, timeout)) {
78 aeap_transport_destroy(transport);
79 return NULL;
80 }
81
82 return transport;
83}
int aeap_transport_connect(struct aeap_transport *transport, const char *url, const char *protocol, int timeout)
Connect a transport.
Definition: transport.c:48
void aeap_transport_destroy(struct aeap_transport *transport)
Destroy a transport.
Definition: transport.c:117
struct aeap_transport * aeap_transport_create(const char *type)
Create an Asterisk external application transport.
Definition: transport.c:27

References aeap_transport_connect(), aeap_transport_create(), aeap_transport_destroy(), NULL, type, and url.

Referenced by AST_TEST_DEFINE().

◆ aeap_transport_destroy()

void aeap_transport_destroy ( struct aeap_transport transport)

Destroy a transport.

Parameters
transportThe transport to destroy

Definition at line 117 of file transport.c.

118{
119 if (!transport) {
120 return;
121 }
122
123 /* Ensure an orderly disconnect occurs before final destruction */
124 aeap_transport_disconnect(transport);
125
126 transport->vtable->destroy(transport);
127
128 ast_mutex_destroy(&transport->read_lock);
129 ast_mutex_destroy(&transport->write_lock);
130
131 ast_free(transport);
132}
#define ast_free(a)
Definition: astmm.h:180
#define ast_mutex_destroy(a)
Definition: lock.h:188
void(* destroy)(struct aeap_transport *self)
Destroy a transport.
Definition: transport.h:60
int aeap_transport_disconnect(struct aeap_transport *transport)
Disconnect a transport.
Definition: transport.c:94

References aeap_transport_disconnect(), ast_free, ast_mutex_destroy, aeap_transport_vtable::destroy, aeap_transport::read_lock, aeap_transport::vtable, and aeap_transport::write_lock.

Referenced by aeap_destructor(), aeap_transport_create_and_connect(), and AST_TEST_DEFINE().

◆ aeap_transport_disconnect()

int aeap_transport_disconnect ( struct aeap_transport transport)

Disconnect a transport.

Note
Locks both the transport's read and write locks before calling transport instance's disconnect, and unlocks both before returning.
Parameters
transportThe transport to disconnect
Returns
0 on success, or -1 on error

Definition at line 94 of file transport.c.

95{
96 int res;
97
98 SCOPED_MUTEX(rlock, &transport->read_lock);
99 SCOPED_MUTEX(wlock, &transport->write_lock);
100
101 if (!aeap_transport_is_connected(transport)) {
102 return 0;
103 }
104
105 res = transport->vtable->disconnect(transport);
106
107 /*
108 * Even though the transport is locked here use atomics to set the value of
109 * 'connected' since it's possible the variable is being 'read' by another
110 * thread via the 'is_connected' call.
111 */
112 ast_atomic_fetch_sub(&transport->connected, 1, __ATOMIC_RELAXED);
113
114 return res;
115}
#define ast_atomic_fetch_sub(ptr, val, memorder)
Definition: lock.h:673
int(* disconnect)(struct aeap_transport *self)
Disconnect a transport.
Definition: transport.h:53

References aeap_transport_is_connected(), ast_atomic_fetch_sub, aeap_transport::connected, aeap_transport_vtable::disconnect, aeap_transport::read_lock, SCOPED_MUTEX, aeap_transport::vtable, and aeap_transport::write_lock.

Referenced by aeap_transport_destroy(), ast_aeap_disconnect(), AST_TEST_DEFINE(), websocket_read(), and websocket_write().

◆ aeap_transport_is_connected()

int aeap_transport_is_connected ( struct aeap_transport transport)

Whether or not the transport is in a connected state.

Parameters
transportThe transport object
Returns
True if connected, false otherwise

Definition at line 85 of file transport.c.

86{
87 /*
88 * Avoid using a lock to 'read' the 'connected' variable in order to
89 * keep things slightly more efficient.
90 */
91 return ast_atomic_fetch_add(&transport->connected, 0, __ATOMIC_RELAXED);
92}
#define ast_atomic_fetch_add(ptr, val, memorder)
Support for atomic instructions.
Definition: lock.h:669

References ast_atomic_fetch_add, and aeap_transport::connected.

Referenced by aeap_receive(), aeap_transport_connect(), aeap_transport_disconnect(), aeap_transport_read(), aeap_transport_write(), ast_aeap_connect(), and AST_TEST_DEFINE().

◆ aeap_transport_read()

intmax_t aeap_transport_read ( struct aeap_transport transport,
void *  buf,
intmax_t  size,
enum AST_AEAP_DATA_TYPE rtype 
)

Read data from the transport.

This is a blocking read, and will not return until the transport implementation returns.

Note
Locks transport's read lock before calling transport instance's read, and unlocks it before returning.
Parameters
transportThe transport to read from
bufThe buffer data is read into
sizeThe size of data given data buffer
rtype[out] The type of data read
Returns
Total number of bytes read, or less than zero on error

Definition at line 134 of file transport.c.

136{
137 SCOPED_MUTEX(lock, &transport->read_lock);
138
139 if (!aeap_transport_is_connected(transport)) {
140 return 0;
141 }
142
143 return transport->vtable->read(transport, buf, size, rtype);
144}
ast_mutex_t lock
Definition: app_sla.c:331
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
intmax_t(* read)(struct aeap_transport *self, void *buf, intmax_t size, enum AST_AEAP_DATA_TYPE *rtype)
Read data from a transport.
Definition: transport.h:72

References aeap_transport_is_connected(), buf, lock, aeap_transport_vtable::read, aeap_transport::read_lock, SCOPED_MUTEX, and aeap_transport::vtable.

Referenced by aeap_receive(), and AST_TEST_DEFINE().

◆ aeap_transport_write()

intmax_t aeap_transport_write ( struct aeap_transport transport,
const void *  buf,
intmax_t  size,
enum AST_AEAP_DATA_TYPE  wtype 
)

Write data to the transport.

Note
Locks transport's write lock before calling transport instance's write, and unlocks it before returning.
Parameters
transportThe transport to write to
bufThe data to write
sizeThe size of data to write
wtypeThe type of data to write
Returns
Total number of bytes written, or less than zero on error

Definition at line 146 of file transport.c.

148{
149 SCOPED_MUTEX(lock, &transport->write_lock);
150
151 if (!aeap_transport_is_connected(transport)) {
152 return 0;
153 }
154
155 return transport->vtable->write(transport, buf, size, wtype);
156}
intmax_t(* write)(struct aeap_transport *self, const void *buf, intmax_t size, enum AST_AEAP_DATA_TYPE wtype)
Write data to a transport.
Definition: transport.h:85

References aeap_transport_is_connected(), buf, lock, SCOPED_MUTEX, aeap_transport::vtable, aeap_transport_vtable::write, and aeap_transport::write_lock.

Referenced by aeap_send(), and AST_TEST_DEFINE().