using System;
using System.IO;
using System.Text;

namespace SharpQuake
{
	internal class net_vcr : INetDriver
	{
		public string Name
		{
			get
			{
				return "VCR";
			}
		}

		public bool IsInitialized
		{
			get
			{
				return this._IsInitialized;
			}
		}

		public void Init()
		{
			this._Next = sys.ReadStructure<VcrRecord>(host.VcrReader.BaseStream);
			this._IsInitialized = true;
		}

		public void Listen(bool state)
		{
		}

		public void SearchForHosts(bool xmit)
		{
		}

		public qsocket_t Connect(string host)
		{
			return null;
		}

		public qsocket_t CheckNewConnections()
		{
			if (host.Time != this._Next.time || this._Next.op != 1)
			{
				sys.Error("VCR missmatch", Array.Empty<object>());
			}
			if (this._Next.session == 0L)
			{
				this.ReadNext();
				return null;
			}
			qsocket_t qsocket_t = net.NewSocket();
			qsocket_t.driverdata = this._Next.session;
			byte[] array = new byte[64];
			host.VcrReader.Read(array, 0, array.Length);
			qsocket_t.address = Encoding.ASCII.GetString(array);
			this.ReadNext();
			return qsocket_t;
		}

		public int GetMessage(qsocket_t sock)
		{
			if (host.Time != this._Next.time || this._Next.op != 2 || this._Next.session != this.SocketToSession(sock))
			{
				sys.Error("VCR missmatch", Array.Empty<object>());
			}
			int num = host.VcrReader.ReadInt32();
			if (num != 1)
			{
				this.ReadNext();
				return num;
			}
			int count = host.VcrReader.ReadInt32();
			net.Message.FillFrom(host.VcrReader.BaseStream, count);
			this.ReadNext();
			return 1;
		}

		public int SendMessage(qsocket_t sock, MsgWriter data)
		{
			if (host.Time != this._Next.time || this._Next.op != 3 || this._Next.session != this.SocketToSession(sock))
			{
				sys.Error("VCR missmatch", Array.Empty<object>());
			}
			int result = host.VcrReader.ReadInt32();
			this.ReadNext();
			return result;
		}

		public int SendUnreliableMessage(qsocket_t sock, MsgWriter data)
		{
			throw new NotImplementedException();
		}

		public bool CanSendMessage(qsocket_t sock)
		{
			if (host.Time != this._Next.time || this._Next.op != 4 || this._Next.session != this.SocketToSession(sock))
			{
				sys.Error("VCR missmatch", Array.Empty<object>());
			}
			int num = host.VcrReader.ReadInt32();
			this.ReadNext();
			return num != 0;
		}

		public bool CanSendUnreliableMessage(qsocket_t sock)
		{
			return true;
		}

		public void Close(qsocket_t sock)
		{
		}

		public void Shutdown()
		{
		}

		private void ReadNext()
		{
			try
			{
				this._Next = sys.ReadStructure<VcrRecord>(host.VcrReader.BaseStream);
			}
			catch (IOException)
			{
				this._Next = new VcrRecord();
				this._Next.op = 255;
				sys.Error("=== END OF PLAYBACK===\n", Array.Empty<object>());
			}
			if (this._Next.op < 1 || this._Next.op > 4)
			{
				sys.Error("VCR_ReadNext: bad op", Array.Empty<object>());
			}
		}

		public long SocketToSession(qsocket_t sock)
		{
			return (long)sock.driverdata;
		}

		private VcrRecord _Next;

		private bool _IsInitialized;
	}
}
