Wed, 26 Mar 2014 14:12:59 +0100
completed eval_move
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 | #ifndef TCHESS_NETWORK_H |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
31 | #define TCHESS_NETWORK_H |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
32 | |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
33 | #include <sys/socket.h> |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
34 | #include <netdb.h> |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
35 | |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
36 | #ifdef __cplusplus |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
37 | extern "C" { |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
38 | #endif |
2
0a08f79c320d
fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
39 | |
3
3693fd2eb0e9
changed UI to ncurses session + added network handshake
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
40 | #define NETCODE_ACCEPT 0x00 |
3693fd2eb0e9
changed UI to ncurses session + added network handshake
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
41 | #define NETCODE_DECLINE 0x01 |
3693fd2eb0e9
changed UI to ncurses session + added network handshake
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
42 | #define NETCODE_GAMEINFO 0x10 |
7
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
43 | #define NETCODE_MOVE 0x20 |
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
44 | #define NETCODE_SURRENDER 0x21 |
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
45 | #define NETCODE_REMIS 0x22 |
11
08d7a6e3ec31
added network messages for check and checkmate
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
46 | #define NETCODE_CHECK 0x23 |
08d7a6e3ec31
added network messages for check and checkmate
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
47 | #define NETCODE_CHECKMATE 0x24 |
3
3693fd2eb0e9
changed UI to ncurses session + added network handshake
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
48 | |
11
08d7a6e3ec31
added network messages for check and checkmate
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
49 | #define NETCODE_VERSION 4 |
1
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
50 | |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
51 | typedef struct { |
2
0a08f79c320d
fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
52 | int fd; /* -1, if we are the client */ |
1
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
53 | struct sockaddr address; |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
54 | socklen_t address_len; |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
55 | } Client; |
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 | typedef struct { |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
58 | int fd; |
2
0a08f79c320d
fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
59 | struct addrinfo* info; /* NULL, if we are the server */ |
1
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
60 | Client *client; |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
61 | } Server; |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
62 | |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
63 | int net_create(Server *server, char* port); |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
64 | int net_find(Server *server, char* host, char* port); |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
65 | |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
66 | int net_listen(Server *server); |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
67 | int net_destroy(Server *server); |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
68 | int net_connect(Server *server); |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
69 | |
3
3693fd2eb0e9
changed UI to ncurses session + added network handshake
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
70 | void net_send_code(int socket, uint32_t code); |
3693fd2eb0e9
changed UI to ncurses session + added network handshake
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
71 | void net_send_data(int socket, void *data, size_t len); |
7
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
72 | uint32_t net_recieve_code(int socket); |
2
0a08f79c320d
fixed network code + added game info and transmission of game info
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
73 | 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
|
74 | |
1
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
75 | |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
76 | #ifdef __cplusplus |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
77 | } |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
78 | #endif |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
79 | |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
80 | #endif /* TCHESS_NETWORK_H */ |
e5fbb8f9edbe
added (single) client / server architecture
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
81 |