James Kuszmaul | 6439136 | 2021-01-17 11:32:00 -0800 | [diff] [blame^] | 1 | #include "channel.h" |
| 2 | #include "../data_transport/transport.h" |
| 3 | #include <rawrtcdc/config.h> |
| 4 | #include <rawrtcdc/data_channel.h> |
| 5 | #include <rawrtcdc/data_channel_parameters.h> |
| 6 | #include <rawrtcc/code.h> |
| 7 | #include <re.h> |
| 8 | |
| 9 | #define DEBUG_MODULE "data-channel" |
| 10 | //#define RAWRTC_DEBUG_MODULE_LEVEL 7 // Note: Uncomment this to debug this module only |
| 11 | #include <rawrtcc/debug.h> |
| 12 | |
| 13 | /* |
| 14 | * Get the current state of the data channel. |
| 15 | */ |
| 16 | enum rawrtc_code rawrtc_data_channel_get_state( |
| 17 | enum rawrtc_data_channel_state* const statep, // de-referenced |
| 18 | struct rawrtc_data_channel* const channel) { |
| 19 | // Check arguments |
| 20 | if (!statep || !channel) { |
| 21 | return RAWRTC_CODE_INVALID_ARGUMENT; |
| 22 | } |
| 23 | |
| 24 | // Set state & done |
| 25 | *statep = channel->state; |
| 26 | return RAWRTC_CODE_SUCCESS; |
| 27 | } |
| 28 | |
| 29 | /* |
| 30 | * Get the currently buffered amount (bytes) of outgoing application |
| 31 | * data of the data channel. |
| 32 | */ |
| 33 | enum rawrtc_code rawrtc_data_channel_get_buffered_amount( |
| 34 | uint64_t* const buffered_amountp, // de-referenced |
| 35 | struct rawrtc_data_channel* const channel) { |
| 36 | // Check arguments |
| 37 | if (!buffered_amountp || !channel) { |
| 38 | return RAWRTC_CODE_INVALID_ARGUMENT; |
| 39 | } |
| 40 | |
| 41 | // TODO: Implement this! |
| 42 | return RAWRTC_CODE_NOT_IMPLEMENTED; |
| 43 | } |
| 44 | |
| 45 | /* |
| 46 | * Set the data channel's buffered amount (bytes) low threshold for |
| 47 | * outgoing application data. |
| 48 | */ |
| 49 | enum rawrtc_code rawrtc_data_channel_set_buffered_amount_low_threshold( |
| 50 | struct rawrtc_data_channel* const channel, uint64_t const buffered_amount_low_threshold) { |
| 51 | // Check arguments |
| 52 | if (!channel) { |
| 53 | return RAWRTC_CODE_INVALID_ARGUMENT; |
| 54 | } |
| 55 | |
| 56 | // TODO: Implement this! |
| 57 | (void) buffered_amount_low_threshold; |
| 58 | return RAWRTC_CODE_NOT_IMPLEMENTED; |
| 59 | } |
| 60 | |
| 61 | /* |
| 62 | * Get the data channel's buffered amount (bytes) low threshold for |
| 63 | * outgoing application data. |
| 64 | */ |
| 65 | enum rawrtc_code rawrtc_data_channel_get_buffered_amount_low_threshold( |
| 66 | uint64_t* const buffered_amount_low_thresholdp, // de-referenced |
| 67 | struct rawrtc_data_channel* const channel) { |
| 68 | // Check arguments |
| 69 | if (!buffered_amount_low_thresholdp || !channel) { |
| 70 | return RAWRTC_CODE_INVALID_ARGUMENT; |
| 71 | } |
| 72 | |
| 73 | // TODO: Implement this! |
| 74 | return RAWRTC_CODE_NOT_IMPLEMENTED; |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * Unset the handler argument and all handlers of the data channel. |
| 79 | */ |
| 80 | enum rawrtc_code rawrtc_data_channel_unset_handlers(struct rawrtc_data_channel* const channel) { |
| 81 | // Check arguments |
| 82 | if (!channel) { |
| 83 | return RAWRTC_CODE_INVALID_ARGUMENT; |
| 84 | } |
| 85 | |
| 86 | // Unset handler argument |
| 87 | channel->arg = NULL; |
| 88 | |
| 89 | // Unset all handlers |
| 90 | channel->message_handler = NULL; |
| 91 | channel->close_handler = NULL; |
| 92 | channel->error_handler = NULL; |
| 93 | channel->buffered_amount_low_handler = NULL; |
| 94 | channel->open_handler = NULL; |
| 95 | |
| 96 | // Done |
| 97 | return RAWRTC_CODE_SUCCESS; |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * Get the data channel's parameters. |
| 102 | * `*parametersp` must be unreferenced. |
| 103 | */ |
| 104 | enum rawrtc_code rawrtc_data_channel_get_parameters( |
| 105 | struct rawrtc_data_channel_parameters** const parametersp, // de-referenced |
| 106 | struct rawrtc_data_channel* const channel) { |
| 107 | // Check arguments |
| 108 | if (!parametersp || !channel) { |
| 109 | return RAWRTC_CODE_INVALID_ARGUMENT; |
| 110 | } |
| 111 | |
| 112 | // Set pointer & done |
| 113 | *parametersp = mem_ref(channel->parameters); |
| 114 | return RAWRTC_CODE_SUCCESS; |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | * Enable or disable streamed delivery. |
| 119 | * |
| 120 | * Note: In case an incoming message is currently pending (there are |
| 121 | * queued chunks in the internal reassembly buffer), this will |
| 122 | * fail with a *still in use* error. |
| 123 | */ |
| 124 | enum rawrtc_code rawrtc_data_channel_set_streaming( |
| 125 | struct rawrtc_data_channel* const channel, bool const on) { |
| 126 | enum rawrtc_code error; |
| 127 | |
| 128 | // Check arguments |
| 129 | if (!channel) { |
| 130 | return RAWRTC_CODE_INVALID_ARGUMENT; |
| 131 | } |
| 132 | |
| 133 | // Check state |
| 134 | if (channel->state == RAWRTC_DATA_CHANNEL_STATE_CLOSING || |
| 135 | channel->state == RAWRTC_DATA_CHANNEL_STATE_CLOSED) { |
| 136 | return RAWRTC_CODE_INVALID_STATE; |
| 137 | } |
| 138 | |
| 139 | // Does anything change? |
| 140 | if ((on && channel->flags & RAWRTC_DATA_CHANNEL_FLAGS_STREAMED) || |
| 141 | (!on && !(channel->flags & RAWRTC_DATA_CHANNEL_FLAGS_STREAMED))) { |
| 142 | return RAWRTC_CODE_SUCCESS; |
| 143 | } |
| 144 | |
| 145 | // Let the transport know we want to enable/disable streaming |
| 146 | error = channel->transport->channel_set_streaming(channel, on); |
| 147 | if (error) { |
| 148 | return error; |
| 149 | } |
| 150 | |
| 151 | // Enable/disable streaming & done |
| 152 | if (on) { |
| 153 | channel->flags |= RAWRTC_DATA_CHANNEL_FLAGS_STREAMED; |
| 154 | DEBUG_PRINTF("Enabled streaming mode\n"); |
| 155 | } else { |
| 156 | channel->flags &= ~RAWRTC_DATA_CHANNEL_FLAGS_STREAMED; |
| 157 | DEBUG_PRINTF("Disabled streaming mode\n"); |
| 158 | } |
| 159 | return RAWRTC_CODE_SUCCESS; |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * Set the data channel's open handler. |
| 164 | */ |
| 165 | enum rawrtc_code rawrtc_data_channel_set_open_handler( |
| 166 | struct rawrtc_data_channel* const channel, |
| 167 | rawrtc_data_channel_open_handler const open_handler // nullable |
| 168 | ) { |
| 169 | // Check arguments |
| 170 | if (!channel) { |
| 171 | return RAWRTC_CODE_INVALID_ARGUMENT; |
| 172 | } |
| 173 | |
| 174 | // Set open handler & done |
| 175 | channel->open_handler = open_handler; |
| 176 | return RAWRTC_CODE_SUCCESS; |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | * Get the data channel's open handler. |
| 181 | * Returns `RAWRTC_CODE_NO_VALUE` in case no handler has been set. |
| 182 | */ |
| 183 | enum rawrtc_code rawrtc_data_channel_get_open_handler( |
| 184 | rawrtc_data_channel_open_handler* const open_handlerp, // de-referenced |
| 185 | struct rawrtc_data_channel* const channel) { |
| 186 | // Check arguments |
| 187 | if (!open_handlerp || !channel) { |
| 188 | return RAWRTC_CODE_INVALID_ARGUMENT; |
| 189 | } |
| 190 | |
| 191 | // Get open handler (if any) |
| 192 | if (channel->open_handler) { |
| 193 | *open_handlerp = channel->open_handler; |
| 194 | return RAWRTC_CODE_SUCCESS; |
| 195 | } else { |
| 196 | return RAWRTC_CODE_NO_VALUE; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | * Set the data channel's buffered amount low handler. |
| 202 | */ |
| 203 | enum rawrtc_code rawrtc_data_channel_set_buffered_amount_low_handler( |
| 204 | struct rawrtc_data_channel* const channel, |
| 205 | rawrtc_data_channel_buffered_amount_low_handler const buffered_amount_low_handler // nullable |
| 206 | ) { |
| 207 | // Check arguments |
| 208 | if (!channel) { |
| 209 | return RAWRTC_CODE_INVALID_ARGUMENT; |
| 210 | } |
| 211 | |
| 212 | // Set buffered amount low handler & done |
| 213 | channel->buffered_amount_low_handler = buffered_amount_low_handler; |
| 214 | return RAWRTC_CODE_SUCCESS; |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * Get the data channel's buffered amount low handler. |
| 219 | * Returns `RAWRTC_CODE_NO_VALUE` in case no handler has been set. |
| 220 | */ |
| 221 | enum rawrtc_code rawrtc_data_channel_get_buffered_amount_low_handler( |
| 222 | rawrtc_data_channel_buffered_amount_low_handler* const |
| 223 | buffered_amount_low_handlerp, // de-referenced |
| 224 | struct rawrtc_data_channel* const channel) { |
| 225 | // Check arguments |
| 226 | if (!buffered_amount_low_handlerp || !channel) { |
| 227 | return RAWRTC_CODE_INVALID_ARGUMENT; |
| 228 | } |
| 229 | |
| 230 | // Get buffered amount low handler (if any) |
| 231 | if (channel->buffered_amount_low_handler) { |
| 232 | *buffered_amount_low_handlerp = channel->buffered_amount_low_handler; |
| 233 | return RAWRTC_CODE_SUCCESS; |
| 234 | } else { |
| 235 | return RAWRTC_CODE_NO_VALUE; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * Set the data channel's error handler. |
| 241 | */ |
| 242 | enum rawrtc_code rawrtc_data_channel_set_error_handler( |
| 243 | struct rawrtc_data_channel* const channel, |
| 244 | rawrtc_data_channel_error_handler const error_handler // nullable |
| 245 | ) { |
| 246 | // Check arguments |
| 247 | if (!channel) { |
| 248 | return RAWRTC_CODE_INVALID_ARGUMENT; |
| 249 | } |
| 250 | |
| 251 | // Set error handler & done |
| 252 | channel->error_handler = error_handler; |
| 253 | return RAWRTC_CODE_SUCCESS; |
| 254 | } |
| 255 | |
| 256 | /* |
| 257 | * Get the data channel's error handler. |
| 258 | * Returns `RAWRTC_CODE_NO_VALUE` in case no handler has been set. |
| 259 | */ |
| 260 | enum rawrtc_code rawrtc_data_channel_get_error_handler( |
| 261 | rawrtc_data_channel_error_handler* const error_handlerp, // de-referenced |
| 262 | struct rawrtc_data_channel* const channel) { |
| 263 | // Check arguments |
| 264 | if (!error_handlerp || !channel) { |
| 265 | return RAWRTC_CODE_INVALID_ARGUMENT; |
| 266 | } |
| 267 | |
| 268 | // Get error handler (if any) |
| 269 | if (channel->error_handler) { |
| 270 | *error_handlerp = channel->error_handler; |
| 271 | return RAWRTC_CODE_SUCCESS; |
| 272 | } else { |
| 273 | return RAWRTC_CODE_NO_VALUE; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | /* |
| 278 | * Set the data channel's close handler. |
| 279 | */ |
| 280 | enum rawrtc_code rawrtc_data_channel_set_close_handler( |
| 281 | struct rawrtc_data_channel* const channel, |
| 282 | rawrtc_data_channel_close_handler const close_handler // nullable |
| 283 | ) { |
| 284 | // Check arguments |
| 285 | if (!channel) { |
| 286 | return RAWRTC_CODE_INVALID_ARGUMENT; |
| 287 | } |
| 288 | |
| 289 | // Set close handler & done |
| 290 | channel->close_handler = close_handler; |
| 291 | return RAWRTC_CODE_SUCCESS; |
| 292 | } |
| 293 | |
| 294 | /* |
| 295 | * Get the data channel's close handler. |
| 296 | * Returns `RAWRTC_CODE_NO_VALUE` in case no handler has been set. |
| 297 | */ |
| 298 | enum rawrtc_code rawrtc_data_channel_get_close_handler( |
| 299 | rawrtc_data_channel_close_handler* const close_handlerp, // de-referenced |
| 300 | struct rawrtc_data_channel* const channel) { |
| 301 | // Check arguments |
| 302 | if (!close_handlerp || !channel) { |
| 303 | return RAWRTC_CODE_INVALID_ARGUMENT; |
| 304 | } |
| 305 | |
| 306 | // Get close handler (if any) |
| 307 | if (channel->close_handler) { |
| 308 | *close_handlerp = channel->close_handler; |
| 309 | return RAWRTC_CODE_SUCCESS; |
| 310 | } else { |
| 311 | return RAWRTC_CODE_NO_VALUE; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | * Set the data channel's message handler. |
| 317 | */ |
| 318 | enum rawrtc_code rawrtc_data_channel_set_message_handler( |
| 319 | struct rawrtc_data_channel* const channel, |
| 320 | rawrtc_data_channel_message_handler const message_handler // nullable |
| 321 | ) { |
| 322 | // Check arguments |
| 323 | if (!channel) { |
| 324 | return RAWRTC_CODE_INVALID_ARGUMENT; |
| 325 | } |
| 326 | |
| 327 | // Set message handler & done |
| 328 | channel->message_handler = message_handler; |
| 329 | return RAWRTC_CODE_SUCCESS; |
| 330 | } |
| 331 | |
| 332 | /* |
| 333 | * Get the data channel's message handler. |
| 334 | * Returns `RAWRTC_CODE_NO_VALUE` in case no handler has been set. |
| 335 | */ |
| 336 | enum rawrtc_code rawrtc_data_channel_get_message_handler( |
| 337 | rawrtc_data_channel_message_handler* const message_handlerp, // de-referenced |
| 338 | struct rawrtc_data_channel* const channel) { |
| 339 | // Check arguments |
| 340 | if (!message_handlerp || !channel) { |
| 341 | return RAWRTC_CODE_INVALID_ARGUMENT; |
| 342 | } |
| 343 | |
| 344 | // Get message handler (if any) |
| 345 | if (channel->message_handler) { |
| 346 | *message_handlerp = channel->message_handler; |
| 347 | return RAWRTC_CODE_SUCCESS; |
| 348 | } else { |
| 349 | return RAWRTC_CODE_NO_VALUE; |
| 350 | } |
| 351 | } |