annotate event_log.c @ 2229:f699f9d500b4

Fix build
author Michael Pavone <pavone@retrodev.com>
date Mon, 05 Sep 2022 23:48:17 -0700
parents a042e046f7f2
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1 #ifdef _WIN32
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
2 #define WINVER 0x501
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
3 #include <winsock2.h>
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
4 #include <ws2tcpip.h>
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
5 #else
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
6 #include <sys/types.h>
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
7 #include <sys/socket.h>
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
8 #include <unistd.h>
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
9 #include <netdb.h>
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
10 #include <netinet/tcp.h>
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
11 #endif
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
12
1986
a042e046f7f2 Fix libretro target
Michael Pavone <pavone@retrodev.com>
parents: 1974
diff changeset
13 #include <stdlib.h>
a042e046f7f2 Fix libretro target
Michael Pavone <pavone@retrodev.com>
parents: 1974
diff changeset
14 #include <string.h>
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
15 #include <errno.h>
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
16 #include "event_log.h"
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
17 #include "util.h"
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
18 #include "blastem.h"
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
19 #include "saves.h"
1958
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
20 #include "zlib/zlib.h"
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
21
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
22 enum {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
23 CMD_GAMEPAD_DOWN,
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
24 CMD_GAMEPAD_UP,
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
25 };
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
26
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
27 static uint8_t active, fully_active;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
28 static FILE *event_file;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
29 static serialize_buffer buffer;
1958
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
30 static uint8_t *compressed;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
31 static size_t compressed_storage;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
32 static z_stream output_stream;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
33 static uint32_t last;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
34
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
35 static void event_log_common_init(void)
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
36 {
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
37 init_serialize(&buffer);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
38 compressed_storage = 128*1024;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
39 compressed = malloc(compressed_storage);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
40 deflateInit(&output_stream, 9);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
41 output_stream.avail_out = compressed_storage;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
42 output_stream.next_out = compressed;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
43 output_stream.avail_in = 0;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
44 output_stream.next_in = buffer.data;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
45 last = 0;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
46 active = 1;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
47 }
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
48
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
49 static uint8_t multi_count;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
50 static size_t multi_start;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
51 static void finish_multi(void)
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
52 {
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
53 buffer.data[multi_start] |= multi_count - 2;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
54 multi_count = 0;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
55 }
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
56
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
57 static void file_finish(void)
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
58 {
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
59 fwrite(compressed, 1, output_stream.next_out - compressed, event_file);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
60 output_stream.next_out = compressed;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
61 output_stream.avail_out = compressed_storage;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
62 int result = deflate(&output_stream, Z_FINISH);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
63 if (Z_STREAM_END != result) {
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
64 fatal_error("Final deflate call returned %d\n", result);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
65 }
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
66 fwrite(compressed, 1, output_stream.next_out - compressed, event_file);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
67 fclose(event_file);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
68 }
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
69
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
70 static const char el_ident[] = "BLSTEL\x02\x00";
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
71 void event_log_file(char *fname)
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
72 {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
73 event_file = fopen(fname, "wb");
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
74 if (!event_file) {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
75 warning("Failed to open event file %s for writing\n", fname);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
76 return;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
77 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
78 fwrite(el_ident, 1, sizeof(el_ident) - 1, event_file);
1958
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
79 event_log_common_init();
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
80 fully_active = 1;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
81 atexit(file_finish);
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
82 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
83
1974
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
84 typedef struct {
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
85 uint8_t *send_progress;
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
86 int sock;
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
87 uint8_t players[1]; //TODO: Expand when support for multiple players per remote is added
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
88 uint8_t num_players;
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
89 } remote;
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
90
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
91 static int listen_sock;
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
92 static remote remotes[7];
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
93 static int num_remotes;
1974
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
94 static uint8_t available_players[7] = {2,3,4,5,6,7,8};
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
95 static int num_available_players = 7;
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
96 void event_log_tcp(char *address, char *port)
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
97 {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
98 struct addrinfo request, *result;
1949
5a76a7373823 Get WIP net play code compiling on Windows and cleanup some unistd.h includes
Michael Pavone <pavone@retrodev.com>
parents: 1948
diff changeset
99 socket_init();
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
100 memset(&request, 0, sizeof(request));
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
101 request.ai_family = AF_INET;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
102 request.ai_socktype = SOCK_STREAM;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
103 request.ai_flags = AI_PASSIVE;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
104 getaddrinfo(address, port, &request, &result);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
105
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
106 listen_sock = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
107 if (listen_sock < 0) {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
108 warning("Failed to open event log listen socket on %s:%s\n", address, port);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
109 goto cleanup_address;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
110 }
1949
5a76a7373823 Get WIP net play code compiling on Windows and cleanup some unistd.h includes
Michael Pavone <pavone@retrodev.com>
parents: 1948
diff changeset
111 int param = 1;
5a76a7373823 Get WIP net play code compiling on Windows and cleanup some unistd.h includes
Michael Pavone <pavone@retrodev.com>
parents: 1948
diff changeset
112 setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, (const char *)&param, sizeof(param));
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
113 if (bind(listen_sock, result->ai_addr, result->ai_addrlen) < 0) {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
114 warning("Failed to bind event log listen socket on %s:%s\n", address, port);
1949
5a76a7373823 Get WIP net play code compiling on Windows and cleanup some unistd.h includes
Michael Pavone <pavone@retrodev.com>
parents: 1948
diff changeset
115 socket_close(listen_sock);
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
116 goto cleanup_address;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
117 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
118 if (listen(listen_sock, 3) < 0) {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
119 warning("Failed to listen for event log remotes on %s:%s\n", address, port);
1949
5a76a7373823 Get WIP net play code compiling on Windows and cleanup some unistd.h includes
Michael Pavone <pavone@retrodev.com>
parents: 1948
diff changeset
120 socket_close(listen_sock);
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
121 goto cleanup_address;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
122 }
1949
5a76a7373823 Get WIP net play code compiling on Windows and cleanup some unistd.h includes
Michael Pavone <pavone@retrodev.com>
parents: 1948
diff changeset
123 socket_blocking(listen_sock, 0);
1958
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
124 event_log_common_init();
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
125 cleanup_address:
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
126 freeaddrinfo(result);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
127 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
128
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
129 static uint8_t *system_start;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
130 static size_t system_start_size;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
131 void event_system_start(system_type stype, vid_std video_std, char *name)
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
132 {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
133 if (!active) {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
134 return;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
135 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
136 save_int8(&buffer, stype);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
137 save_int8(&buffer, video_std);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
138 size_t name_len = strlen(name);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
139 if (name_len > 255) {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
140 name_len = 255;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
141 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
142 save_int8(&buffer, name_len);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
143 save_buffer8(&buffer, name, strlen(name));
1958
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
144 if (listen_sock) {
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
145 system_start = malloc(buffer.size);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
146 system_start_size = buffer.size;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
147 memcpy(system_start, buffer.data, buffer.size);
1958
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
148 } else {
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
149 //system start header is never compressed, so write to file immediately
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
150 fwrite(buffer.data, 1, buffer.size, event_file);
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
151 }
1958
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
152 buffer.size = 0;
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
153 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
154
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
155 //header formats
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
156 //Single byte: 4 bit type, 4 bit delta (16-31)
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
157 //Three Byte: 8 bit type, 16-bit delta
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
158 //Four byte: 8-bit type, 24-bit signed delta
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
159 #define FORMAT_3BYTE 0xE0
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
160 #define FORMAT_4BYTE 0xF0
1956
275f1c4bdb25 Netplay protocol size optimization
Michael Pavone <pavone@retrodev.com>
parents: 1955
diff changeset
161 static uint8_t last_event_type = 0xFF;
275f1c4bdb25 Netplay protocol size optimization
Michael Pavone <pavone@retrodev.com>
parents: 1955
diff changeset
162 static uint32_t last_delta;
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
163 static void event_header(uint8_t type, uint32_t cycle)
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
164 {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
165 uint32_t delta = cycle - last;
1956
275f1c4bdb25 Netplay protocol size optimization
Michael Pavone <pavone@retrodev.com>
parents: 1955
diff changeset
166 if (multi_count) {
275f1c4bdb25 Netplay protocol size optimization
Michael Pavone <pavone@retrodev.com>
parents: 1955
diff changeset
167 if (type != last_event_type || delta != last_delta) {
1958
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
168 finish_multi();
1956
275f1c4bdb25 Netplay protocol size optimization
Michael Pavone <pavone@retrodev.com>
parents: 1955
diff changeset
169 } else {
275f1c4bdb25 Netplay protocol size optimization
Michael Pavone <pavone@retrodev.com>
parents: 1955
diff changeset
170 ++multi_count;
275f1c4bdb25 Netplay protocol size optimization
Michael Pavone <pavone@retrodev.com>
parents: 1955
diff changeset
171 if (multi_count == 17) {
1958
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
172 finish_multi();
1956
275f1c4bdb25 Netplay protocol size optimization
Michael Pavone <pavone@retrodev.com>
parents: 1955
diff changeset
173 last_event_type = 0xFF;
275f1c4bdb25 Netplay protocol size optimization
Michael Pavone <pavone@retrodev.com>
parents: 1955
diff changeset
174 }
275f1c4bdb25 Netplay protocol size optimization
Michael Pavone <pavone@retrodev.com>
parents: 1955
diff changeset
175 return;
275f1c4bdb25 Netplay protocol size optimization
Michael Pavone <pavone@retrodev.com>
parents: 1955
diff changeset
176 }
1957
ba06346611a1 Fix some netplay issues
Mike Pavone <pavone@retrodev.com>
parents: 1956
diff changeset
177 } else if (type == last_event_type && delta == last_delta && type != EVENT_FLUSH) {
1956
275f1c4bdb25 Netplay protocol size optimization
Michael Pavone <pavone@retrodev.com>
parents: 1955
diff changeset
178 //make some room
275f1c4bdb25 Netplay protocol size optimization
Michael Pavone <pavone@retrodev.com>
parents: 1955
diff changeset
179 save_int8(&buffer, 0);
275f1c4bdb25 Netplay protocol size optimization
Michael Pavone <pavone@retrodev.com>
parents: 1955
diff changeset
180 //shift existing command
275f1c4bdb25 Netplay protocol size optimization
Michael Pavone <pavone@retrodev.com>
parents: 1955
diff changeset
181 memmove(buffer.data + multi_start + 1, buffer.data + multi_start, buffer.size - multi_start - 1);
275f1c4bdb25 Netplay protocol size optimization
Michael Pavone <pavone@retrodev.com>
parents: 1955
diff changeset
182 buffer.data[multi_start] = EVENT_MULTI << 4;
275f1c4bdb25 Netplay protocol size optimization
Michael Pavone <pavone@retrodev.com>
parents: 1955
diff changeset
183 multi_count = 2;
275f1c4bdb25 Netplay protocol size optimization
Michael Pavone <pavone@retrodev.com>
parents: 1955
diff changeset
184 return;
275f1c4bdb25 Netplay protocol size optimization
Michael Pavone <pavone@retrodev.com>
parents: 1955
diff changeset
185 }
275f1c4bdb25 Netplay protocol size optimization
Michael Pavone <pavone@retrodev.com>
parents: 1955
diff changeset
186 multi_start = buffer.size;
275f1c4bdb25 Netplay protocol size optimization
Michael Pavone <pavone@retrodev.com>
parents: 1955
diff changeset
187 last_event_type = type;
275f1c4bdb25 Netplay protocol size optimization
Michael Pavone <pavone@retrodev.com>
parents: 1955
diff changeset
188 last_delta = delta;
275f1c4bdb25 Netplay protocol size optimization
Michael Pavone <pavone@retrodev.com>
parents: 1955
diff changeset
189
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
190 if (delta > 65535) {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
191 save_int8(&buffer, FORMAT_4BYTE | type);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
192 save_int8(&buffer, delta >> 16);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
193 save_int16(&buffer, delta);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
194 } else if (delta >= 16 && delta < 32) {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
195 save_int8(&buffer, type << 4 | (delta - 16));
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
196 } else {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
197 save_int8(&buffer, FORMAT_3BYTE | type);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
198 save_int16(&buffer, delta);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
199 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
200 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
201
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
202 void event_cycle_adjust(uint32_t cycle, uint32_t deduction)
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
203 {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
204 if (!fully_active) {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
205 return;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
206 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
207 event_header(EVENT_ADJUST, cycle);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
208 last = cycle - deduction;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
209 save_int32(&buffer, deduction);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
210 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
211
1974
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
212 static uint8_t next_available_player(void)
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
213 {
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
214 uint8_t lowest = 0xFF;
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
215 int lowest_index = -1;
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
216 for (int i = 0; i < num_available_players; i++)
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
217 {
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
218 if (available_players[i] < lowest) {
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
219 lowest = available_players[i];
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
220 lowest_index = i;
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
221 }
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
222 }
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
223 if (lowest_index >= 0) {
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
224 available_players[lowest_index] = available_players[num_available_players - 1];
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
225 --num_available_players;
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
226 }
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
227 return lowest;
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
228 }
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
229
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
230 static void flush_socket(void)
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
231 {
1974
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
232 int remote_sock = accept(listen_sock, NULL, NULL);
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
233 if (remote_sock != -1) {
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
234 if (num_remotes == 7) {
1974
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
235 socket_close(remote_sock);
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
236 } else {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
237 printf("remote %d connected\n", num_remotes);
1974
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
238 uint8_t player = next_available_player();
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
239 remotes[num_remotes++] = (remote){
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
240 .sock = remote_sock,
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
241 .send_progress = NULL,
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
242 .players = {player},
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
243 .num_players = player == 0xFF ? 0 : 1
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
244 };
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
245 current_system->save_state = EVENTLOG_SLOT + 1;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
246 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
247 }
1958
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
248 uint8_t *min_progress = compressed;
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
249 for (int i = 0; i < num_remotes; i++) {
1974
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
250 if (remotes[i].send_progress) {
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
251 uint8_t recv_buffer[1500];
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
252 int bytes = recv(remotes[i].sock, recv_buffer, sizeof(recv_buffer), 0);
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
253 for (int j = 0; j < bytes; j++)
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
254 {
1974
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
255 uint8_t cmd = recv_buffer[j];
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
256 switch(cmd)
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
257 {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
258 case CMD_GAMEPAD_DOWN:
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
259 case CMD_GAMEPAD_UP: {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
260 ++j;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
261 if (j < bytes) {
1974
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
262 uint8_t button = recv_buffer[j];
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
263 uint8_t pad = (button >> 5) - 1;
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
264 button &= 0x1F;
1974
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
265 if (pad < remotes[i].num_players) {
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
266 pad = remotes[i].players[pad];
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
267 if (cmd == CMD_GAMEPAD_DOWN) {
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
268 current_system->gamepad_down(current_system, pad, button);
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
269 } else {
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
270 current_system->gamepad_up(current_system, pad, button);
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
271 }
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
272 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
273 } else {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
274 warning("Received incomplete command %X\n", cmd);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
275 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
276 break;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
277 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
278 default:
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
279 warning("Unrecognized remote command %X\n", cmd);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
280 j = bytes;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
281 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
282 }
1974
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
283 int sent = 1;
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
284 while (sent && output_stream.next_out > remotes[i].send_progress)
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
285 {
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
286 sent = send(remotes[i].sock, remotes[i].send_progress, output_stream.next_out - remotes[i].send_progress, 0);
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
287 if (sent >= 0) {
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
288 remotes[i].send_progress += sent;
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
289 } else if (!socket_error_is_wouldblock()) {
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
290 socket_close(remotes[i].sock);
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
291 for (int j = 0; j < remotes[i].num_players; j++) {
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
292 available_players[num_available_players++] = remotes[i].players[j];
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
293 }
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
294 remotes[i] = remotes[num_remotes-1];
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
295 num_remotes--;
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
296 if (!num_remotes) {
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
297 //last remote disconnected, reset buffers/deflate
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
298 fully_active = 0;
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
299 deflateReset(&output_stream);
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
300 output_stream.next_out = compressed;
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
301 output_stream.avail_out = compressed_storage;
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
302 buffer.size = 0;
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
303 }
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
304 i--;
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
305 break;
1973
cd163b230cf9 Fix handling of remote disconnects
Michael Pavone <pavone@retrodev.com>
parents: 1972
diff changeset
306 }
1974
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
307 if (remotes[i].send_progress > min_progress) {
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
308 min_progress = remotes[i].send_progress;
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
309 }
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
310 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
311 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
312 }
1958
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
313 if (min_progress == output_stream.next_out) {
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
314 output_stream.next_out = compressed;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
315 output_stream.avail_out = compressed_storage;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
316 for (int i = 0; i < num_remotes; i++) {
1974
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
317 if (remotes[i].send_progress) {
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
318 remotes[i].send_progress = compressed;
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
319 }
1958
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
320 }
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
321 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
322 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
323
1971
80920c21bb52 Add an event log soft flush and call it twice per frame in between hard flushes to netplay latency when there are insufficient hardware updates to flush packets in the middle of a frame
Michael Pavone <pavone@retrodev.com>
parents: 1967
diff changeset
324 uint8_t wrote_since_last_flush;
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
325 void event_log(uint8_t type, uint32_t cycle, uint8_t size, uint8_t *payload)
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
326 {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
327 if (!fully_active) {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
328 return;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
329 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
330 event_header(type, cycle);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
331 last = cycle;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
332 save_buffer8(&buffer, payload, size);
1958
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
333 if (!multi_count) {
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
334 last_event_type = 0xFF;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
335 output_stream.avail_in = buffer.size - (output_stream.next_in - buffer.data);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
336 int result = deflate(&output_stream, Z_NO_FLUSH);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
337 if (result != Z_OK) {
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
338 fatal_error("deflate returned %d\n", result);
1957
ba06346611a1 Fix some netplay issues
Mike Pavone <pavone@retrodev.com>
parents: 1956
diff changeset
339 }
1958
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
340 if (listen_sock) {
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
341 if ((output_stream.next_out - compressed) > 1280 || !output_stream.avail_out) {
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
342 flush_socket();
1971
80920c21bb52 Add an event log soft flush and call it twice per frame in between hard flushes to netplay latency when there are insufficient hardware updates to flush packets in the middle of a frame
Michael Pavone <pavone@retrodev.com>
parents: 1967
diff changeset
343 wrote_since_last_flush = 1;
1958
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
344 }
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
345 } else if (!output_stream.avail_out) {
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
346 fwrite(compressed, 1, compressed_storage, event_file);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
347 output_stream.next_out = compressed;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
348 output_stream.avail_out = compressed_storage;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
349 }
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
350 if (!output_stream.avail_in) {
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
351 buffer.size = 0;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
352 output_stream.next_in = buffer.data;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
353 }
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
354 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
355 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
356
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
357 static uint32_t last_word_address;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
358 void event_vram_word(uint32_t cycle, uint32_t address, uint16_t value)
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
359 {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
360 uint32_t delta = address - last_word_address;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
361 if (delta < 256) {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
362 uint8_t buffer[3] = {delta, value >> 8, value};
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
363 event_log(EVENT_VRAM_WORD_DELTA, cycle, sizeof(buffer), buffer);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
364 } else {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
365 uint8_t buffer[5] = {address >> 16, address >> 8, address, value >> 8, value};
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
366 event_log(EVENT_VRAM_WORD, cycle, sizeof(buffer), buffer);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
367 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
368 last_word_address = address;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
369 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
370
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
371 static uint32_t last_byte_address;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
372 void event_vram_byte(uint32_t cycle, uint16_t address, uint8_t byte, uint8_t auto_inc)
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
373 {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
374 uint32_t delta = address - last_byte_address;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
375 if (delta == 1) {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
376 event_log(EVENT_VRAM_BYTE_ONE, cycle, sizeof(byte), &byte);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
377 } else if (delta == auto_inc) {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
378 event_log(EVENT_VRAM_BYTE_AUTO, cycle, sizeof(byte), &byte);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
379 } else if (delta < 256) {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
380 uint8_t buffer[2] = {delta, byte};
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
381 event_log(EVENT_VRAM_BYTE_DELTA, cycle, sizeof(buffer), buffer);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
382 } else {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
383 uint8_t buffer[3] = {address >> 8, address, byte};
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
384 event_log(EVENT_VRAM_BYTE, cycle, sizeof(buffer), buffer);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
385 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
386 last_byte_address = address;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
387 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
388
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
389 static size_t send_all(int sock, uint8_t *data, size_t size, int flags)
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
390 {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
391 size_t total = 0, sent = 1;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
392 while(sent > 0 && total < size)
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
393 {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
394 sent = send(sock, data + total, size - total, flags);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
395 if (sent > 0) {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
396 total += sent;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
397 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
398 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
399 return total;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
400 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
401
1958
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
402 void deflate_flush(uint8_t full)
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
403 {
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
404 output_stream.avail_in = buffer.size - (output_stream.next_in - buffer.data);
1972
f2d37131840e Fix "full" deflate flush so multiple remotes can successfully join
Michael Pavone <pavone@retrodev.com>
parents: 1971
diff changeset
405 uint8_t force = full;
f2d37131840e Fix "full" deflate flush so multiple remotes can successfully join
Michael Pavone <pavone@retrodev.com>
parents: 1971
diff changeset
406 while (output_stream.avail_in || force)
1958
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
407 {
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
408 if (!output_stream.avail_out) {
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
409 size_t old_storage = compressed_storage;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
410 uint8_t *old_compressed = compressed;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
411 compressed_storage *= 2;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
412 compressed = realloc(compressed, compressed_storage);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
413 output_stream.next_out = compressed + old_storage;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
414 output_stream.avail_out = old_storage;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
415 for (int i = 0; i < num_remotes; i++) {
1974
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
416 if (remotes[i].send_progress) {
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
417 remotes[i].send_progress = compressed + (remotes[i].send_progress - old_compressed);
1958
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
418 }
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
419 }
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
420 }
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
421 int result = deflate(&output_stream, full ? Z_FINISH : Z_SYNC_FLUSH);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
422 if (result != (full ? Z_STREAM_END : Z_OK)) {
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
423 fatal_error("deflate returned %d\n", result);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
424 }
1972
f2d37131840e Fix "full" deflate flush so multiple remotes can successfully join
Michael Pavone <pavone@retrodev.com>
parents: 1971
diff changeset
425 if (full && result == Z_STREAM_END) {
1958
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
426 result = deflateReset(&output_stream);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
427 if (result != Z_OK) {
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
428 fatal_error("deflateReset returned %d\n", result);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
429 }
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
430 }
1972
f2d37131840e Fix "full" deflate flush so multiple remotes can successfully join
Michael Pavone <pavone@retrodev.com>
parents: 1971
diff changeset
431 force = 0;
1958
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
432 }
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
433 output_stream.next_in = buffer.data;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
434 buffer.size = 0;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
435 }
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
436
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
437 void event_state(uint32_t cycle, serialize_buffer *state)
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
438 {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
439 if (!fully_active) {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
440 last = cycle;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
441 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
442 uint8_t header[] = {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
443 EVENT_STATE << 4, last >> 24, last >> 16, last >> 8, last,
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
444 last_word_address >> 16, last_word_address >> 8, last_word_address,
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
445 last_byte_address >> 8, last_byte_address,
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
446 state->size >> 16, state->size >> 8, state->size
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
447 };
1958
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
448 uint8_t sent_system_start = 0;
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
449 for (int i = 0; i < num_remotes; i++)
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
450 {
1974
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
451 if (!remotes[i].send_progress) {
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
452 if (send_all(remotes[i].sock, system_start, system_start_size, 0) == system_start_size) {
1958
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
453 sent_system_start = 1;
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
454 } else {
1974
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
455 socket_close(remotes[i].sock);
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
456 remotes[i] = remotes[num_remotes-1];
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
457 num_remotes--;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
458 i--;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
459 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
460 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
461 }
1958
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
462 if (sent_system_start) {
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
463 if (fully_active) {
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
464 if (multi_count) {
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
465 finish_multi();
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
466 }
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
467 //full flush is needed so new and old clients can share a stream
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
468 deflate_flush(1);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
469 }
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
470 save_buffer8(&buffer, header, sizeof(header));
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
471 save_buffer8(&buffer, state->data, state->size);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
472 size_t old_compressed_size = output_stream.next_out - compressed;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
473 deflate_flush(1);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
474 size_t state_size = output_stream.next_out - compressed - old_compressed_size;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
475 for (int i = 0; i < num_remotes; i++) {
1974
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
476 if (!remotes[i].send_progress) {
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
477 if (send_all(remotes[i].sock, compressed + old_compressed_size, state_size, 0) == state_size) {
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
478 remotes[i].send_progress = compressed + old_compressed_size;
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
479 socket_blocking(remotes[i].sock, 0);
1958
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
480 int flag = 1;
1974
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
481 setsockopt(remotes[i].sock, IPPROTO_TCP, TCP_NODELAY, (const char *)&flag, sizeof(flag));
1958
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
482 fully_active = 1;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
483 } else {
1974
04b79a725b7f Better handling of pad assignment to remotes
Michael Pavone <pavone@retrodev.com>
parents: 1973
diff changeset
484 socket_close(remotes[i].sock);
1958
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
485 remotes[i] = remotes[num_remotes-1];
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
486 num_remotes--;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
487 i--;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
488 }
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
489 }
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
490 }
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
491 output_stream.next_out = compressed + old_compressed_size;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
492 output_stream.avail_out = compressed_storage - old_compressed_size;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
493 }
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
494 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
495
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
496 void event_flush(uint32_t cycle)
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
497 {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
498 if (!active) {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
499 return;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
500 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
501 if (fully_active) {
1958
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
502 event_header(EVENT_FLUSH, cycle);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
503 last = cycle;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
504
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
505 deflate_flush(0);
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
506 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
507 if (event_file) {
1958
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
508 fwrite(compressed, 1, output_stream.next_out - compressed, event_file);
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
509 fflush(event_file);
1958
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
510 output_stream.next_out = compressed;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
511 output_stream.avail_out = compressed_storage;
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
512 } else if (listen_sock) {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
513 flush_socket();
1971
80920c21bb52 Add an event log soft flush and call it twice per frame in between hard flushes to netplay latency when there are insufficient hardware updates to flush packets in the middle of a frame
Michael Pavone <pavone@retrodev.com>
parents: 1967
diff changeset
514 wrote_since_last_flush = 0;
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
515 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
516 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
517
1971
80920c21bb52 Add an event log soft flush and call it twice per frame in between hard flushes to netplay latency when there are insufficient hardware updates to flush packets in the middle of a frame
Michael Pavone <pavone@retrodev.com>
parents: 1967
diff changeset
518 void event_soft_flush(uint32_t cycle)
80920c21bb52 Add an event log soft flush and call it twice per frame in between hard flushes to netplay latency when there are insufficient hardware updates to flush packets in the middle of a frame
Michael Pavone <pavone@retrodev.com>
parents: 1967
diff changeset
519 {
80920c21bb52 Add an event log soft flush and call it twice per frame in between hard flushes to netplay latency when there are insufficient hardware updates to flush packets in the middle of a frame
Michael Pavone <pavone@retrodev.com>
parents: 1967
diff changeset
520 if (!fully_active || wrote_since_last_flush || event_file) {
80920c21bb52 Add an event log soft flush and call it twice per frame in between hard flushes to netplay latency when there are insufficient hardware updates to flush packets in the middle of a frame
Michael Pavone <pavone@retrodev.com>
parents: 1967
diff changeset
521 return;
80920c21bb52 Add an event log soft flush and call it twice per frame in between hard flushes to netplay latency when there are insufficient hardware updates to flush packets in the middle of a frame
Michael Pavone <pavone@retrodev.com>
parents: 1967
diff changeset
522 }
80920c21bb52 Add an event log soft flush and call it twice per frame in between hard flushes to netplay latency when there are insufficient hardware updates to flush packets in the middle of a frame
Michael Pavone <pavone@retrodev.com>
parents: 1967
diff changeset
523 event_header(EVENT_FLUSH, cycle);
80920c21bb52 Add an event log soft flush and call it twice per frame in between hard flushes to netplay latency when there are insufficient hardware updates to flush packets in the middle of a frame
Michael Pavone <pavone@retrodev.com>
parents: 1967
diff changeset
524 last = cycle;
80920c21bb52 Add an event log soft flush and call it twice per frame in between hard flushes to netplay latency when there are insufficient hardware updates to flush packets in the middle of a frame
Michael Pavone <pavone@retrodev.com>
parents: 1967
diff changeset
525
80920c21bb52 Add an event log soft flush and call it twice per frame in between hard flushes to netplay latency when there are insufficient hardware updates to flush packets in the middle of a frame
Michael Pavone <pavone@retrodev.com>
parents: 1967
diff changeset
526 deflate_flush(0);
80920c21bb52 Add an event log soft flush and call it twice per frame in between hard flushes to netplay latency when there are insufficient hardware updates to flush packets in the middle of a frame
Michael Pavone <pavone@retrodev.com>
parents: 1967
diff changeset
527 flush_socket();
80920c21bb52 Add an event log soft flush and call it twice per frame in between hard flushes to netplay latency when there are insufficient hardware updates to flush packets in the middle of a frame
Michael Pavone <pavone@retrodev.com>
parents: 1967
diff changeset
528 }
80920c21bb52 Add an event log soft flush and call it twice per frame in between hard flushes to netplay latency when there are insufficient hardware updates to flush packets in the middle of a frame
Michael Pavone <pavone@retrodev.com>
parents: 1967
diff changeset
529
1958
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
530 static void init_event_reader_common(event_reader *reader)
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
531 {
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
532 reader->last_cycle = 0;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
533 reader->repeat_event = 0xFF;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
534 reader->storage = 512 * 1024;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
535 init_deserialize(&reader->buffer, malloc(reader->storage), reader->storage);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
536 reader->buffer.size = 0;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
537 memset(&reader->input_stream, 0, sizeof(reader->input_stream));
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
538
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
539 }
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
540
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
541 void init_event_reader(event_reader *reader, uint8_t *data, size_t size)
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
542 {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
543 reader->socket = 0;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
544 reader->last_cycle = 0;
1956
275f1c4bdb25 Netplay protocol size optimization
Michael Pavone <pavone@retrodev.com>
parents: 1955
diff changeset
545 reader->repeat_event = 0xFF;
1958
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
546 init_event_reader_common(reader);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
547 uint8_t name_len = data[1];
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
548 reader->buffer.size = name_len + 2;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
549 memcpy(reader->buffer.data, data, reader->buffer.size);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
550 reader->input_stream.next_in = data + reader->buffer.size;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
551 reader->input_stream.avail_in = size - reader->buffer.size;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
552
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
553 int result = inflateInit(&reader->input_stream);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
554 if (Z_OK != result) {
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
555 fatal_error("inflateInit returned %d\n", result);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
556 }
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
557 reader->input_stream.next_out = reader->buffer.data + reader->buffer.size;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
558 reader->input_stream.avail_out = reader->storage - reader->buffer.size;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
559 result = inflate(&reader->input_stream, Z_NO_FLUSH);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
560 if (Z_OK != result && Z_STREAM_END != result) {
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
561 fatal_error("inflate returned %d\n", result);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
562 }
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
563 reader->buffer.size = reader->input_stream.next_out - reader->buffer.data;
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
564 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
565
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
566 void init_event_reader_tcp(event_reader *reader, char *address, char *port)
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
567 {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
568 struct addrinfo request, *result;
1949
5a76a7373823 Get WIP net play code compiling on Windows and cleanup some unistd.h includes
Michael Pavone <pavone@retrodev.com>
parents: 1948
diff changeset
569 socket_init();
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
570 memset(&request, 0, sizeof(request));
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
571 request.ai_family = AF_INET;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
572 request.ai_socktype = SOCK_STREAM;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
573 request.ai_flags = AI_PASSIVE;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
574 getaddrinfo(address, port, &request, &result);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
575
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
576 reader->socket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
577 if (reader->socket < 0) {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
578 fatal_error("Failed to create socket for event log connection to %s:%s\n", address, port);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
579 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
580 if (connect(reader->socket, result->ai_addr, result->ai_addrlen) < 0) {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
581 fatal_error("Failed to connect to %s:%s for event log stream\n", address, port);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
582 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
583
1958
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
584 init_event_reader_common(reader);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
585 reader->socket_buffer_size = 256 * 1024;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
586 reader->socket_buffer = malloc(reader->socket_buffer_size);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
587
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
588 while(reader->buffer.size < 3 || reader->buffer.size < 3 + reader->buffer.data[2])
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
589 {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
590 int bytes = recv(reader->socket, reader->buffer.data + reader->buffer.size, reader->storage - reader->buffer.size, 0);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
591 if (bytes < 0) {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
592 fatal_error("Failed to receive system init from %s:%s\n", address, port);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
593 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
594 reader->buffer.size += bytes;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
595 }
1958
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
596 size_t init_msg_len = 3 + reader->buffer.data[2];
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
597 memcpy(reader->socket_buffer, reader->buffer.data + init_msg_len, reader->buffer.size - init_msg_len);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
598 reader->input_stream.next_in = reader->socket_buffer;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
599 reader->input_stream.avail_in = reader->buffer.size - init_msg_len;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
600 reader->buffer.size = init_msg_len;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
601 int res = inflateInit(&reader->input_stream);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
602 if (Z_OK != res) {
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
603 fatal_error("inflateInit returned %d\n", res);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
604 }
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
605 reader->input_stream.next_out = reader->buffer.data + init_msg_len;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
606 reader->input_stream.avail_out = reader->storage - init_msg_len;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
607 res = inflate(&reader->input_stream, Z_NO_FLUSH);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
608 if (Z_OK != res && Z_BUF_ERROR != res) {
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
609 fatal_error("inflate returned %d in init_event_reader_tcp\n", res);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
610 }
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
611 int flag = 1;
1949
5a76a7373823 Get WIP net play code compiling on Windows and cleanup some unistd.h includes
Michael Pavone <pavone@retrodev.com>
parents: 1948
diff changeset
612 setsockopt(reader->socket, IPPROTO_TCP, TCP_NODELAY, (const char *)&flag, sizeof(flag));
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
613 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
614
1957
ba06346611a1 Fix some netplay issues
Mike Pavone <pavone@retrodev.com>
parents: 1956
diff changeset
615 static void read_from_socket(event_reader *reader)
ba06346611a1 Fix some netplay issues
Mike Pavone <pavone@retrodev.com>
parents: 1956
diff changeset
616 {
1958
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
617 if (reader->socket_buffer_size - reader->input_stream.avail_in < 128 * 1024) {
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
618 reader->socket_buffer_size *= 2;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
619 uint8_t *new_buf = malloc(reader->socket_buffer_size);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
620 memcpy(new_buf, reader->input_stream.next_in, reader->input_stream.avail_in);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
621 free(reader->socket_buffer);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
622 reader->socket_buffer = new_buf;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
623 reader->input_stream.next_in = new_buf;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
624 } else if (
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
625 reader->input_stream.next_in - reader->socket_buffer >= reader->input_stream.avail_in
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
626 && reader->input_stream.next_in - reader->socket_buffer + reader->input_stream.avail_in >= reader->socket_buffer_size/2
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
627 ) {
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
628 memmove(reader->socket_buffer, reader->input_stream.next_in, reader->input_stream.avail_in);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
629 reader->input_stream.next_in = reader->socket_buffer;
1957
ba06346611a1 Fix some netplay issues
Mike Pavone <pavone@retrodev.com>
parents: 1956
diff changeset
630 }
1958
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
631 uint8_t *space_start = reader->input_stream.next_in + reader->input_stream.avail_in;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
632 size_t space = (reader->socket_buffer + reader->socket_buffer_size) - space_start;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
633 int bytes = recv(reader->socket, space_start, space, 0);
1957
ba06346611a1 Fix some netplay issues
Mike Pavone <pavone@retrodev.com>
parents: 1956
diff changeset
634 if (bytes >= 0) {
1958
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
635 reader->input_stream.avail_in += bytes;
1957
ba06346611a1 Fix some netplay issues
Mike Pavone <pavone@retrodev.com>
parents: 1956
diff changeset
636 } else if (!socket_error_is_wouldblock()) {
ba06346611a1 Fix some netplay issues
Mike Pavone <pavone@retrodev.com>
parents: 1956
diff changeset
637 fatal_error("Connection closed, error = %X\n", socket_last_error());
ba06346611a1 Fix some netplay issues
Mike Pavone <pavone@retrodev.com>
parents: 1956
diff changeset
638 }
ba06346611a1 Fix some netplay issues
Mike Pavone <pavone@retrodev.com>
parents: 1956
diff changeset
639 }
ba06346611a1 Fix some netplay issues
Mike Pavone <pavone@retrodev.com>
parents: 1956
diff changeset
640
1958
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
641 static void inflate_flush(event_reader *reader)
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
642 {
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
643 if (reader->buffer.cur_pos > reader->storage / 2) {
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
644 memmove(reader->buffer.data, reader->buffer.data + reader->buffer.cur_pos, reader->buffer.size - reader->buffer.cur_pos);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
645 reader->buffer.size -= reader->buffer.cur_pos;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
646 reader->buffer.cur_pos = 0;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
647 reader->input_stream.next_out = reader->buffer.data + reader->buffer.size;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
648 reader->input_stream.avail_out = reader->storage - reader->buffer.size;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
649 }
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
650 int result = inflate(&reader->input_stream, Z_SYNC_FLUSH);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
651 if (Z_OK != result && Z_STREAM_END != result) {
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
652 fatal_error("inflate returned %d\n", result);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
653 }
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
654 reader->buffer.size = reader->input_stream.next_out - reader->buffer.data;
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
655 if (result == Z_STREAM_END && (reader->socket || reader->input_stream.avail_in)) {
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
656 inflateReset(&reader->input_stream);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
657 if (reader->input_stream.avail_in) {
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
658 inflate_flush(reader);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
659 }
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
660 }
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
661
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
662 }
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
663
1957
ba06346611a1 Fix some netplay issues
Mike Pavone <pavone@retrodev.com>
parents: 1956
diff changeset
664 void reader_ensure_data(event_reader *reader, size_t bytes)
ba06346611a1 Fix some netplay issues
Mike Pavone <pavone@retrodev.com>
parents: 1956
diff changeset
665 {
1958
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
666 if (reader->buffer.size - reader->buffer.cur_pos < bytes) {
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
667 if (reader->input_stream.avail_in) {
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
668 inflate_flush(reader);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
669 }
1967
bd70f1e15684 Make netplay remote sync to network rather than audio or video so it doesn't drift out of sync with the host
Michael Pavone <pavone@retrodev.com>
parents: 1958
diff changeset
670 if (reader->socket) {
1958
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
671 while (reader->buffer.size - reader->buffer.cur_pos < bytes) {
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
672 read_from_socket(reader);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
673 inflate_flush(reader);
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
674 }
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
675 }
1957
ba06346611a1 Fix some netplay issues
Mike Pavone <pavone@retrodev.com>
parents: 1956
diff changeset
676 }
ba06346611a1 Fix some netplay issues
Mike Pavone <pavone@retrodev.com>
parents: 1956
diff changeset
677 }
ba06346611a1 Fix some netplay issues
Mike Pavone <pavone@retrodev.com>
parents: 1956
diff changeset
678
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
679 uint8_t reader_next_event(event_reader *reader, uint32_t *cycle_out)
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
680 {
1956
275f1c4bdb25 Netplay protocol size optimization
Michael Pavone <pavone@retrodev.com>
parents: 1955
diff changeset
681 if (reader->repeat_remaining) {
275f1c4bdb25 Netplay protocol size optimization
Michael Pavone <pavone@retrodev.com>
parents: 1955
diff changeset
682 reader->repeat_remaining--;
275f1c4bdb25 Netplay protocol size optimization
Michael Pavone <pavone@retrodev.com>
parents: 1955
diff changeset
683 *cycle_out = reader->last_cycle + reader->repeat_delta;
275f1c4bdb25 Netplay protocol size optimization
Michael Pavone <pavone@retrodev.com>
parents: 1955
diff changeset
684 reader->last_cycle = *cycle_out;
275f1c4bdb25 Netplay protocol size optimization
Michael Pavone <pavone@retrodev.com>
parents: 1955
diff changeset
685 return reader->repeat_event;
275f1c4bdb25 Netplay protocol size optimization
Michael Pavone <pavone@retrodev.com>
parents: 1955
diff changeset
686 }
1958
9c01945b5d20 Use zlib to compress event log streams
Mike Pavone <pavone@retrodev.com>
parents: 1957
diff changeset
687 reader_ensure_data(reader, 1);
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
688 uint8_t header = load_int8(&reader->buffer);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
689 uint8_t ret;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
690 uint32_t delta;
1956
275f1c4bdb25 Netplay protocol size optimization
Michael Pavone <pavone@retrodev.com>
parents: 1955
diff changeset
691 uint8_t multi_start = 0;
275f1c4bdb25 Netplay protocol size optimization
Michael Pavone <pavone@retrodev.com>
parents: 1955
diff changeset
692 if ((header & 0xF0) == (EVENT_MULTI << 4)) {
275f1c4bdb25 Netplay protocol size optimization
Michael Pavone <pavone@retrodev.com>
parents: 1955
diff changeset
693 reader->repeat_remaining = (header & 0xF) + 1;
275f1c4bdb25 Netplay protocol size optimization
Michael Pavone <pavone@retrodev.com>
parents: 1955
diff changeset
694 multi_start = 1;
1957
ba06346611a1 Fix some netplay issues
Mike Pavone <pavone@retrodev.com>
parents: 1956
diff changeset
695 reader_ensure_data(reader, 1);
1956
275f1c4bdb25 Netplay protocol size optimization
Michael Pavone <pavone@retrodev.com>
parents: 1955
diff changeset
696 header = load_int8(&reader->buffer);
275f1c4bdb25 Netplay protocol size optimization
Michael Pavone <pavone@retrodev.com>
parents: 1955
diff changeset
697 }
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
698 if ((header & 0xF0) < FORMAT_3BYTE) {
1948
d01527615c7c Fix short event format decode bug
Michael Pavone <pavone@retrodev.com>
parents: 1947
diff changeset
699 delta = (header & 0xF) + 16;
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
700 ret = header >> 4;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
701 } else if ((header & 0xF0) == FORMAT_3BYTE) {
1957
ba06346611a1 Fix some netplay issues
Mike Pavone <pavone@retrodev.com>
parents: 1956
diff changeset
702 reader_ensure_data(reader, 2);
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
703 delta = load_int16(&reader->buffer);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
704 ret = header & 0xF;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
705 } else {
1957
ba06346611a1 Fix some netplay issues
Mike Pavone <pavone@retrodev.com>
parents: 1956
diff changeset
706 reader_ensure_data(reader, 3);
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
707 delta = load_int8(&reader->buffer) << 16;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
708 //sign extend 24-bit delta to 32-bit
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
709 if (delta & 0x800000) {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
710 delta |= 0xFF000000;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
711 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
712 delta |= load_int16(&reader->buffer);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
713 ret = header & 0xF;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
714 }
1956
275f1c4bdb25 Netplay protocol size optimization
Michael Pavone <pavone@retrodev.com>
parents: 1955
diff changeset
715 if (multi_start) {
275f1c4bdb25 Netplay protocol size optimization
Michael Pavone <pavone@retrodev.com>
parents: 1955
diff changeset
716 reader->repeat_event = ret;
275f1c4bdb25 Netplay protocol size optimization
Michael Pavone <pavone@retrodev.com>
parents: 1955
diff changeset
717 reader->repeat_delta = delta;
275f1c4bdb25 Netplay protocol size optimization
Michael Pavone <pavone@retrodev.com>
parents: 1955
diff changeset
718 }
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
719 *cycle_out = reader->last_cycle + delta;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
720 reader->last_cycle = *cycle_out;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
721 if (ret == EVENT_ADJUST) {
1957
ba06346611a1 Fix some netplay issues
Mike Pavone <pavone@retrodev.com>
parents: 1956
diff changeset
722 reader_ensure_data(reader, 4);
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
723 size_t old_pos = reader->buffer.cur_pos;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
724 uint32_t adjust = load_int32(&reader->buffer);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
725 reader->buffer.cur_pos = old_pos;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
726 reader->last_cycle -= adjust;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
727 } else if (ret == EVENT_STATE) {
1957
ba06346611a1 Fix some netplay issues
Mike Pavone <pavone@retrodev.com>
parents: 1956
diff changeset
728 reader_ensure_data(reader, 8);
1947
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
729 reader->last_cycle = load_int32(&reader->buffer);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
730 reader->last_word_address = load_int8(&reader->buffer) << 16;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
731 reader->last_word_address |= load_int16(&reader->buffer);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
732 reader->last_byte_address = load_int16(&reader->buffer);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
733 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
734 return ret;
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
735 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
736
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
737 uint8_t reader_system_type(event_reader *reader)
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
738 {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
739 return load_int8(&reader->buffer);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
740 }
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
741
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
742 void reader_send_gamepad_event(event_reader *reader, uint8_t pad, uint8_t button, uint8_t down)
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
743 {
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
744 uint8_t buffer[] = {down ? CMD_GAMEPAD_DOWN : CMD_GAMEPAD_UP, pad << 5 | button};
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
745 //TODO: Deal with the fact that we're not in blocking mode so this may not actually send all
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
746 //if the buffer is full
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
747 send_all(reader->socket, buffer, sizeof(buffer), 0);
c36102d09351 Add missing netplay files and add in support for sending gamepad commands back to host
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
748 }