1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 */
20 // net_vcr.c
21
22 #include "quakedef.h"
23 #include "net_vcr.h"
24
25 extern int vcrFile;
26
27 // This is the playback portion of the VCR. It reads the file produced
28 // by the recorder and plays it back to the host. The recording contains
29 // everything necessary (events, timestamps, and data) to duplicate the game
30 // from the viewpoint of everything above the network layer.
31
32 static struct
33 {
34 double time;
35 int op;
36 long session;
37 } next;
38
VCR_Init(void)39 int VCR_Init (void)
40 {
41 net_drivers[0].Init = VCR_Init;
42
43 net_drivers[0].SearchForHosts = VCR_SearchForHosts;
44 net_drivers[0].Connect = VCR_Connect;
45 net_drivers[0].CheckNewConnections = VCR_CheckNewConnections;
46 net_drivers[0].QGetMessage = VCR_GetMessage;
47 net_drivers[0].QSendMessage = VCR_SendMessage;
48 net_drivers[0].CanSendMessage = VCR_CanSendMessage;
49 net_drivers[0].Close = VCR_Close;
50 net_drivers[0].Shutdown = VCR_Shutdown;
51
52 Sys_FileRead(vcrFile, &next, sizeof(next));
53 return 0;
54 }
55
VCR_ReadNext(void)56 void VCR_ReadNext (void)
57 {
58 if (Sys_FileRead(vcrFile, &next, sizeof(next)) == 0)
59 {
60 next.op = 255;
61 Sys_Error ("=== END OF PLAYBACK===\n");
62 }
63 if (next.op < 1 || next.op > VCR_MAX_MESSAGE)
64 Sys_Error ("VCR_ReadNext: bad op");
65 }
66
67
VCR_Listen(qboolean state)68 void VCR_Listen (qboolean state)
69 {
70 }
71
72
VCR_Shutdown(void)73 void VCR_Shutdown (void)
74 {
75 }
76
getDriverDataAsLong(qsocket_t * sock)77 static long getDriverDataAsLong(qsocket_t *sock) {
78 long driverDataAsLong;
79 memcpy(&driverDataAsLong, &sock->driverdata, sizeof(long));
80 return driverDataAsLong;
81 }
82
VCR_GetMessage(qsocket_t * sock)83 int VCR_GetMessage (qsocket_t *sock)
84 {
85 int ret;
86
87 if (host_time != next.time || next.op != VCR_OP_GETMESSAGE || next.session != getDriverDataAsLong(sock))
88 Sys_Error ("VCR missmatch");
89
90 Sys_FileRead(vcrFile, &ret, sizeof(int));
91 if (ret != 1)
92 {
93 VCR_ReadNext ();
94 return ret;
95 }
96
97 Sys_FileRead(vcrFile, &net_message.cursize, sizeof(int));
98 Sys_FileRead(vcrFile, net_message.data, net_message.cursize);
99
100 VCR_ReadNext ();
101
102 return 1;
103 }
104
105
VCR_SendMessage(qsocket_t * sock,sizebuf_t * data)106 int VCR_SendMessage (qsocket_t *sock, sizebuf_t *data)
107 {
108 int ret;
109
110 if (host_time != next.time || next.op != VCR_OP_SENDMESSAGE || next.session != getDriverDataAsLong(sock))
111 Sys_Error ("VCR missmatch");
112
113 Sys_FileRead(vcrFile, &ret, sizeof(int));
114
115 VCR_ReadNext ();
116
117 return ret;
118 }
119
120
VCR_CanSendMessage(qsocket_t * sock)121 qboolean VCR_CanSendMessage (qsocket_t *sock)
122 {
123 qboolean ret;
124
125 if (host_time != next.time || next.op != VCR_OP_CANSENDMESSAGE || next.session != getDriverDataAsLong(sock))
126 Sys_Error ("VCR missmatch");
127
128 Sys_FileRead(vcrFile, &ret, sizeof(int));
129
130 VCR_ReadNext ();
131
132 return ret;
133 }
134
135
VCR_Close(qsocket_t * sock)136 void VCR_Close (qsocket_t *sock)
137 {
138 }
139
140
VCR_SearchForHosts(qboolean xmit)141 void VCR_SearchForHosts (qboolean xmit)
142 {
143 }
144
145
VCR_Connect(const char * host)146 qsocket_t *VCR_Connect (const char *host)
147 {
148 return NULL;
149 }
150
151
VCR_CheckNewConnections(void)152 qsocket_t *VCR_CheckNewConnections (void)
153 {
154 qsocket_t *sock;
155
156 if (host_time != next.time || next.op != VCR_OP_CONNECT)
157 Sys_Error ("VCR missmatch");
158
159 if (!next.session)
160 {
161 VCR_ReadNext ();
162 return NULL;
163 }
164
165 sock = NET_NewQSocket ();
166 memcpy(&sock->driverdata, &next.session, sizeof(long));
167
168 Sys_FileRead (vcrFile, sock->address, NET_NAMELEN);
169 VCR_ReadNext ();
170
171 return sock;
172 }
173