File lib/socket_co.lua

Socket communication using coroutines, integrated with the Gtk message loop to allow background transfers. Copyright (C) 2007 Wolfgang Oertl

Functions

check (ioc, ...) Read a response from the server, and compare with allowed status codes.
connect (host, port, buffered) Connect to the server.
create_io_channel (sock, buffered) Given a socket, construct a GIOChannel around it.
flush (ioc) Make sure all the data is actually written to the socket.
get_reply (ioc) Read a reply from the server This is somewhat protocol specific, but works for FTP and HTTP.
read_chars (ioc, length) Read some data from the server.
receive_line (ioc) Read a line from the server; if no input is available, yield.
source_buffer (arg, op, len) A data source for upload which reads from a buffer in memory.
source_chain (arg, op, len) Implements a source that is a chain of multiple subsources.
source_file (arg, op, len) A data source that reads from a file.
write_chars (ioc, data, do_flush) Send a block of data over the given socket.


Functions

check (ioc, ...)
Read a response from the server, and compare with allowed status codes. returns the response if OK, else NIL and an error message

Parameters

  • ioc:
  • ...:
connect (host, port, buffered)
Connect to the server. Returns the new GIOChannel and the socket. Be sure to keep a reference to the socket, otherwise it will be destroyed!

Parameters

  • host: Host to connect to; IP address or DNS name.
  • port: The port to connect to; must be numeric.
  • buffered: true to use buffered sockets; don't do this.

Return value:

GIOChannel, or nil + error message.
create_io_channel (sock, buffered)
Given a socket, construct a GIOChannel around it.

Parameters

  • sock: A socket
  • buffered: Should be false

Return value:

a GIOChannel for the socket, and the socket itself
flush (ioc)
Make sure all the data is actually written to the socket. NOTE: There is a problem with Glib. The watch G_IO_OUT that is placed on the channel only checks that the channel's output buffers are not full. I don't see a way to wait on the channel such that the socket is ready to take more data. Therefore, this goes into a 100% CPU busy loop. NOTE 2: in this form, probably does nothing. A flush would have to be performed on the socket, not on the channel.

Parameters

  • ioc:
get_reply (ioc)
Read a reply from the server This is somewhat protocol specific, but works for FTP and HTTP.

Parameters

  • ioc:
read_chars (ioc, length)
Read some data from the server. This has to take our own buffering into account. It can return UP TO length bytes but may return less. Calling it again will then return more, unless the server stops sending data.

Parameters

  • ioc: GIOChannel
  • length: Max. bytes to read

Return value:

Buffer, or nil and message
receive_line (ioc)
Read a line from the server; if no input is available, yield. Buffering is done internally.

Parameters

  • ioc:
source_buffer (arg, op, len)
A data source for upload which reads from a buffer in memory.

Parameters

  • arg: The usual arg
  • op: The operation, may be open, get-length, or read
  • len: For read, how many bytes to return at most.

Return value:

For read, the next len bytes; nil at EOF
source_chain (arg, op, len)
Implements a source that is a chain of multiple subsources. set arg.source_parts as an array of { source=..., source_data=... }

Parameters

  • arg:
  • op:
  • len:
source_file (arg, op, len)
A data source that reads from a file. XXX The file is read synchronously; it could be otherwise, yielding as required.

Parameters

  • arg:
  • op:
  • len:
write_chars (ioc, data, do_flush)
Send a block of data over the given socket.

Parameters

  • ioc:
  • data:
  • do_flush:

Valid XHTML 1.0!