#Help creating a system to listen for messages over an IPC port

1 messages · Page 1 of 1 (latest)

paper crane
#

I am new to rust and very new to bevy, and I'm trying to make a game that will require a lot of IPC. I also just figured out how to do IPC just recently, producing a basic proof of concept using zero mq. So I tried to make a component to store the socket data, so I can then call a system on said component to receive messages over IPC. The problem is bevy doesn't seem to like that for synchronization reasons that I don't fully understand yet. Also i understand that this is partially due to the interaction between zmq and bevy; if you can tell me a better way to do IPC in bevy then please do.

#[derive(Component)]
struct ReceiverComponent {
    context:zmq::Context,
    socket:zmq::Socket,
    addr:String,
}

fn init_listener(mut commands:Commands) {
    let context = zmq::Context::new();
    let socket = match context.socket(zmq::REP) {
        Ok(socket) => socket,
        Err(..) => panic!("couldn't open socket"),
    };
    let addr = "ipc:///tmp/daemoncraft.ipc";
    socket.bind(addr);
    commands.spawn(
        ReceiverComponent {
            context:context,
            socket:socket,
            addr:addr.to_string(),
        }
    );
}
#

The only thing I can think is that I don't need to share the component between threads, I just need to run the listener system

#

but the listener system needs to be able to access the zmq socket from the init_listener system

fervent rune
#

Is the type Send? If a type isn't Sync but it's Send, you can keep it a normal component/resource and wrap it in an Mutex. And if it's neither Sync nor Send, then it cannot be a component or normal resource - either stored within bevy as a NonSend resource, or it can be created on another thread and exclusively live there

#

Generally when dealing with sockets like this, you tend to want a dedicated thread (or task, if the lib you're using offers async) that communicates with bevy via channels

paper crane
paper crane
paper crane
#

okay I'll look into how to wrap it in a mutex

#

alternatively i may try using the not feature complete but prolly good enough for my purposes native rust implementation of zmq

#

cuz this seems to be a c being wacky thing

fervent rune
paper crane
#

yup, found it there

#

I was initially looking in the main zmq docs which is why I was a little lost