~ubuntu-branches/ubuntu/trusty/teeworlds/trusty-updates

« back to all changes in this revision

Viewing changes to src/engine/e_network_client.c

  • Committer: Bazaar Package Importer
  • Author(s): Gonéri Le Bouder
  • Date: 2009-04-12 02:32:37 UTC
  • mfrom: (3.2.1 sid)
  • Revision ID: james.westby@ubuntu.com-20090412023237-ufmf1xn0rkjmx6f3
Tags: 0.5.1-2
* Fix the ouput of teeworlds-server --help with /bin/sh ->
  /bin/bash (Closes: #511600)
* Standard version 3.8.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <base/system.h>
 
2
#include "e_network.h"
 
3
#include "e_network_internal.h"
 
4
 
 
5
struct NETCLIENT
 
6
{
 
7
        NETADDR server_addr;
 
8
        NETSOCKET socket;
 
9
        
 
10
        NETRECVINFO recv;
 
11
        NETCONNECTION conn;
 
12
};
 
13
 
 
14
NETCLIENT *netclient_open(NETADDR bindaddr, int flags)
 
15
{
 
16
        NETCLIENT *client = (NETCLIENT *)mem_alloc(sizeof(NETCLIENT), 1);
 
17
        mem_zero(client, sizeof(NETCLIENT));
 
18
        client->socket = net_udp_create(bindaddr);
 
19
        conn_init(&client->conn, client->socket);
 
20
        return client;
 
21
}
 
22
 
 
23
int netclient_close(NETCLIENT *c)
 
24
{
 
25
        /* TODO: implement me */
 
26
        return 0;
 
27
}
 
28
 
 
29
 
 
30
int netclient_disconnect(NETCLIENT *c, const char *reason)
 
31
{
 
32
        dbg_msg("netclient", "disconnected. reason=\"%s\"", reason);
 
33
        conn_disconnect(&c->conn, reason);
 
34
        return 0;
 
35
}
 
36
 
 
37
int netclient_update(NETCLIENT *c)
 
38
{
 
39
        conn_update(&c->conn);
 
40
        if(c->conn.state == NET_CONNSTATE_ERROR)
 
41
                netclient_disconnect(c, conn_error(&c->conn));
 
42
        return 0;
 
43
}
 
44
 
 
45
int netclient_connect(NETCLIENT *c, NETADDR *addr)
 
46
{
 
47
        conn_connect(&c->conn, addr);
 
48
        return 0;
 
49
}
 
50
 
 
51
int netclient_error_string_reset(NETCLIENT *c)
 
52
{
 
53
        mem_zero(c->conn.error_string, sizeof(c->conn.error_string));
 
54
        return 0;
 
55
}
 
56
 
 
57
int netclient_recv(NETCLIENT *c, NETCHUNK *chunk)
 
58
{
 
59
        while(1)
 
60
        {
 
61
                NETADDR addr;
 
62
                int bytes;
 
63
                        
 
64
                /* check for a chunk */
 
65
                if(recvinfo_fetch_chunk(&c->recv, chunk))
 
66
                        return 1;
 
67
                
 
68
                /* TODO: empty the recvinfo */
 
69
                bytes = net_udp_recv(c->socket, &addr, c->recv.buffer, NET_MAX_PACKETSIZE);
 
70
 
 
71
                /* no more packets for now */
 
72
                if(bytes <= 0)
 
73
                        break;
 
74
 
 
75
                if(unpack_packet(c->recv.buffer, bytes, &c->recv.data) == 0)
 
76
                {
 
77
                        if(c->recv.data.flags&NET_PACKETFLAG_CONNLESS)
 
78
                        {
 
79
                                chunk->flags = NETSENDFLAG_CONNLESS;
 
80
                                chunk->client_id = -1;
 
81
                                chunk->address = addr;
 
82
                                chunk->data_size = c->recv.data.data_size;
 
83
                                chunk->data = c->recv.data.chunk_data;
 
84
                                return 1;
 
85
                        }
 
86
                        else
 
87
                        {
 
88
                                if(conn_feed(&c->conn, &c->recv.data, &addr))
 
89
                                        recvinfo_start(&c->recv, &addr, &c->conn, 0);
 
90
                        }
 
91
                }
 
92
        }
 
93
        return 0;
 
94
}
 
95
 
 
96
int netclient_send(NETCLIENT *c, NETCHUNK *chunk)
 
97
{
 
98
        if(chunk->data_size >= NET_MAX_PAYLOAD)
 
99
        {
 
100
                dbg_msg("netclient", "chunk payload too big. %d. dropping chunk", chunk->data_size);
 
101
                return -1;
 
102
        }
 
103
        
 
104
        if(chunk->flags&NETSENDFLAG_CONNLESS)
 
105
        {
 
106
                /* send connectionless packet */
 
107
                send_packet_connless(c->socket, &chunk->address, chunk->data, chunk->data_size);
 
108
        }
 
109
        else
 
110
        {
 
111
                int f = 0;
 
112
                dbg_assert(chunk->client_id == 0, "errornous client id");
 
113
                
 
114
                if(chunk->flags&NETSENDFLAG_VITAL)
 
115
                        f = NET_CHUNKFLAG_VITAL;
 
116
                
 
117
                conn_queue_chunk(&c->conn, f, chunk->data_size, chunk->data);
 
118
 
 
119
                if(chunk->flags&NETSENDFLAG_FLUSH)
 
120
                        conn_flush(&c->conn);
 
121
        }
 
122
        return 0;
 
123
}
 
124
 
 
125
int netclient_state(NETCLIENT *c)
 
126
{
 
127
        if(c->conn.state == NET_CONNSTATE_ONLINE)
 
128
                return NETSTATE_ONLINE;
 
129
        if(c->conn.state == NET_CONNSTATE_OFFLINE)
 
130
                return NETSTATE_OFFLINE;
 
131
        return NETSTATE_CONNECTING;
 
132
}
 
133
 
 
134
int netclient_flush(NETCLIENT *c)
 
135
{
 
136
        return conn_flush(&c->conn);
 
137
}
 
138
 
 
139
int netclient_gotproblems(NETCLIENT *c)
 
140
{
 
141
        if(time_get() - c->conn.last_recv_time > time_freq())
 
142
                return 1;
 
143
        return 0;
 
144
}
 
145
 
 
146
void netclient_stats(NETCLIENT *c, NETSTATS *stats)
 
147
{
 
148
        *stats = c->conn.stats;
 
149
}
 
150
 
 
151
const char *netclient_error_string(NETCLIENT *c)
 
152
{
 
153
        return conn_error(&c->conn);
 
154
}