1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
#ifndef __AGNI_SOCK_CONN_H
#define __AGNI_SOCK_CONN_H
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <netdb.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <errno.h>
#include "util.h"
#include "debug.h"
#define IRC_BUF_IN_SIZE 1024*64
#define IRC_BUF_OUT_SIZE 1024
/*
Reads ins and spils out IRC lines ended with \r\n.
*/
//?what will happend if there is no string consumer?
typedef struct irc_buf
{
int out_size;
int max_out_size;
int in_size;
int max_in_size;
char *buf_in;
char *buf_out;
int ready;//if 1 there is one full line
} irc_buf;
irc_buf* irc_buf_create();
//put string inside in reverse order =P
int irc_buf_puts(irc_buf *ib, char *in_buf, int sz);
int irc_buf_putc(irc_buf *ib, char c);
int irc_buf_sz(irc_buf *ib);
int irc_buf_ready(irc_buf *ib);
char *irc_buf_line(irc_buf *ib);
int irc_buf_destroy(irc_buf *ib);
//connecto to irc
typedef struct irc_conn
{
int conn_fd; //save connection fd
char *hostname;
char *port;
struct timespec last_read; //when last read where happening
int timeout;
int err; //local error type
int liberr; //save error from libc
} irc_conn;
int irc_connect(char *hostname, char *port);
//list of errors that will give more precise error description, if there is -1 returned one of this errors happened
#define ERR_SC_NONE 0 //no errors
#define ERR_SC_LIBC 1 //error from libc, dont have our own error
#define ERR_SC_PARAM 2 //something wrong with given params
#define ERR_SC_TIMEOUT 3 //timeout
#define ERR_SC_ALLOC 4 //memory allocation error
#define ERR_SC_CONN 5 //somethign with network connection
#define ERR_SC_RXTX 6 //read/write issue
#define ERR_SC_AGAIN 7 //async stuff try read later
#define ERR_SC_ANY 8 //any other error
//irc_conn preallocated, artifacts can stay if something whent wrong
int irc_open(char *hostname, char *port, irc_conn *conn);
//if havent readed for more then [timeout] seconds then assume no connection
int irc_read_timeout(irc_conn *conn, int timeout);
//wrapped up usual read, read non blocking, count how long there was no data,
//and asks to reconnect, use err/liberr to diagnose error
int irc_read(irc_conn *conn, char *buf, size_t sz);
//no extra errors
int irc_reconnect(irc_conn *conn);
//clean dont free
int irc_close(irc_conn *conn);
#endif
|