Server

Name

Server -- TCP Server

Synopsis



struct      GServer;
enum        GServerStatus;
void        (*GServerFunc)                  (GServer *server,
                                             GServerStatus status,
                                             GConn *conn,
                                             gpointer user_data);
GServer*    gnet_server_new                 (const GInetAddr *iface,
                                             gboolean force_port,
                                             GServerFunc func,
                                             gpointer user_data);
void        gnet_server_delete              (GServer *server);

Description

Server represents a TCP server. A callback is called when there's a new connection.

Details

struct GServer

struct GServer
{
  GInetAddr* 	iface;
  gint		port;

  GTcpSocket* 	socket;

  GServerFunc	func;
  gpointer	user_data;

};


enum GServerStatus

typedef enum
{
  GNET_SERVER_STATUS_CONNECT,
  GNET_SERVER_STATUS_ERROR

} GServerStatus;

Status of GServer, passed by GServerFunc.


GServerFunc ()

void        (*GServerFunc)                  (GServer *server,
                                             GServerStatus status,
                                             GConn *conn,
                                             gpointer user_data);

Callback for gnet_server_new(). When a new client connects the function is called with status CONNECT and conn is the new connection. The conn is owned by the callee. If an error occurs, the function is called with status ERROR and conn is NULL.

server : Server
status : Server status
conn : New connection (or NULL if error)
user_data : User data specified in gnet_server_new()


gnet_server_new ()

GServer*    gnet_server_new                 (const GInetAddr *iface,
                                             gboolean force_port,
                                             GServerFunc func,
                                             gpointer user_data);

Create a new GServer object representing a server. The interface is specified as in gnet_tcp_socket_server_new_interface(). Usually, iface is NULL or the iface is created by gnet_inetaddr_new_any() and the port is set to a specific port. The callback is called whenever a new connection arrives or if the socket fails.

FIX: Remove force_port. It's easier for someone to call gnet_server_new again than for me to explain how force port works. (If force_port is TRUE, and the socket with the specified port cannot be created, this function fails. If force_port is FALSE, the function reattempts to create a socket but lets the OS choose the port.

iface : Interface to bind to (NULL if any)
force_port : Fail if can't get requested port
func : Callback to call when a connection is accepted
user_data : Data to pass to callback.
Returns : A new GServer.


gnet_server_delete ()

void        gnet_server_delete              (GServer *server);

Close and delete a GServer.

server : Server to delete.