Tue, 01 Apr 2014 10:28:08 +0200
implemented queen
1
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
1 | /* |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
3 | * |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
4 | * Copyright 2014 Mike Becker. All rights reserved. |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
5 | * |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
8 | * |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
11 | * |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
15 | * |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
27 | * |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
28 | */ |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
29 | |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
30 | #include <stdlib.h> |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
31 | #include <string.h> |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
32 | #include "network.h" |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
33 | |
2
0a08f79c320d
fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
34 | #define new_socket() socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
0a08f79c320d
fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
35 | |
0a08f79c320d
fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
36 | int net_create(Server *server, char* port) { |
0a08f79c320d
fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
37 | server->info = NULL; |
0a08f79c320d
fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
38 | |
0a08f79c320d
fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
39 | struct sockaddr_in addr; |
0a08f79c320d
fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
40 | addr.sin_family = AF_INET; |
0a08f79c320d
fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
41 | addr.sin_addr.s_addr = INADDR_ANY; |
0a08f79c320d
fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
42 | addr.sin_port = htons(atoi(port)); |
0a08f79c320d
fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
43 | |
0a08f79c320d
fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
44 | server->fd = new_socket(); |
1
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
45 | if (server->fd > -1) { |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
46 | int true = 1; |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
47 | setsockopt(server->fd, SOL_SOCKET, SO_REUSEADDR, &true, sizeof(int)); |
2
0a08f79c320d
fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
48 | if (bind(server->fd, (struct sockaddr*)&addr, sizeof(addr))) { |
1
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
49 | server->fd = -1; |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
50 | return 1; |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
51 | } else { |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
52 | return 0; |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
53 | } |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
54 | } else { |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
55 | return 1; |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
56 | } |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
57 | } |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
58 | |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
59 | int getaddrinfo_intrnl(char *host, char *port, struct addrinfo **info) { |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
60 | struct addrinfo hints; |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
61 | memset(&hints, 0, sizeof(hints)); |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
62 | hints.ai_socktype = SOCK_STREAM; |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
63 | hints.ai_protocol = IPPROTO_TCP; |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
64 | |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
65 | return getaddrinfo(host, port, &hints, info); |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
66 | } |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
67 | |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
68 | int net_find(Server *server, char *host, char* port) { |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
69 | memset(server, 0, sizeof(Server)); |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
70 | server->fd = -1; |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
71 | |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
72 | return getaddrinfo_intrnl(host, port, &(server->info)); |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
73 | } |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
74 | |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
75 | int net_listen(Server *server) { |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
76 | listen(server->fd, 1); |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
77 | Client* client = calloc(1, sizeof(Client)); |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
78 | client->fd = -1; |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
79 | server->client = client; |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
80 | |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
81 | client->fd = accept(server->fd, |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
82 | &(client->address), &(client->address_len)); |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
83 | |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
84 | return client->fd == -1; |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
85 | } |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
86 | |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
87 | int net_connect(Server *server) { |
2
0a08f79c320d
fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
88 | |
0a08f79c320d
fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
89 | Client* client = calloc(1, sizeof(Client)); |
0a08f79c320d
fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
90 | client->fd = -1; |
0a08f79c320d
fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
91 | server->fd = new_socket(); |
0a08f79c320d
fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
92 | server->client = client; |
0a08f79c320d
fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
93 | |
0a08f79c320d
fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
94 | if (server->fd == -1) { |
1
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
95 | return 1; |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
96 | } |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
97 | |
2
0a08f79c320d
fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
98 | return connect(server->fd, server->info->ai_addr, server->info->ai_addrlen); |
1
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
99 | } |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
100 | |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
101 | int net_destroy(Server *server) { |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
102 | if (server->info) { |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
103 | freeaddrinfo(server->info); |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
104 | } |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
105 | if (server->client) { |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
106 | shutdown(server->client->fd, SHUT_RDWR); |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
107 | free(server->client); |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
108 | } |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
109 | if (server->fd > -1) { |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
110 | return shutdown(server->fd, SHUT_RDWR); |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
111 | } |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
112 | |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
113 | return EXIT_SUCCESS; |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
114 | } |
2
0a08f79c320d
fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
115 | |
22
41bbfd4d17a3
improved network performance
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
116 | void net_send_code(int socket, uint8_t code) { |
41bbfd4d17a3
improved network performance
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
117 | send(socket, &code, sizeof(uint8_t), 0); |
3
3693fd2eb0e9
changed UI to ncurses session + added network handshake
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
118 | } |
3693fd2eb0e9
changed UI to ncurses session + added network handshake
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
119 | |
22
41bbfd4d17a3
improved network performance
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
120 | void net_send_data(int socket, uint8_t code, void *data, size_t len) { |
41bbfd4d17a3
improved network performance
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
121 | uint8_t pkg[len+1]; |
41bbfd4d17a3
improved network performance
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
122 | pkg[0] = code; |
41bbfd4d17a3
improved network performance
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
123 | memcpy(pkg+1, data, len); |
41bbfd4d17a3
improved network performance
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
124 | send(socket, pkg, len+1, 0); |
2
0a08f79c320d
fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
125 | } |
0a08f79c320d
fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
126 | |
22
41bbfd4d17a3
improved network performance
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
127 | uint8_t net_recieve_code(int socket) { |
41bbfd4d17a3
improved network performance
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
128 | uint8_t code; |
41bbfd4d17a3
improved network performance
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
129 | recv(socket, &code, sizeof(uint8_t), 0); |
41bbfd4d17a3
improved network performance
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
130 | return code; |
2
0a08f79c320d
fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
131 | } |
0a08f79c320d
fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
132 | |
0a08f79c320d
fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
133 | void net_recieve_data(int socket, void *data, size_t len) { |
0a08f79c320d
fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
134 | recv(socket, data, len, 0); |
0a08f79c320d
fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
135 | } |