ceptr
 All Data Structures Files Functions Variables Typedefs Macros Modules Pages
stream.c File Reference

stream abstraction implementation More...

#include "stream.h"
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include "process.h"
#include "debug.h"
#include "util.h"
Include dependency graph for stream.c:

Go to the source code of this file.

#define BACKLOG   10
 
char * DELIM_LF = "\n"
 
char * DELIM_CRLF = "\r\n"
 
void __st_realloc_reader (Stream *st)
 
size_t __st_unix_stream_load (Stream *st)
 
size_t __st_socket_stream_load (Stream *st)
 
void __st_scan (Stream *st)
 
char * ss2str (int s)
 
void __st_stream_read (Stream *st)
 
void * _st_stream_read (void *arg)
 
Stream__st_alloc_stream ()
 
void __st_start_reader (Stream *s, size_t reader_buffer_size)
 
Stream__st_new_unix_stream (FILE *stream, size_t reader_buffer_size)
 
Stream_st_new_socket_stream (int sockfd)
 
void * get_in_addr (struct sockaddr *sa)
 
void * __st_socket_stream_accept (void *arg)
 
SocketListener_st_new_socket_listener (int port, lisenterConnectionCallbackFn fn, void *callback_arg, char *delim)
 
void _st_close_listener (SocketListener *l)
 
void _st_start_read (Stream *st)
 
void _st_data_consumed (Stream *st)
 
void _st_kill (Stream *st)
 
void _st_free (Stream *st)
 
int _st_write (Stream *st, char *buf, size_t len)
 
int _st_writeln (Stream *stream, char *str)
 

Detailed Description

stream abstraction implementation

Definition in file stream.c.

Function Documentation

Stream* __st_new_unix_stream ( FILE *  stream,
size_t  reader_buffer_size 
)

create a new stream object of the unix file flavor

if the the stream is a read stream you must pass in a starting buffer size

Definition at line 300 of file stream.c.

void __st_scan ( Stream st)

scan a stream's buffer for a unit

currently a unit simply is defined by a new-line char

Todo:
allow other delimiters, or even simply a length, to mark off a unit.

Definition at line 125 of file stream.c.

size_t __st_socket_stream_load ( Stream st)

load socket data into the stream buffer

returns the number of bytes loaded if 0 bytes loaded, st->err will hold the reason, which is either because socket error condition, or a some other unix error (from errno)

if the buffer is already full when this function is called, it will attempt to increase the buffer size with a realloc before reading.

Definition at line 95 of file stream.c.

void __st_start_reader ( Stream s,
size_t  reader_buffer_size 
)

set up a stream as a reader by allocating a read buffer creating the reader thread, and setting up the conditional mutex that gets called to activate reading in that thread

Definition at line 276 of file stream.c.

void __st_stream_read ( Stream st)

reads a unit from a stream

calls the stream loading function according to stream type when needed to get stream data into the read buffer, and then scans along that buffer setting the StreamHasData flag for each unit found. If the scan returns Partial (i.e. no terminator found yet) and then stream is terminated (indicate by 0 value from the load function) then the partial will be treated as a successfully read unit.

Definition at line 178 of file stream.c.

size_t __st_unix_stream_load ( Stream st)

load file data into the stream buffer

returns the number of bytes loaded if 0 bytes loaded, st->err will hold the reason, which is either because and EndOfFile condition, or a some other unix error (from errno)

if the buffer is already full when this function is called, it will attempt to increase the buffer size with a realloc before reading.

Definition at line 49 of file stream.c.

void _st_close_listener ( SocketListener l)

close a socket listener

Definition at line 444 of file stream.c.

void _st_data_consumed ( Stream st)

mark that data in a stream has been consumed

Definition at line 470 of file stream.c.

void _st_free ( Stream st)

release the resources of a stream object

Definition at line 507 of file stream.c.

void _st_kill ( Stream st)

kill a stream

if a reader stream is blocked and waiting, calls _st_start_read so that the reader thread can complete.

Definition at line 481 of file stream.c.

SocketListener* _st_new_socket_listener ( int  port,
lisenterConnectionCallbackFn  fn,
void *  callback_arg,
char *  delim 
)

create a socket listener on a port which will generate socket streams when connections arrive

Definition at line 372 of file stream.c.

Stream* _st_new_socket_stream ( int  sockfd)

create a new stream object of the unix file flavor

Definition at line 314 of file stream.c.

void _st_start_read ( Stream st)

wake the stream reader thread

Definition at line 455 of file stream.c.

int _st_write ( Stream st,
char *  buf,
size_t  len 
)

write to a stream

Definition at line 534 of file stream.c.

int _st_writeln ( Stream stream,
char *  str 
)

write a line to a stream using the delim as the EOL

Definition at line 563 of file stream.c.