#[repr(C)]
pub struct LibIpconMsg { pub group: [c_char; 32], pub peer: [c_char; 32], /* private fields */ }
Expand description

Message interface to libipcon.

Fields§

§group: [c_char; 32]§peer: [c_char; 32]

Implementations§

Examples found in repository?
src/ipcon_msg.rs (line 231)
230
231
232
    fn default() -> Self {
        Self::new()
    }
More examples
Hide additional examples
src/ipcon.rs (line 294)
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
    pub fn receive_msg(&self) -> Result<IpconMsg, IpconError> {
        let lmsg = LibIpconMsg::new();

        unsafe {
            let ret = ipcon_rcv(Ipcon::to_handler(self.handler), &lmsg);
            if ret < 0 {
                return Err(errno_to_error(ret))
                    .into_report()
                    .attach_printable(format!(
                        "ipcon_rcv() {} receive message failed: {}",
                        self.name.as_deref().unwrap_or("Anon"),
                        ret
                    ));
            }
        }

        lmsg.into()
    }

    /// Send an unicast IPCON message to a specific peer.
    /// This function will fail if the peer doesn't enable IPF_SND_IF.
    pub fn send_unicast_msg(&self, peer: &str, buf: &[u8]) -> Result<(), IpconError> {
        self.send_unicast_msg_by_ref(peer, buf)
    }

    /// Send an unicast IPCON message to a specific peer.
    /// This function will fail if the peer doesn't enable IPF_SND_IF.
    pub fn send_unicast_msg_by_ref(&self, peer: &str, buf: &[u8]) -> Result<(), IpconError> {
        valid_name(peer).attach_printable(format!("Invalid peer name: {}", peer))?;

        if buf.len() > IPCON_MAX_PAYLOAD_LEN {
            return Err(IpconError::InvalidData)
                .into_report()
                .attach_printable(format!(
                    "Buffer length is to large {} > {}",
                    buf.len(),
                    IPCON_MAX_PAYLOAD_LEN
                ));
        }

        let pname = CString::new(peer)
            .into_report()
            .change_context(IpconError::InvalidData)?;

        unsafe {
            let ptr = pname.into_raw();
            let ret = ipcon_send_unicast(
                Ipcon::to_handler(self.handler),
                ptr as *const c_char,
                buf.as_ptr(),
                buf.len() as size_t,
            );

            let _ = CString::from_raw(ptr);

            if ret < 0 {
                return Err(errno_to_error(ret))
                    .into_report()
                    .attach_printable(format!(
                        "send_unicast_msg() {} send message to peer `{}` failed: {}",
                        self.name.as_deref().unwrap_or("Anon"),
                        peer,
                        ret
                    ));
            }
        }

        Ok(())
    }

    /// Register a multicast group.
    pub fn register_group(&self, group: &str) -> Result<(), IpconError> {
        valid_name(group).attach_printable("register_group error: invalid group name")?;

        let g = CString::new(group)
            .into_report()
            .change_context(IpconError::InvalidName)?;

        unsafe {
            let ptr = g.into_raw();
            let ret = ipcon_register_group(Ipcon::to_handler(self.handler), ptr as *const c_char);
            let _ = CString::from_raw(ptr);
            if ret < 0 {
                return Err(errno_to_error(ret))
                    .into_report()
                    .attach_printable(format!(
                        "ipcon_register_group() {} register `{}` failed: {}",
                        self.name.as_deref().unwrap_or("Anon"),
                        group,
                        ret
                    ));
            }
        }

        Ok(())
    }

    /// Unregister a multicast group.
    pub fn unregister_group(&self, group: &str) -> Result<(), IpconError> {
        valid_name(group).attach_printable(format!("Invalid group name: {}", group))?;

        let g = CString::new(group)
            .into_report()
            .change_context(IpconError::InvalidName)?;

        unsafe {
            let ptr = g.into_raw();
            let ret = ipcon_unregister_group(Ipcon::to_handler(self.handler), ptr as *const c_char);
            let _ = CString::from_raw(ptr);
            if ret < 0 {
                return Err(errno_to_error(ret))
                    .into_report()
                    .attach_printable(format!(
                        "ipcon_unregister_group() {} unregister `{}` failed: {}",
                        self.name.as_deref().unwrap_or("Anon"),
                        group,
                        ret
                    ));
            }
        }

        Ok(())
    }

    /// Subscribe a multicast group of a peer.
    pub fn join_group(&self, peer: &str, group: &str) -> Result<(), IpconError> {
        valid_name(peer).attach_printable(format!("Invalid peer name: {}", peer))?;
        valid_name(group).attach_printable(format!("Invalid group name: {}", group))?;

        let p = CString::new(peer)
            .into_report()
            .change_context(IpconError::InvalidName)?;

        let g = CString::new(group)
            .into_report()
            .change_context(IpconError::InvalidName)?;

        unsafe {
            let ptr = p.into_raw();
            let pgtr = g.into_raw();
            let ret = ipcon_join_group(
                Ipcon::to_handler(self.handler),
                ptr as *const c_char,
                pgtr as *const c_char,
            );
            let _ = CString::from_raw(ptr);
            let _ = CString::from_raw(pgtr);
            if ret < 0 {
                return Err(errno_to_error(ret))
                    .into_report()
                    .attach_printable(format!(
                        "ipcon_join_group() {} join `{}@{}` failed: {}",
                        self.name.as_deref().unwrap_or("Anon"),
                        group,
                        peer,
                        ret
                    ));
            }
        }

        Ok(())
    }

    /// Unsubscribe a multicast group of a peer.
    pub fn leave_group(&self, peer: &str, group: &str) -> Result<(), IpconError> {
        valid_name(peer).attach_printable(format!("Invalid peer name: {}", peer))?;
        valid_name(group).attach_printable(format!("Invalid group name: {}", group))?;

        let p = CString::new(peer)
            .into_report()
            .change_context(IpconError::InvalidName)?;

        let g = CString::new(group)
            .into_report()
            .change_context(IpconError::InvalidName)?;

        unsafe {
            let ptr = p.into_raw();
            let pgtr = g.into_raw();
            let ret = ipcon_leave_group(
                Ipcon::to_handler(self.handler),
                ptr as *const c_char,
                pgtr as *const c_char,
            );
            let _ = CString::from_raw(ptr);
            let _ = CString::from_raw(pgtr);

            if ret < 0 {
                return Err(errno_to_error(ret))
                    .into_report()
                    .attach_printable(format!(
                        "ipcon_leave_group() {} leave `{}@{}` failed: {}",
                        self.name.as_deref().unwrap_or("Anon"),
                        group,
                        peer,
                        ret
                    ));
            }
        }

        Ok(())
    }

    /// Send multicast messages to an owned group.
    pub fn send_multicast(&self, group: &str, buf: &[u8], sync: bool) -> Result<(), IpconError> {
        self.send_multicast_by_ref(group, buf, sync)
    }

    /// Send multicast messages to an owned group.
    pub fn send_multicast_by_ref(
        &self,
        group: &str,
        buf: &[u8],
        sync: bool,
    ) -> Result<(), IpconError> {
        valid_name(group).attach_printable(format!("Invalid group name: {}", group))?;

        if buf.len() > IPCON_MAX_PAYLOAD_LEN {
            return Err(IpconError::InvalidData)
                .into_report()
                .attach_printable(format!(
                    "Buffer length is too large {} > {}",
                    buf.len(),
                    IPCON_MAX_PAYLOAD_LEN,
                ));
        }

        let g = CString::new(group)
            .into_report()
            .change_context(IpconError::InvalidName)?;

        let mut s: i32 = 0;
        if sync {
            s = 1;
        }

        unsafe {
            let pgtr = g.into_raw();
            let ret = ipcon_send_multicast(
                Ipcon::to_handler(self.handler),
                pgtr as *const c_char,
                buf.as_ptr(),
                buf.len() as size_t,
                s,
            );
            let _ = CString::from_raw(pgtr);

            if ret < 0 {
                return Err(errno_to_error(ret))
                    .into_report()
                    .attach_printable(format!(
                        "ipcon_send_multicast() to `{}@{}` failed: {}",
                        group,
                        self.name.as_deref().unwrap_or("Anon"),
                        ret
                    ));
            }
        }

        Ok(())
    }

    /// Receiving message with timeout.
    /// receive_msg() will block until a message come. receive_msg_timeout() adds a timeout to
    /// it.The timeout is specified with seconds and microseconds.
    pub fn receive_msg_timeout(&self, tv_sec: u32, tv_usec: u32) -> Result<IpconMsg, IpconError> {
        let lmsg = LibIpconMsg::new();
        let t = libc::timeval {
            tv_sec: tv_sec as libc::time_t,
            tv_usec: tv_usec as libc::suseconds_t,
        };

        unsafe {
            let ret = ipcon_rcv_timeout(Ipcon::to_handler(self.handler), &lmsg, &t);
            if ret < 0 {
                return Err(errno_to_error(ret))
                    .into_report()
                    .attach_printable(format!(
                        "ipcon_rcv_timeout() {} receive message failed: {}",
                        self.name.as_deref().unwrap_or("Anon"),
                        ret
                    ));
            }
        }

        lmsg.into()
    }

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Returns the “default value” for a type. Read more
Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Set the foreground color generically Read more
Set the background color generically. Read more
Change the foreground color to black
Change the background color to black
Change the foreground color to red
Change the background color to red
Change the foreground color to green
Change the background color to green
Change the foreground color to yellow
Change the background color to yellow
Change the foreground color to blue
Change the background color to blue
Change the foreground color to magenta
Change the background color to magenta
Change the foreground color to purple
Change the background color to purple
Change the foreground color to cyan
Change the background color to cyan
Change the foreground color to white
Change the background color to white
Change the foreground color to the terminal default
Change the background color to the terminal default
Change the foreground color to bright black
Change the background color to bright black
Change the foreground color to bright red
Change the background color to bright red
Change the foreground color to bright green
Change the background color to bright green
Change the foreground color to bright yellow
Change the background color to bright yellow
Change the foreground color to bright blue
Change the background color to bright blue
Change the foreground color to bright magenta
Change the background color to bright magenta
Change the foreground color to bright purple
Change the background color to bright purple
Change the foreground color to bright cyan
Change the background color to bright cyan
Change the foreground color to bright white
Change the background color to bright white
Make the text bold
Make the text dim
Make the text italicized
Make the text italicized
Make the text blink
Make the text blink (but fast!)
Swap the foreground and background colors
Hide the text
Cross out the text
Set the foreground color at runtime. Only use if you do not know which color will be used at compile-time. If the color is constant, use either OwoColorize::fg or a color-specific method, such as OwoColorize::green, Read more
Set the background color at runtime. Only use if you do not know what color to use at compile-time. If the color is constant, use either OwoColorize::bg or a color-specific method, such as OwoColorize::on_yellow, Read more
Set the foreground color to a specific RGB value.
Set the background color to a specific RGB value.
Sets the foreground color to an RGB value.
Sets the background color to an RGB value.
Apply a runtime-determined style
Apply a given transformation function to all formatters if the given stream supports at least basic ANSI colors, allowing you to conditionally apply given styles/colors. Read more
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more