Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How to protect a set of dynamically increasing critical sections  (Read 4136 times)

0 Members and 2 Guests are viewing this topic.

argoran

  • Newbie
  • *
  • Posts: 4
    • View Profile
I'm trying to implement a network library with sfml 1.6.

This library provides a client/server model where a client can connect to a server and send and receive message between the server, and a server can accept multiple clients' connections and communicate with them.

My problem is for the server.
I want to process the client connections in a non-blocking mode, so in the "update" function of the server it only launches two thread for each connection (one for sending message, one for receiving) and checks for their completion.
I also want to have two message buffers for each client connection, so the server can push message into the sending buffer and the receiving threads will push messages into the receiving buffer. These buffers are critical sections and should be protected by mutexes.

Since the number of clients are undetermined and should be scalable, I cant just define a global mutex (or several glabal mutexes) as in the tutorial because I can never know the number of mutex I need.

Is there a way of dealing with this kind of situation where the number of resources that need to be protected might change dynamically during runtime ?

Or my server model is wrong and I should use some other way to implement non-blocking here ?

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
Re: How to protect a set of dynamically increasing critical sections
« Reply #1 on: April 03, 2012, 07:52:02 pm »
I don't think you need to use threads here. You can set your sockets in non blocking mode : http://www.sfml-dev.org/documentation/2.0/classsf_1_1Socket.php#a165fc1423e281ea2714c70303d3a9782
Want to play movies in your SFML application? Check out sfeMovie!

argoran

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: How to protect a set of dynamically increasing critical sections
« Reply #2 on: April 03, 2012, 09:19:42 pm »
I don't think you need to use threads here. You can set your sockets in non blocking mode : http://www.sfml-dev.org/documentation/2.0/classsf_1_1Socket.php#a165fc1423e281ea2714c70303d3a9782

But how do you check if the function call (say "Receive") is finished ? They return Socket::NotReady immediately in non-blocking mode.

Maybe with a selector ?

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
Re: How to protect a set of dynamically increasing critical sections
« Reply #3 on: April 03, 2012, 09:59:08 pm »
From what I remember, getting NotReady means the data has not been fully received. But maybe a part of it. So what you would have to do is calling receive() again and again until everything has been received.
Want to play movies in your SFML application? Check out sfeMovie!

argoran

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: How to protect a set of dynamically increasing critical sections
« Reply #4 on: April 03, 2012, 11:12:50 pm »
From what I remember, getting NotReady means the data has not been fully received. But maybe a part of it. So what you would have to do is calling receive() again and again until everything has been received.

OK, thank you !

And for "Send", "Connect", and "Accept", I should do the same thing ?

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
Re: How to protect a set of dynamically increasing critical sections
« Reply #5 on: April 03, 2012, 11:24:32 pm »
Looks like send() can't be non-blocking, whereas connect() can, you'll have to have a look at the documentation. And it has no meaning for accept(), as reaching the accept() call means a connection is ready to be created.
Want to play movies in your SFML application? Check out sfeMovie!

argoran

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: How to protect a set of dynamically increasing critical sections
« Reply #6 on: April 04, 2012, 12:58:47 am »
OK, thank you !

I will have a try :)