Boost logo

Boost :

From: christopher baus (christopher_at_[hidden])
Date: 2006-01-12 20:33:02


>> On Thu, 12 Jan 2006, christopher baus wrote:
>>
>>> Also when a connection is closed by a peer during a read operation what
>>> error should be expected?
>>>
>>> eof, not_connected, fault, operation_aborted all seem reasonable.
>>
>> For a TCP socket, I would expect it to return eof, since that is how TCP
>> sockets work.
>
> I tested it on Windows and it does indeed return eof. It might be worth
> while to document this with the read functions.

Here's the class I'm using to track the socket state in my application
code. I am setting the state in my event handlers (which is rather
error prone), but asio could manage it internally.

class stream_socket_connection_state
{
public:
  stream_socket_connection_state():state_(DISCONNECTED){}

  void connect()
  {
    if(DISCONNECTED == state_){
      state_ = CONNECTED_FOR_READ_AND_WRITE;
    }
    //
    // ignore attempts to reconnect socket
  }

  void shutdown_read()
  {
    if(CONNECTED_FOR_READ == state_){
      state_ = DISCONNECTED;
    }
    else if(CONNECTED_FOR_READ_AND_WRITE == state_){
      state_ = CONNECTED_FOR_WRITE;
    }
    //
    // ignore re-shutting down.
  }

  void shutdown_write()
  {
    if(CONNECTED_FOR_WRITE == state_){
      state_ = DISCONNECTED;
    }
    else if(CONNECTED_FOR_READ_AND_WRITE == state_){
      state_ = CONNECTED_FOR_READ;
    }
    //
    // ignore re-shutting down.
  }

  void disconnect()
  {
    state_ = DISCONNECTED;
  }

  //
  // Most apps only care if they can read or write
  // to a socket, but all states can be determined.
  bool connected_for_read()
  {
    return CONNECTED_FOR_READ || CONNECTED_FOR_READ_AND_WRITE;
  }
  bool connected_for_write()
  {
    return CONNECTED_FOR_WRITE || CONNECTED_FOR_READ_AND_WRITE;
  }

private:
  enum SOCKET_STATE
  {
    DISCONNECTED,
    CONNECTED_FOR_READ,
    CONNECTED_FOR_WRITE,
    CONNECTED_FOR_READ_AND_WRITE
  };

  SOCKET_STATE state_;
};


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk