Module: cogen.core.sockets

Socket-only coroutine operations and Socket wrapper. Really - the only thing you need to know for most stuff is the Socket class.

Classes

Accept

Returns a (conn, addr) tuple when the operation completes.

Connect

Read

Example usage:

yield sockets.Read(socket_object, buffer_length)

buffer_length is max read size, BUT, if if there are buffers from ReadLine return them first.

ReadAll

Run this operation till we've read len bytes.

ReadLine

Run this operation till we read a newline (n) or we have a overflow.

SendFile

Uses underling OS sendfile call or a regular memory copy operation if there is no sendfile. You can use this as a WriteAll if you specify the length. Usage:

yield sockets.SendFile(file_object, socket_object, 0, blocksize=0)
    # there will be only one send operation (if successfull)
    # that meas the whole file will be read in memory if there is
    #no sendfile

yield sockets.SendFile(file_object, socket_object, 0, file_size)
    # this will hang if we can't read file_size bytes
    #from the file

Socket

A wrapper for socket objects, sets nonblocking mode and adds some internal bufers and wrappers. Regular calls to the usual socket methods return operations for use in a coroutine.

So you use this in a coroutine like:

sock = Socket(family, type, proto) # just like the builtin socket module
yield sock.read(1024)

Constructor details:

Socket([family[, type[, proto]]]) -> socket object

Open a socket of the given type. The family argument specifies the address family; it defaults to AF_INET. The type argument specifies whether this is a stream (SOCK_STREAM, this is the default) or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0, specifying the default protocol. Keyword arguments are accepted.

A socket object represents one endpoint of a network connection.

SocketOperation

This is a generic class for a operation that involves some socket call.

A socket operation should subclass WriteOperation or ReadOperation, define a run method and call the __init__ method of the superclass.

Write

Write the buffer to the socket and return the number of bytes written.

WriteAll

Run this operation till all the bytes have been written.

Functions

getdefaulttimeout ()

setdefaulttimeout (timeout)

Set the default timeout used by the socket wrapper (Socket class)