ks_socket_posix.c File Reference

libkarmaclient socket type (posix implementation). More...

#include "ks_config.h"
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/select.h>
#include "ks_socket.h"
#include "ks_malloc.h"

Go to the source code of this file.

Data Structures

struct  _ks_socket_t

Defines

#define RECV_SIZE   65536
#define MSECS   1000
#define USECS   1000000
#define NSECS   1000000000
#define QUEUE_SIZE_MAX   64

Functions

void ks_socket_set_auth (ks_socket_t *s, const char *principal, const char *credentials)
ks_socket_tks_socket_new (const char *hostname, const char *port, int proto)
void ks_socket_free (ks_socket_t *k)
int ks_socket_connect (ks_socket_t *k)
void ks_socket_close (ks_socket_t *k)
ssize_t ks_socket_send (ks_socket_t *k, ks_bquery_t *q)
ks_response_tks_socket_recv (ks_socket_t *k, ks_string_t *id, int timeout)
ks_response_tks_socket_ask (ks_socket_t *k, ks_bquery_t *q, int timeout)


Detailed Description

libkarmaclient socket type (posix implementation).

A threadsafe socket implementation including caching responses and returning responses from the cache if they've already been received.

Definition in file ks_socket_posix.c.


Define Documentation

#define MSECS   1000

Definition at line 103 of file ks_socket_posix.c.

Referenced by ks_socket_recv().

#define NSECS   1000000000

Definition at line 105 of file ks_socket_posix.c.

Referenced by ks_socket_recv().

#define QUEUE_SIZE_MAX   64

Definition at line 169 of file ks_socket_posix.c.

#define RECV_SIZE   65536

Definition at line 38 of file ks_socket_posix.c.

#define USECS   1000000

Definition at line 104 of file ks_socket_posix.c.

Referenced by ks_socket_recv().


Function Documentation

ks_response_t* ks_socket_ask ( ks_socket_t k,
ks_bquery_t q,
int  timeout 
)

A threadsafe implementation to retrieve a response to a specific query from the socket. It is important that the value returned by this function is tested for failure, as other functions that take the ks_response_t pointer as an argument will not test that it is not NULL before trying to dereference it.

This method makes at most 3 retries, doubling the timeout each time.

Parameters:
k A pointer to the ks_socket_t to retrieve the response from.
q A pointer to the ks_bquery_t to send to the socket.
timeout The timeout for each retry, in milliseconds.
Returns:
A pointer to a ks_response_t, or NULL on an error.

Definition at line 734 of file ks_socket_posix.c.

References ks_bquery_id_get(), ks_socket_recv(), ks_socket_send(), and KS_TIMEOUT_DEFAULT.

Referenced by main().

void ks_socket_close ( ks_socket_t k  ) 

Close a connected socket, if it is connected.

Parameters:
k A pointer to the ks_socket_t to close.
Returns:
Nothing.

Definition at line 390 of file ks_socket_posix.c.

References _ks_socket_t::socket.

Referenced by ks_socket_connect(), ks_socket_free(), and ks_socket_send().

int ks_socket_connect ( ks_socket_t k  ) 

Connect a ks_socket_t. This looks up the hostname and port to connect to, and connects a new socket to the address.

Parameters:
k A pointer to the ks_socket_t to connect.
Returns:
0 on success, non zero otherwise.

Definition at line 351 of file ks_socket_posix.c.

References _ks_socket_t::addr, ks_error(), ks_socket_close(), _ks_socket_t::proto, and _ks_socket_t::socket.

Referenced by ks_socket_send(), and main().

void ks_socket_free ( ks_socket_t k  ) 

Destroy a given ks_socket_t, close it if it's open and free any associated allocated memory.

Parameters:
k A pointer to the ks_socket_t to be destroyed.
Returns:
Nothing.

Definition at line 322 of file ks_socket_posix.c.

References _ks_socket_t::c_cache, _ks_socket_t::cache, _ks_socket_t::creds, _ks_socket_t::hostname, ks_array_fini(), ks_free(), ks_socket_close(), _ks_socket_t::m_cache, _ks_socket_t::m_recv, _ks_socket_t::m_send, _ks_socket_t::port, and _ks_socket_t::princ.

Referenced by main().

ks_socket_t* ks_socket_new ( const char *  hostname,
const char *  port,
int  proto 
)

Create a new ks_socket_t to connect to hostname:port using proto.

Parameters:
hostname A NUL terminated string, which is the host to connect to.
port A NUL terminated string, which is the port to connect to.
proto The protocol number to use.
Returns:
A pointer to a new ks_socket_t on success, NULL otherwise.

Definition at line 275 of file ks_socket_posix.c.

References _ks_socket_t::c_cache, _ks_socket_t::cache, _ks_socket_t::creds, _ks_socket_t::hostname, ks_array_init(), ks_error(), ks_malloc(), KS_SLAVE_HOST_DEFAULT, KS_SLAVE_PORT_DEFAULT, _ks_socket_t::m_cache, _ks_socket_t::m_recv, _ks_socket_t::m_send, _ks_socket_t::port, _ks_socket_t::princ, _ks_socket_t::proto, and _ks_socket_t::socket.

Referenced by main().

ks_response_t* ks_socket_recv ( ks_socket_t k,
ks_string_t id,
int  timeout 
)

A threadsafe implementation to retrieve a response with a specific id from the socket.

Parameters:
k A pointer to the ks_socket_t to retrieve the response from.
id The id of the response to retrieve.
timeout The timeout for the call, in milliseconds.
Returns:
A pointer to a ks_response_t, or NULL on an error.

Definition at line 648 of file ks_socket_posix.c.

References _ks_socket_t::c_cache, ks_error(), ks_response_id(), ks_string_equals(), KS_TIMEOUT_DEFAULT, _ks_socket_t::m_cache, _ks_socket_t::m_recv, MSECS, NSECS, and USECS.

Referenced by ks_socket_ask().

ssize_t ks_socket_send ( ks_socket_t k,
ks_bquery_t q 
)

Send ks_bquery_t (q) over ks_socket_t (k).

Parameters:
k A pointer to the ks_socket_t to send the query to.
q A pointer to the ks_bquery_t to send to the socket.
Returns:
On success returns the number of characters sent, on error -1 is returned.

Definition at line 424 of file ks_socket_posix.c.

References _ks_socket_t::creds, ks_bquery_has_auth(), ks_bquery_packet(), ks_bquery_set_auth(), ks_error(), ks_free(), ks_malloc(), ks_socket_close(), ks_socket_connect(), ks_string_free(), ks_string_get(), ks_string_length(), _ks_socket_t::m_send, _ks_socket_t::princ, _ks_socket_t::proto, and _ks_socket_t::socket.

Referenced by ks_socket_ask().

void ks_socket_set_auth ( ks_socket_t s,
const char *  principal,
const char *  credentials 
)

Definition at line 141 of file ks_socket_posix.c.

References _ks_socket_t::creds, ks_free(), ks_malloc(), and _ks_socket_t::princ.

Referenced by main().


Generated on Wed May 9 01:01:57 2007 for libkarmaclient by  doxygen 1.5.1