[484] | 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;
|
---|
[486] | 117 | char Animation[32];
|
---|
| 118 | uint16_t AnimationToState;
|
---|
| 119 | uint16_t AnimationFromState;
|
---|
| 120 | uint16_t AnimationType;
|
---|
| 121 | uint16_t NextAnimationType;
|
---|
| 122 | uint16_t AnimationType2;
|
---|
[484] | 123 | } player_data;
|
---|
| 124 |
|
---|
| 125 | //used for storing data about each player
|
---|
| 126 | typedef struct {
|
---|
| 127 | char id;
|
---|
| 128 | int packet_index;
|
---|
| 129 | char data[1080];
|
---|
| 130 | } flatline_packet;
|
---|
| 131 | #define FLATLINE_HEADER sizeof(flatline_packet)-sizeof(char)*1080
|
---|
| 132 |
|
---|
| 133 | bool FLrServer_PacketCallback(char* data, int datalen, int from);
|
---|
| 134 | bool FLrServer_Run();
|
---|
| 135 | bool FLrClient_Run(flatline_packet* packet);
|
---|
| 136 | extern int sock;
|
---|
| 137 |
|
---|
| 138 | enum {
|
---|
| 139 | CONNECT_SEND,
|
---|
| 140 | CONNECT_REPLY,
|
---|
| 141 | STATUS,
|
---|
| 142 | MESSAGE,
|
---|
| 143 | CHANGE_NAME,
|
---|
| 144 | ECHO,
|
---|
| 145 | NEW_PLAYER,
|
---|
| 146 | PLAYER_INPUT,
|
---|
| 147 | PLAYER_DATA,
|
---|
| 148 | };
|
---|
| 149 |
|
---|
| 150 |
|
---|
| 151 | typedef struct {
|
---|
| 152 | int ip;
|
---|
| 153 | char name[32];
|
---|
| 154 | char country[2];
|
---|
| 155 | Character* Chr;
|
---|
| 156 | uint16_t spawnnumber;
|
---|
| 157 | uint16_t list_slot;
|
---|
| 158 | uint32_t Actions1;
|
---|
| 159 | uint32_t Actions2;
|
---|
| 160 | float MouseDeltaX;
|
---|
| 161 | float MouseDeltaY;
|
---|
| 162 | } player_info;
|
---|
| 163 |
|
---|
| 164 | player_info * FLr_FindEmptySlot();
|
---|
| 165 | uint16_t FLr_FindEmptyListSlot();
|
---|
| 166 | void * ONICALL FLrInput_Update_Keys(void);
|
---|
| 167 |
|
---|
| 168 | void NetCatchError();
|
---|
| 169 | #define MAX_PLAYERS 128
|
---|
| 170 | #define CONNECTION_TIMEOUT 15
|
---|
| 171 | #define MAX_CONNECTIONS 32
|
---|
| 172 | #define NetTCPSocket_Send NetUDPSocket_Send
|
---|
| 173 | #define NetTCPServer_Send NetUDPServer_Send
|
---|
| 174 | extern int client_sock;
|
---|
| 175 | //these two could probably be combined
|
---|
| 176 | extern sockaddr_in client_address;
|
---|
| 177 | extern sockaddr_in address;
|
---|
| 178 | extern player_info Players[];
|
---|
| 179 | extern player_info * PlayerList[];
|
---|
| 180 | int UDPServer_SendToAll(void* packet, int size);
|
---|
| 181 | extern bool client_connected;
|
---|
| 182 | extern bool server_started;
|
---|
| 183 | extern char player_name[];
|
---|