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

Author Topic: Handling asynchronous receive with a TCP socket  (Read 6487 times)

0 Members and 2 Guests are viewing this topic.

Overv

  • Newbie
  • *
  • Posts: 9
    • View Profile
Handling asynchronous receive with a TCP socket
« on: June 10, 2010, 08:39:21 pm »
Hi,

I'm currently using a thread to process a queue with asynchronous Connect and Send operations. Now I also want to make receive asynchronous. The problem with receiving however is that now it doesn't block the main program, but it blocks the asynchronous processing queue, so my socket still becomes useless until I receive something. What is the equivalent of "select" with SFML?

This is my current thread code:

Code: [Select]
void TCPThread( void* userdata )
{
TCPSocket* sock = (TCPSocket*)userdata;

while ( true )
{
if ( sock->queue.size() > 0 )
{
TCPOperation op = sock->queue[0];

switch ( op.op )
{
case 0:
sock->connected = false;
sock->connected = sock->sock.Connect( op.arg2, op.arg1, op.arg4 ) == sf::Socket::Done;
break;

case 1:
sock->sock.Send( op.arg1, op.arg3 );
break;
}

sock->queue.pop_front();
}
}
}


Also, what is the use of SetBlocking? There is no way to check if you're connected yet.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Handling asynchronous receive with a TCP socket
« Reply #1 on: June 10, 2010, 08:43:45 pm »
Quote
What is the equivalent of "select" with SFML?

sf::Selector.

Quote
Also, what is the use of SetBlocking? There is no way to check if you're connected yet.

Why do you need to know if you're connected?
Calling Receive on a non-blocking socket will return a useful status: Done, Error, NotReady or Error.
Laurent Gomila - SFML developer

Overv

  • Newbie
  • *
  • Posts: 9
    • View Profile
Handling asynchronous receive with a TCP socket
« Reply #2 on: June 10, 2010, 09:06:59 pm »
Quote from: "Laurent"
sf::Selector.


I tried using that, but VS told me GetSocketsReady doesn't exist.

Quote from: "Laurent"
Why do you need to know if you're connected?
Calling Receive on a non-blocking socket will return a useful status: Done, Error, NotReady or Error.


What if I want to check if I'm connected without sending anything? Also, making my socket non-blocking and doing the following makes the program loop infinitely.

Code: [Select]
while ( sock.Send( data, sizeof( data ) ) != sf::Socket::Done );

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Handling asynchronous receive with a TCP socket
« Reply #3 on: June 10, 2010, 09:09:40 pm »
Quote
I tried using that, but VS told me GetSocketsReady doesn't exist.

Which version of SFML are you using?

Quote
What if I want to check if I'm connected without sending anything?

This is not possible in SFML 1, but it is implemented in SFML 2. But this is not related to non-blocking mode. You shouldn't need it more with non-blocking sockets.

Quote
Also, making my socket non-blocking and doing the following makes the program loop infinitely.

You should loop while you get sf::Socket::NotReady, but break on sf::Socket::Error and sf::Socket::Disconnected.
Laurent Gomila - SFML developer

Overv

  • Newbie
  • *
  • Posts: 9
    • View Profile
Handling asynchronous receive with a TCP socket
« Reply #4 on: June 10, 2010, 09:11:38 pm »
Quote from: "Laurent"
Quote
I tried using that, but VS told me GetSocketsReady doesn't exist.

Which version of SFML are you using?


SFML 1.6

The loop problem seems to be a VS2010 problem, nevermind.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Handling asynchronous receive with a TCP socket
« Reply #5 on: June 10, 2010, 09:24:11 pm »
In SFML it's Selector::GetSocketReady, not GetSocketsReady ;)
Laurent Gomila - SFML developer

Overv

  • Newbie
  • *
  • Posts: 9
    • View Profile
Handling asynchronous receive with a TCP socket
« Reply #6 on: June 10, 2010, 11:19:10 pm »
Quote from: "Laurent"
In SFML it's Selector::GetSocketReady, not GetSocketsReady ;)


Oh, alright. Then you need to correct the tutorial :)
http://www.sfml-dev.org/tutorials/1.2/network-selector.php

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Handling asynchronous receive with a TCP socket
« Reply #7 on: June 10, 2010, 11:25:34 pm »
This is the tutorial for SFML 1.2... ;)
Quote
WARNING : this is the documentation for an old version of SFML ; the documentation for the latest official release is available through the main menu
Laurent Gomila - SFML developer

Overv

  • Newbie
  • *
  • Posts: 9
    • View Profile
Handling asynchronous receive with a TCP socket
« Reply #8 on: June 11, 2010, 12:34:25 am »
Quote from: "Laurent"
This is the tutorial for SFML 1.2... ;)
Quote
WARNING : this is the documentation for an old version of SFML ; the documentation for the latest official release is available through the main menu


Sorry for wasting your time  :roll:

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
Handling asynchronous receive with a TCP socket
« Reply #9 on: June 11, 2010, 04:23:45 am »
Yeah that's kinda what's crazy when you are using constantly evolving code.

You have to dig through the sources for the best documentation. I was surprised to see a mini-tutorial/example in a header file so maybe there is something more if you look?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Handling asynchronous receive with a TCP socket
« Reply #10 on: June 11, 2010, 08:26:49 am »
Quote
You have to dig through the sources for the best documentation. I was surprised to see a mini-tutorial/example in a header file so maybe there is something more if you look?

This is just for non-released versions for which I didn't generate/upload the documentation yet. Otherwise, you're not supposed to dig through the source code to find it.
Laurent Gomila - SFML developer

 

anything