forked from Memorix101/SharpQuake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
net_vcr.cs
207 lines (168 loc) · 5.31 KB
/
net_vcr.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/// <copyright>
///
/// Rewritten in C# by Yury Kiselev, 2010.
///
/// Copyright (C) 1996-1997 Id Software, Inc.
///
/// This program is free software; you can redistribute it and/or
/// modify it under the terms of the GNU General Public License
/// as published by the Free Software Foundation; either version 2
/// of the License, or (at your option) any later version.
///
/// This program is distributed in the hope that it will be useful,
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
///
/// See the GNU General Public License for more details.
///
/// You should have received a copy of the GNU General Public License
/// along with this program; if not, write to the Free Software
/// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
/// </copyright>
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace SharpQuake
{
internal static class VcrOp
{
public const int VCR_OP_CONNECT = 1;
public const int VCR_OP_GETMESSAGE = 2;
public const int VCR_OP_SENDMESSAGE = 3;
public const int VCR_OP_CANSENDMESSAGE = 4;
public const int VCR_MAX_MESSAGE = 4;
}
internal class net_vcr : INetDriver
{
private VcrRecord _Next;
private bool _IsInitialized;
#region INetDriver Members
public string Name
{
get
{
return "VCR";
}
}
public bool IsInitialized
{
get
{
return _IsInitialized;
}
}
public void Init()
{
_Next = sys.ReadStructure<VcrRecord>( host.VcrReader.BaseStream );
_IsInitialized = true;
}
public void Listen( bool state )
{
// nothing to do
}
public void SearchForHosts( bool xmit )
{
// nothing to do
}
public qsocket_t Connect( string host )
{
return null;
}
public qsocket_t CheckNewConnections()
{
if( host.Time != _Next.time || _Next.op != VcrOp.VCR_OP_CONNECT )
sys.Error( "VCR missmatch" );
if( _Next.session == 0 )
{
ReadNext();
return null;
}
qsocket_t sock = net.NewSocket();
sock.driverdata = _Next.session;
byte[] buf = new byte[net.NET_NAMELEN];
host.VcrReader.Read( buf, 0, buf.Length );
sock.address = Encoding.ASCII.GetString( buf );
ReadNext();
return sock;
}
public int GetMessage( qsocket_t sock )
{
if( host.Time != _Next.time || _Next.op != VcrOp.VCR_OP_GETMESSAGE || _Next.session != SocketToSession( sock ) )
sys.Error( "VCR missmatch" );
int ret = host.VcrReader.ReadInt32();
if( ret != 1 )
{
ReadNext();
return ret;
}
int length = host.VcrReader.ReadInt32();
net.Message.FillFrom( host.VcrReader.BaseStream, length );
ReadNext();
return 1;
}
public int SendMessage( qsocket_t sock, MsgWriter data )
{
if( host.Time != _Next.time || _Next.op != VcrOp.VCR_OP_SENDMESSAGE || _Next.session != SocketToSession( sock ) )
sys.Error( "VCR missmatch" );
int ret = host.VcrReader.ReadInt32();
ReadNext();
return ret;
}
public int SendUnreliableMessage( qsocket_t sock, MsgWriter data )
{
throw new NotImplementedException();
}
public bool CanSendMessage( qsocket_t sock )
{
if( host.Time != _Next.time || _Next.op != VcrOp.VCR_OP_CANSENDMESSAGE || _Next.session != SocketToSession( sock ) )
sys.Error( "VCR missmatch" );
int ret = host.VcrReader.ReadInt32();
ReadNext();
return ret != 0;
}
public bool CanSendUnreliableMessage( qsocket_t sock )
{
return true;
}
public void Close( qsocket_t sock )
{
// nothing to do
}
public void Shutdown()
{
// nothing to do
}
/// <summary>
/// VCR_ReadNext
/// </summary>
private void ReadNext()
{
try
{
_Next = sys.ReadStructure<VcrRecord>( host.VcrReader.BaseStream );
}
catch( IOException )
{
_Next = new VcrRecord();
_Next.op = 255;
sys.Error( "=== END OF PLAYBACK===\n" );
}
if( _Next.op < 1 || _Next.op > VcrOp.VCR_MAX_MESSAGE )
sys.Error( "VCR_ReadNext: bad op" );
}
#endregion INetDriver Members
public long SocketToSession( qsocket_t sock )
{
return (long)sock.driverdata;
}
}
[StructLayout( LayoutKind.Sequential, Pack = 1 )]
internal class VcrRecord
{
public double time;
public int op;
public long session;
public static int SizeInBytes = Marshal.SizeOf(typeof(VcrRecord));
}
}