locka99 / opcua

A client and server implementation of the OPC UA specification written in Rust
Mozilla Public License 2.0
496 stars 131 forks source link

[Help] Keep server alive in thread. #131

Closed jigar88 closed 3 years ago

jigar88 commented 3 years ago

Hello , Thanks for this amazing library.

I am building a opc server side using this library to create opc tags for machine. I am getting messages for machine status through unix datagram server. So I need to keep both connection running continuously.

To run opc server I am spawning a thread then I am adding a delay about 30 seconds to set up the client and connects to server. Once server connected it works fine and process all messages and creates nodes and update them properly. However if I am not set client side within specified time delay then client is not able to connect to server at all and gives timeout error.

If I use join method for opc server thread then it never advance to tcp thread.

I can't spawn TCP thread first because it receives messages continuously so there is an infinite loop.

Anything else I can try or I might just increase the delay to work around.

In the code example : I must have to connect to server within 30 seconds or server will keep timing out.

fn run_opc_server() {
        let server = Server::new(ServerConfig::load(&PathBuf::from("/tmp/server.conf")).unwrap());
        let ns = {
            let address_space = server.server.write().unwrap().address_space();
            let mut address_space = address_space.write().unwrap();
            address_space.register_namespace("NS").unwrap()
        };

        let threaded_server_object = self.server.clone();
        thread::spawn(move || Server::run_server(threaded_server_object));

        thread::sleep(time::Duration::from_secs(30 as u64));

        let threaded_server_for_nodes = self.server.clone();
        let handler = thread::spawn(move || processing_tcp_server_messages(
            &mut threaded_server_for_nodes.write().unwrap(), ns,
        ));
        let _ = handler.join().unwrap();
        loop {
            thread::sleep(time::Duration::from_secs(0.0000000000001 as u64));
        }
    }
jigar88 commented 3 years ago

I figured out finally to keep server alive all the time . Thanks .