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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
/* File: z-sock.h */
/*
* Copyright (c) 2002 DarkGod
*
* This software may be copied and distributed for educational, research,
* and not for profit purposes provided that this copyright and statement
* are included in all such copies.
*/
#ifndef INCLUDED_Z_SOCK_H
#define INCLUDED_Z_SOCK_H
#include "h-basic.h"
/*
* This represents an IP connection
*/
typedef struct ip_connection ip_connection;
/* A callback used when the connection suddently dies */
typedef void (*lose_connection_hook)(ip_connection *conn);
struct ip_connection
{
bool setup; /* Has it been setted up yet? */
long conn_ip; /* The IP where to connect to */
int conn_port; /* The port where to connect to */
byte conn_type; /* Type of connection */
bool connected; /* The connection status */
void *socket; /* The socket for the connection */
lose_connection_hook lost_conn; /* Called when the conenction dies */
bool server; /* Is it a server socket ? */
};
/*
* Possible connection types
*/
#define ZSOCK_TYPE_TCP 1
/* #define ZSOCK_TYPE_UDP 2 */
/*
* The time in milliseconds when to call the sockets callbacks for the timer
*/
#define ZSOCK_TIMER_DELAY 100
/* Timer callbacks */
typedef void (*timer_callback)(void);
typedef struct timer_callback_list timer_callback_list;
struct timer_callback_list
{
timer_callback callback;
timer_callback_list *next;
};
/*
* Hooks needed for a main-foo.c to be sock-able
*/
typedef struct zsock_hooks zsock_hooks;
struct zsock_hooks
{
/* Creates a struct */
ip_connection *(*new_connection)(void);
/* Free it */
void (*free_connection)(ip_connection *c);
/* Setup a connection, but do NOT connect */
bool (*setup)(ip_connection *conn, cptr conn_ip, int port, byte conn_type, bool server);
/* Unsetup a connection, but and DO close before if needed */
bool (*unsetup)(ip_connection *conn);
/* Open(connect) a well setup-ed connection */
bool (*open)(ip_connection *conn);
/* Close a connected connection */
bool (*close)(ip_connection *conn);
/* Send data on the connection */
bool (*write)(ip_connection *conn, cptr str, int *size);
/* Read data on the connection */
bool (*read)(ip_connection *conn, char *str, int *len, bool raw);
/* Send data on the connection -- easy to use */
bool (*write_simple)(ip_connection *conn, cptr str);
/* Read data on the connection -- easy to use */
bool (*read_simple)(ip_connection *conn, char *str, int len);
/* Set the dying connection callback */
void (*set_lose_connection)(ip_connection *conn, lose_connection_hook hook);
/* Accept a connection */
bool (*accept)(ip_connection *conn, ip_connection *child);
/* Check if there is any data to be read and return instantly in any case */
bool (*can_read)(ip_connection *conn);
/* Wait until there is any data to be read and return after seconds time in any case */
bool (*wait)(ip_connection *conn, int seconds);
/*
* Timer stuff, I hope I can make that look better
*/
int __timers;
timer_callback_list *__timer_callbacks;
/* Setup the timer */
bool (*add_timer)(timer_callback callback);
/* Remove the timer */
bool (*remove_timer)(timer_callback callback);
};
extern zsock_hooks zsock;
extern bool zsock_init(void);
#endif
|