1 | #pragma once
|
---|
2 |
|
---|
3 | #include <stdlib.h>
|
---|
4 | #include <stdio.h>
|
---|
5 | #include <time.h>
|
---|
6 | //#define DDrStartupMessage printf
|
---|
7 |
|
---|
8 | #include <string.h>
|
---|
9 | #include <stdbool.h>
|
---|
10 | #include <stdint.h>
|
---|
11 |
|
---|
12 | #define thread __thread
|
---|
13 |
|
---|
14 | #ifdef WIN32
|
---|
15 | #include <winsock2.h>
|
---|
16 | #include "Flatline_Win32.h"
|
---|
17 | #else
|
---|
18 | #include <sys/ioctl.h>
|
---|
19 | #include <sys/types.h>
|
---|
20 | #include <sys/socket.h>
|
---|
21 | #include <unistd.h>
|
---|
22 | #include <stropts.h>
|
---|
23 | #include <arpa/inet.h>
|
---|
24 | #include <netinet/in.h>
|
---|
25 |
|
---|
26 | #define NetPlatform_Initalize() /* */
|
---|
27 | #define NetPlatform_Shutdown() /* */
|
---|
28 | #define closesocket close
|
---|
29 | #define ioctlsocket ioctl
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | #include "Daodan.h"
|
---|
33 | #include "BFW_Utility.h"
|
---|
34 | #include "Daodan_Console.h"
|
---|
35 | #include "Oni_Character.h"
|
---|
36 |
|
---|
37 |
|
---|
38 | #define pad1_size (sizeof(int64_t) - sizeof(short))
|
---|
39 | #define pad2_size (128 - (sizeof(short) + pad1_size + sizeof(int64_t)))
|
---|
40 |
|
---|
41 | typedef struct {
|
---|
42 | short ss_family;
|
---|
43 | char pad1[pad1_size];
|
---|
44 | uint64_t pad64;
|
---|
45 | char pad2[pad2_size];
|
---|
46 | } sockaddr_storage;
|
---|
47 |
|
---|
48 | typedef struct sockaddr sockaddr;
|
---|
49 | typedef struct sockaddr_in sockaddr_in;
|
---|
50 | typedef sockaddr_storage sockaddr_in6;
|
---|
51 |
|
---|
52 | bool NetUDPServer_Listen(uint16_t port, bool (*packet_callback)(char* data, int datalen, int from));
|
---|
53 | bool NetUDPServer_Send(sockaddr* address, char* data, int datalen);
|
---|
54 |
|
---|
55 | int NetUDPSocket_Create(uint16_t port);
|
---|
56 | int NetUDPSocket_Send(int socket, const sockaddr* address, const char* data, int datalen);
|
---|
57 | void NetUDPSocket_Close(int sock);
|
---|
58 | bool NetUDPSocket_Recieve(int socket, sockaddr_storage* address, char* data, uint16_t* datalen);
|
---|
59 |
|
---|
60 | DWORD WINAPI StartServer(void* lol);
|
---|
61 | DWORD WINAPI StartClient(void* lol);
|
---|
62 |
|
---|
63 | //oh snap, I just realized this is rossy's version of flatline_packet. :|
|
---|
64 | typedef struct {
|
---|
65 | char signature[8];
|
---|
66 | uint16_t protocol_version;
|
---|
67 | char data[0]; //data[0] doesn't work well with simple casts, btw. If you allocate enough space for a handshake_packet
|
---|
68 | } handshake_packet; //there won't be enough room for data. You would have to manually manage the memory (ew)
|
---|
69 |
|
---|
70 | //initial connection
|
---|
71 | typedef struct {
|
---|
72 | char country[2];
|
---|
73 | char name[256];
|
---|
74 | } connect_send; //signature="CONNECT\0"
|
---|
75 |
|
---|
76 | //reply to connection.
|
---|
77 | //goodtogo is if it is going to let you in
|
---|
78 | //message is optional, only used for denial message
|
---|
79 | typedef struct {
|
---|
80 | bool goodtogo;
|
---|
81 | int player_slot;
|
---|
82 | char message[256];
|
---|
83 | } connect_reply;
|
---|
84 |
|
---|
85 | //um, status of the server? :/
|
---|
86 | typedef struct {
|
---|
87 | char name[256];
|
---|
88 | uint32_t numplayers; //signature="STATUS\0\0"
|
---|
89 | } server_status;
|
---|
90 |
|
---|
91 | typedef struct {
|
---|
92 | uint16_t Playernumber;
|
---|
93 | CharacterObject Character;
|
---|
94 | } new_player;
|
---|
95 | extern int update_rate;
|
---|
96 |
|
---|
97 | typedef struct {
|
---|
98 | float MouseDeltaX;
|
---|
99 | float MouseDeltaY;
|
---|
100 | uint32_t Actions1;
|
---|
101 | uint32_t Actions2;
|
---|
102 | } input_struct;
|
---|
103 |
|
---|
104 | //
|
---|
105 | typedef struct {
|
---|
106 | uint16_t PlayerNum;
|
---|
107 | Vector3 Position;
|
---|
108 | Vector3 LastPosition;
|
---|
109 | Vector3 Location;
|
---|
110 | float Facing;
|
---|
111 | float DesiredFacing;
|
---|
112 | float CosmeticFacing;
|
---|
113 | uint32_t Health;
|
---|
114 | uint32_t MaxHealth;
|
---|
115 | input_struct Inputs;
|
---|
116 | uint16_t Frame;
|
---|
117 | } player_data;
|
---|
118 |
|
---|
119 | //used for storing data about each player
|
---|
120 | typedef struct {
|
---|
121 | char id;
|
---|
122 | int packet_index;
|
---|
123 | char data[1080];
|
---|
124 | } flatline_packet;
|
---|
125 | #define FLATLINE_HEADER sizeof(flatline_packet)-sizeof(char)*1080
|
---|
126 |
|
---|
127 | bool FLrServer_PacketCallback(char* data, int datalen, int from);
|
---|
128 | bool FLrServer_Run();
|
---|
129 | bool FLrClient_Run(flatline_packet* packet);
|
---|
130 | extern int sock;
|
---|
131 |
|
---|
132 | enum {
|
---|
133 | CONNECT_SEND,
|
---|
134 | CONNECT_REPLY,
|
---|
135 | STATUS,
|
---|
136 | MESSAGE,
|
---|
137 | CHANGE_NAME,
|
---|
138 | ECHO,
|
---|
139 | NEW_PLAYER,
|
---|
140 | PLAYER_INPUT,
|
---|
141 | PLAYER_DATA,
|
---|
142 | };
|
---|
143 |
|
---|
144 |
|
---|
145 | typedef struct {
|
---|
146 | int ip;
|
---|
147 | char name[32];
|
---|
148 | char country[2];
|
---|
149 | Character* Chr;
|
---|
150 | uint16_t spawnnumber;
|
---|
151 | uint16_t list_slot;
|
---|
152 | uint32_t Actions1;
|
---|
153 | uint32_t Actions2;
|
---|
154 | float MouseDeltaX;
|
---|
155 | float MouseDeltaY;
|
---|
156 | } player_info;
|
---|
157 |
|
---|
158 | player_info * FLr_FindEmptySlot();
|
---|
159 | uint16_t FLr_FindEmptyListSlot();
|
---|
160 | void * ONICALL FLrInput_Update_Keys(void);
|
---|
161 |
|
---|
162 | void NetCatchError();
|
---|
163 | #define MAX_PLAYERS 128
|
---|
164 | #define CONNECTION_TIMEOUT 15
|
---|
165 | #define MAX_CONNECTIONS 32
|
---|
166 | #define NetTCPSocket_Send NetUDPSocket_Send
|
---|
167 | #define NetTCPServer_Send NetUDPServer_Send
|
---|
168 | extern int client_sock;
|
---|
169 | //these two could probably be combined
|
---|
170 | extern sockaddr_in client_address;
|
---|
171 | extern sockaddr_in address;
|
---|
172 | extern player_info Players[];
|
---|
173 | extern player_info * PlayerList[];
|
---|
174 | int UDPServer_SendToAll(void* packet, int size);
|
---|
175 | extern bool client_connected;
|
---|
176 | extern bool server_started;
|
---|
177 | extern char player_name[];
|
---|