<?xml version="1.0"?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN" 
               "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" [
<!ENTITY gnet-gnet SYSTEM "xml/gnet.xml">
<!ENTITY gnet-inetaddr SYSTEM "xml/inetaddr.xml">
<!ENTITY gnet-mcast SYSTEM "xml/mcast.xml">
<!ENTITY gnet-tcp SYSTEM "xml/tcp.xml">
<!ENTITY gnet-udp SYSTEM "xml/udp.xml">
<!ENTITY gnet-conn SYSTEM "xml/conn.xml">
<!ENTITY gnet-conn-http SYSTEM "xml/conn-http.xml">
<!ENTITY gnet-iochannel SYSTEM "xml/iochannel.xml">
<!ENTITY gnet-md5 SYSTEM "xml/md5.xml">
<!ENTITY gnet-pack SYSTEM "xml/pack.xml">
<!ENTITY gnet-server SYSTEM "xml/server.xml">
<!ENTITY gnet-sha SYSTEM "xml/sha.xml">
<!ENTITY gnet-uri SYSTEM "xml/uri.xml">
<!ENTITY gnet-socks SYSTEM "xml/socks.xml">
<!ENTITY gnet-unix SYSTEM "xml/unix.xml">
<!ENTITY gnet-ipv6 SYSTEM "xml/ipv6.xml">
<!ENTITY gnet-base64 SYSTEM "xml/base64.xml">
<!ENTITY version SYSTEM "version.xml">
<!ENTITY hash     "#">
<!ENTITY at       "@">
<!ENTITY percent  "&percnt;">
<!ENTITY parens   "()">
<!ENTITY space    " ">
<!ENTITY comstart "/*">
<!ENTITY comend   "*/">
]>

<book>

  <bookinfo>

    <title>GNet Network Library Reference Manual</title>
    <releaseinfo>for GNet &version;</releaseinfo>

    <authorgroup>
      <author>
        <firstname>David</firstname>
        <surname>Helder</surname>
        <affiliation>
	  <address>
	    <email>dhelder@umich.edu</email>
	  </address>
        </affiliation>
      </author>
    </authorgroup>

    <copyright>
      <year>2000-2005</year>
      <holder>David Helder</holder>
    </copyright>	

    <legalnotice>
      <para>
	Permission is granted to make and distribute verbatim copies
	of this manual provided the copyright notice and this
	permission notice are preserved on all copies.
      </para>

      <para>
	Permission is granted to copy and distribute modified versions
	of this manual under the conditions for verbatim copying,
	provided also that the entire resulting derived work is
	distributed under the terms of a permission notice identical
	to this one.
      </para>

      <para>
	Permission is granted to copy and distribute translations of
	this manual into another language, under the above conditions
	for modified versions.
      </para>
    </legalnotice>

    <abstract>
      <para>
	This manual documents GNet, a network library.  It gives an
	overview of GNet, discusses examples that come with GNet, and
	includes detailed documentation of the GNet API.
      </para>
    </abstract>

  </bookinfo>

  <!-- ******************************************* -->

  <chapter id="gnet-overview">
    <title>GNet Overview</title>

    <para>
      GNet is a network library.  It is written in C, object-oriented,
      and built upon GLib.  It is intended to be easy to use and port.
    </para>

    <para>
      Features:
      <itemizedlist>
	<listitem><para>TCP "client" and "server" sockets</para></listitem>
        <listitem><para>UDP and IP Multicast sockets</para></listitem>
	<listitem><para>High-level TCP connection and server objects</para></listitem>
        <listitem><para>Asynchronous socket IO</para></listitem>
        <listitem><para>HTTP client object</para></listitem>

        <listitem><para>Internet address abstraction</para></listitem>
	<listitem><para>Asynchronous DNS lookup</para></listitem>
	<listitem><para>IPv4 and IPv6 support</para></listitem>

	<listitem><para>Byte packing and unpacking</para></listitem>
	<listitem><para>URI parsing</para></listitem>
	<listitem><para>SHA and MD5 hashes</para></listitem>
	<listitem><para>Base64 encoding and decoding</para></listitem>
	<listitem><para>SOCKS support</para></listitem>
      </itemizedlist>
    </para>

    <para>
      GNet is licensed under the GNU Lesser General Public License.
      Read COPYING for information on the license (this file comes
      with the GNet distribution).
    </para>

  </chapter>

  <!-- ******************************************* -->

  <chapter id="gnet-developers">
    <title>GNet for developers</title>

    <sect1 id="gnet-developers-compiling">
      <title>Compiling programs with GNet</title>

      <para>
	If you are a developer and you want to use GNet in your
	program, you can use the pkg-config script to figure out
	which flags need to set when compiling.  For example, type
	<userinput>gcc main.c `pkg-config gnet --cflags --libs`</userinput>
	to compile the program main.c with GNet and link it to GNet.
      </para>

      <para>
	If you use autoconf and automake, you can the pkg-config
	macros to set the appropriate variables.  Just add this to
	your configure.in:
      
	<programlisting>
PKG_CHECK_MODULES(GNET, gnet &gt; 2.0.0,
  [LIBS="$LIBS $GNET_LIBS" CFLAGS="$CFLAGS $GNET_CFLAGS"],
  AC_MSG_ERROR(Cannot find GNet: Is gnet-config in path?))
	</programlisting>

      </para>
    </sect1>

    <sect1 id="gnet-developers-tips">
      <title>Tips and tricks</title>

      <para>
	GNet was designed so that most of the implementation is hidden
	from the programmer.  You should not have to include any
	network header files other than
	<filename>gnet.h</filename>.
      </para>
      
      <para>
	By convention, integers are usually transfered in big-endian,
	or network-byte, order.  GLib has conversion functions for
	shorts and ints: <function>g_ntohs</function> (network-to-host
	short), <function>g_htons</function> (host-to network short),
	<function>g_ntohl</function> (network-to-host long), and
	<function>g_htonl</function> (host-to-network long).
      </para>

    </sect1>

    <sect1 id="gnet-developers-async">
      <title>Hiding blocking</title>

      <para>
	Many GNet functions block.  That is, they do not return
	immediately.  For example
	<function>gnet_inetaddr_new</function> performs a DNS lookup
	that may take several seconds.  This is acceptable for many
	programs.  But, it is not tolerable in interactive GUI
	programs or high-performance servers.  
      </para>

      <para>
	There are two ways to hide blocking.  The first is to use
	threads.  Pthreads is a common thread library available on
	most systems.  GLib 2.0 also includes thread support.  The
	second method is to use asynchronous functions.  Asynchronous
	functions return immediately and call a callback when the
	operation is completed.  For example,
	<function>gnet_inetaddr_new_async</function> begins an
	asynchronous DNS lookup and returns immediately.  When the
	lookup is complete, a callback is called with the GInetAddr.
	Most blocking function in GNet have an asynchronous
	counterpart.
      </para>

      <para>
	To use GNet's asynchronous functions, you must also use the
	GLib main event loop.  Most GTK and Gnome program already do
	this.
      </para>

      <para>
	Another common blocking operation is reading or writing to a
	GIOChannel (a GLib object).
	<function>g_io_channel_read</function> blocks until there is
	data available to read.
	<function>g_io_channel_write</function> blocks until there is
	OS buffer space to write to.  To determine when a GIOChannel
	can be read to or written from without blocking, use GLib's
	<function>g_io_add_watch</function> to set a watch.  A
	callback is called when the GIOChannel becomes readable or
	writable.  See the GLib documentation for more information and
	GNet's echoclient-async and echoserver-async for an example.
      </para>

    </sect1>

  </chapter>

  <!-- ******************************************* -->

  <chapter id="gnet-examples">
    <title>GNet Examples</title>

    <para>
      GNet comes with several example programs.  They are intended to
      be used by developers as examples of how to use GNet.  The
      example programs are in the examples directory that comes with
      the source code.
    </para>

    <para>
      The tests in the tests directory also demonstrate to use several
      modules: MD5/SHA, InetAddr, IPv6, Pack/Unpack, and URI.
    </para>

    <sect1 id="gnet-examples-echo">
      <title>echoclient and echoserver</title>

      <para>
	The echoclient connects to the echoserver and sends data it
	reads in from the user.  echoserver then sends it back and
	echoclient prints it out.  These programs demonstrate how to
	write a basic client and server.  There are three versions of
	the TCP echoclient and echoserver.  Each demonstrates a
	different method of using GNet: blocking, asynchronous, and
	object GConn/GServer.  There is also a blocking UDP example
	and a Unix sockets example.
      </para>

      <para>
	echoclient/echoserver.  The blocking echoserver accepts a
	connection and reads and writes to the socket until the socket
	is closed.  No more than one echoclient can be connected at
	once.  A good, robust server wouldn't use this method, but it
	is suitable for many simple applications.
      </para>

      <para>
	echoclient-async/echoserver-async.  The
	asynchronous server does not block while reading or writing to
	a socket or waiting for a connect.  Since it's never blocked,
	it can accept new connections when it isn't reading or writing
	to another socket.  The advantage of the this method is that
	multiple clients can be served at once.
      </para>

      <para>
	echoclient-gconn/echoserver-gserver.
	echoclient-gconn uses GNet's GConn object.  echoserver-gserver
	uses GNet's GServer object.  GConn and GServer are high-level
	wrappers around GTcpSocket.  Using GConn and GServer is not as
	flexible as using GTcpSocket directly, but it is much easier
	and is flexible enough for most applications.
      </para>

      <para>
	There is no example using threads.  If you would like to
	contribute one, please send it to us.
      </para>

    </sect1>


    <sect1 id="gnet-examples-other">
      <title>dnslookup, hash, hfetch, hostinfo, and SDR</title>

      <para>
	dnslookup.  dnslookup performs a DNS
	lookup of a hostname or a reverse DNS lookup of an address.
	It demonstrates how to use both the blocking and asynchronous
	methods of DNS lookups.
      </para>

      <para>
	hash.  hash computes the MD5 and SHA
	hashes of a file.
      </para>

      <para>
	hfetch.  hfetch is a simple HTTP file fetcher.
      </para>

      <para>
	hostinfo.  hostinfo prints information
	about the host including it's domain name and interfaces.
      </para>

      <para>
	SDR.  SDR prints multimedia session
	announcements.  It demonstrates how to use multicast sockets
	in GNet.  SDR will only work if your network supports IP
	Multicast.  In fact, SDR is a good test for this.  If the
	network supports multicast, SDR will print announcements after
	a few seconds, otherwise, it will print nothing.
      </para>

    </sect1>

  </chapter>

  <!-- ******************************************* -->

  <chapter id="libgnet-reference">
    <title>GNet Library Reference</title>

    <para>
      This section contains the API reference for gnet. All the public
      interfaces are documented here. This reference guide is built by
      extracting comments from the code sources.
    </para>

    &gnet-gnet;
    &gnet-inetaddr;
    &gnet-tcp;
    &gnet-udp;
    &gnet-mcast;
    &gnet-conn-http;
    &gnet-conn;
    &gnet-server;
    &gnet-iochannel;
    &gnet-uri;
    &gnet-base64;
    &gnet-pack;
    &gnet-md5;
    &gnet-sha;
    &gnet-unix;
    &gnet-ipv6;
    &gnet-socks;

  </chapter>
</book>
