Back to index

avfs  1.0.1
Classes | Defines | Functions
send.c File Reference
#include "send.h"
#include <stdlib.h>
#include <unistd.h>

Go to the source code of this file.

Classes

struct  buffer

Defines

#define BUFLEN   1024

Functions

static int sock_write (int sock, const char *msg, unsigned int len)
static void init_buffer (struct buffer *buf, int fd)
static void read_buffer (struct buffer *buf, char *msg, unsigned int len)
static void write_buffer (struct buffer *buf, const char *msg, unsigned int len)
static void flush_buffer (struct buffer *buf)
int __av_read_message (int sock, struct avfs_in_message *msg)
int __av_write_message (int sock, struct avfs_out_message *msg)

Class Documentation

struct buffer

Definition at line 16 of file send.c.

Class Members
char buf
int error
int fd
unsigned int len
unsigned int ptr

Define Documentation

#define BUFLEN   1024

Definition at line 15 of file send.c.


Function Documentation

int __av_read_message ( int  sock,
struct avfs_in_message msg 
)

Definition at line 124 of file send.c.

{
    unsigned int lengths[MAXSEG];
    struct buffer buf;
    int i;

    init_buffer(&buf, sock);
    read_buffer(&buf, (void *) lengths, sizeof(int) * MAXSEG);

    for(i = 0; i < MAXSEG; i++) {
        msg->seg[i].len = lengths[i];
        if(lengths[i] != 0) {
            if(msg->seg[i].buf == NULL)
                msg->seg[i].buf = malloc(lengths[i]);
            
            read_buffer(&buf, msg->seg[i].buf, msg->seg[i].len);
        }
    }
    
    return buf.error;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int __av_write_message ( int  sock,
struct avfs_out_message msg 
)

Definition at line 147 of file send.c.

{
    int i;
    unsigned int lengths[MAXSEG];
    struct buffer buf;

    for(i = 0; i < MAXSEG; i++) {
        if(i < msg->num)
            lengths[i] = msg->seg[i].len;
        else
            lengths[i] = 0;
    }

    init_buffer(&buf, sock);

    write_buffer(&buf, (void *) lengths, sizeof(int) * MAXSEG);
    
    for(i = 0; i < msg->num; i++)
        write_buffer(&buf, msg->seg[i].buf, msg->seg[i].len);

    flush_buffer(&buf);

    return buf.error;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void flush_buffer ( struct buffer buf) [static]

Definition at line 116 of file send.c.

{
    if(buf->error != 0)
        return;
    
    buf->error = sock_write(buf->fd, buf->buf, buf->ptr);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void init_buffer ( struct buffer buf,
int  fd 
) [static]

Definition at line 40 of file send.c.

{
    buf->fd = fd;
    buf->ptr = 0;
    buf->len = 0;
    buf->error = 0;
}

Here is the caller graph for this function:

static void read_buffer ( struct buffer buf,
char *  msg,
unsigned int  len 
) [static]

Definition at line 49 of file send.c.

{
    int res;

    while(len > 0) {
        if(buf->error != 0)
            return;

        if(buf->len == 0 && len > BUFLEN) {
            res = read(buf->fd, msg, len);
            if(res <= 0) {
                buf->error = -1;
                return;
            }
            
            len -= res;
            msg += res;
        }
        else if(len <= buf->len) {
            memcpy(msg, buf->buf + buf->ptr, len);
            buf->len -= len;
            buf->ptr += len;
            return;
        }
        else {
            memcpy(msg, buf->buf + buf->ptr, buf->len);
            msg += buf->len;
            len -= buf->len;
            buf->ptr = 0;

            res = read(buf->fd, buf->buf, BUFLEN);
            if(res <= 0)
                buf->error = -1;
            else
                buf->len = res;
        }
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int sock_write ( int  sock,
const char *  msg,
unsigned int  len 
) [static]

Definition at line 24 of file send.c.

{
    int res;

    while(len > 0) {
        res = write(sock, msg, len);
        if(res == -1)
            return -1;

        len -= res;
        msg += res;
    }

    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void write_buffer ( struct buffer buf,
const char *  msg,
unsigned int  len 
) [static]

Definition at line 88 of file send.c.

{
    unsigned int rem;

    while(len > 0) {
        if(buf->error != 0)
            return;

        if(buf->ptr == 0 && len > BUFLEN) {
            buf->error = sock_write(buf->fd, msg, len);
            return;
        }

        rem = BUFLEN - buf->ptr;
        if(len < rem) {
            memcpy(buf->buf + buf->ptr, msg, len);
            buf->ptr += len;
            return;
        }
        
        memcpy(buf->buf + buf->ptr, msg, rem);
        len -= rem;
        msg += rem;
        buf->error = sock_write(buf->fd, buf->buf, BUFLEN);
        buf->ptr = 0;
    }
}

Here is the call graph for this function:

Here is the caller graph for this function: