Files
sdk/runtime/bin/socket.dart
T
sgjesse@google.com 516d71ab0b Change the handling of closing sockets
Sockets now supports being half-closed for either reading or writing. When a socket is closed it is by default closed for both read and write and the underlying file descriptor is destroyed. However the socket close can be asked to only half-close the socket to send end of stream to the other end and still have the ability to receive more data.

The close event on a socket is only emitted when the socket is closed by the other end. Both half-close and full-close by the other end is reported as a close event. If a socket is already half closed the close event will automatically destroy the socket.

The streams on the sockets also takes advantage of this. A socket input stream will report a close event when the other end closed. A socket output stream will half-close the socket when close is called making it possible to still receive data on the input stream. When both streams have been closed the socket is destroyed.

For sockets operating on pipes they are initially created as half-closed for either reading or writing depending on which type of pipe a socket object is based on. A pipe for writing will start half-closed for reading so if it is only half-closed for writing the socket will still be destroyed. Same with a pipe for reading that will start half-closed for writing and when a close event is received half-closing the other direction will destroy the socket. Socket objects based on pipes are only exposed through streams.

Extended the socket close test to test a number of different scenarios.

Also refactor the socket data C++ object to encapsulate more socket information and operations.

Now the Linux and Mac OS versions are 100% the same as it turned out that using POLLRDHUP on Linux was not required any more.

R=ager@google.com

BUG=
TEST=

Review URL: http://codereview.chromium.org//8437090

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@1196 260f80e4-7a28-3924-810f-c04153c831b5
2011-11-04 12:34:43 +00:00

134 lines
3.4 KiB
Dart

// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
interface ServerSocket factory _ServerSocket {
/*
* Constructs a new server socket, binds it to a given address and port,
* and listens on it.
*/
ServerSocket(String bindAddress, int port, int backlog);
/*
* Accepts a connection to this socket.
*/
Socket accept();
/*
* The connection handler gets executed when there are incoming connections
* on the socket.
*/
void set connectionHandler(void callback());
/*
* The error handler gets executed when a socket error occurs.
*/
void set errorHandler(void callback());
/*
* Returns the port used by this socket.
*/
int get port();
/*
* Closes the socket.
*/
void close();
}
interface Socket factory _Socket {
/*
* Constructs a new socket and connects it to the given host on the given
* port.
*/
Socket(String host, int port);
/*
* Returns the number of received and non-read bytes in the socket that
* can be read.
*/
int available();
/*
* Reads up to [count] bytes of data from the socket and stores them into
* buffer after buffer offset [offset]. The number of successfully read
* bytes is returned. This function is non-blocking and will only read data
* if data is available.
*/
int readList(List<int> buffer, int offset, int count);
/*
* Writes up to [count] bytes of the buffer from [offset] buffer offset to
* the socket. The number of successfully written bytes is returned. This
* function is non-blocking and will only write data if buffer space is
* available in the socket. It will return 0 if an error occurs, e.g., no
* buffer space available.
*/
int writeList(List<int> buffer, int offset, int count);
/*
* The connect handler gets called when connection to a given host
* succeeded.
*/
void set connectHandler(void callback());
/*
* The data handler gets called when data becomes available at the socket.
*/
void set dataHandler(void callback());
/*
* The write handler gets called when the socket becomes available for
* writing.
*/
void set writeHandler(void callback());
/*
* The close handler gets called when a the last byte have been read
* from a socket. At this point the socket might still be open for
* writing for sending more data.
*/
void set closeHandler(void callback());
/*
* The error handler gets called when a socket error occurs.
*/
void set errorHandler(void callback());
/*
* Returns input stream to the socket.
*/
InputStream get inputStream();
/*
* Returns output stream of the socket.
*/
OutputStream get outputStream();
/*
* Returns the port used by this socket.
*/
int get port();
/*
* Closes the socket. Calling [close] will never throw an exception
* and calling it several times is supported. If [halfClose] is true
* the socket will only be closed for writing and it might still be
* possible to read data. Calling [close] will not trigger a call to
* the [closeHandler].
*/
void close([bool halfClose]);
}
class SocketIOException implements Exception {
const SocketIOException([String this.message = ""]);
String toString() => "SocketIOException: $message";
/*
* Contains the exception message.
*/
final String message;
}