/// <summary> /// NET_FreeQSocket /// </summary> public static void FreeSocket(QSocket sock) { // remove it from active list if (!_ActiveSockets.Remove(sock)) { Sys.Error("NET_FreeQSocket: not active\n"); } // add it to free list _FreeSockets.Add(sock); sock.disconnected = true; }
public int SendMessage(QSocket sock, MessageWriter 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 bool CanSendMessage(QSocket 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); }
/// <summary> /// NET_CheckNewConnections /// </summary> /// <returns></returns> public static QSocket CheckNewConnections() { SetNetTime(); for (_DriverLevel = 0; _DriverLevel < _Drivers.Length; _DriverLevel++) { if (!_Drivers[_DriverLevel].IsInitialized) { continue; } if (_DriverLevel > 0 && !_IsListening) { continue; } QSocket ret = Net.Driver.CheckNewConnections(); if (ret != null) { if (_IsRecording) { _VcrConnect.time = Host.Time; _VcrConnect.op = VcrOp.VCR_OP_CONNECT; _VcrConnect.session = 1; // (long)ret; // Uze: todo: make it work on 64bit systems byte[] buf = Sys.StructureToBytes(ref _VcrConnect); Host.VcrWriter.Write(buf, 0, buf.Length); buf = Encoding.ASCII.GetBytes(ret.address); int count = Math.Min(buf.Length, NET_NAMELEN); int extra = NET_NAMELEN - count; Host.VcrWriter.Write(buf, 0, count); for (int i = 0; i < extra; i++) { Host.VcrWriter.Write((byte)0); } } return(ret); } } if (_IsRecording) { _VcrConnect.time = Host.Time; _VcrConnect.op = VcrOp.VCR_OP_CONNECT; _VcrConnect.session = 0; byte[] buf = Sys.StructureToBytes(ref _VcrConnect); Host.VcrWriter.Write(buf, 0, buf.Length); } return(null); }
/// <summary> /// Datagram_Connect /// </summary> public QSocket Connect(string host) { QSocket ret = null; for (Net.LanDriverLevel = 0; Net.LanDriverLevel < Net.LanDrivers.Length; Net.LanDriverLevel++) { if (Net.LanDrivers[Net.LanDriverLevel].IsInitialized) { ret = InternalConnect(host); if (ret != null) { break; } } } return(ret); }
/// <summary> /// Datagram_CheckNewConnections /// </summary> public QSocket CheckNewConnections() { QSocket ret = null; for (Net.LanDriverLevel = 0; Net.LanDriverLevel < Net.LanDrivers.Length; Net.LanDriverLevel++) { if (Net.LanDriver.IsInitialized) { ret = InternalCheckNewConnections(); if (ret != null) { break; } } } return(ret); }
public void Close(QSocket sock) { if (sock.driverdata != null) { ((QSocket)sock.driverdata).driverdata = null; } sock.ClearBuffers(); sock.canSend = true; if (sock == _Client) { _Client = null; } else { _Server = null; } }
/// <summary> /// NET_Close /// </summary> public static void Close(QSocket sock) { if (sock == null) { return; } if (sock.disconnected) { return; } SetNetTime(); // call the driver_Close function _Drivers[sock.driver].Close(sock); FreeSocket(sock); }
public void Clear() { active = false; spawned = false; dropasap = false; privileged = false; sendsignon = false; last_message = 0; netconnection = null; cmd.Clear(); wishdir = Vector3.Zero; message.Clear(); edict = null; name = null; colors = 0; Array.Clear(ping_times, 0, ping_times.Length); num_pings = 0; Array.Clear(spawn_parms, 0, spawn_parms.Length); old_frags = 0; }
/// <summary> /// SV_ConnectClient /// Initializes a client_t for a new net connection. This will only be called /// once for a player each game, not once for each level change. /// </summary> static void ConnectClient(int clientnum) { client_t client = svs.clients[clientnum]; Con.DPrint("Client {0} connected\n", client.netconnection.address); int edictnum = clientnum + 1; edict_t ent = EdictNum(edictnum); // set up the client_t QSocket netconnection = client.netconnection; float[] spawn_parms = new float[NUM_SPAWN_PARMS]; if (sv.loadgame) { Array.Copy(client.spawn_parms, spawn_parms, spawn_parms.Length); } client.Clear(); client.netconnection = netconnection; client.name = "unconnected"; client.active = true; client.spawned = false; client.edict = ent; client.message.AllowOverflow = true; // we can catch it client.privileged = false; if (sv.loadgame) { Array.Copy(spawn_parms, client.spawn_parms, spawn_parms.Length); } else { // call the progs to get default spawn parms for the new client Progs.Execute(Progs.GlobalStruct.SetNewParms); AssignGlobalSpawnparams(client); } SendServerInfo(client); }
/// <summary> /// NET_NewQSocket /// Called by drivers when a new communications endpoint is required /// The sequence and buffer fields will be filled in properly /// </summary> public static QSocket NewSocket() { if (_FreeSockets.Count == 0) { return(null); } if (Net.ActiveConnections >= Server.svs.maxclients) { return(null); } // get one from free list int i = _FreeSockets.Count - 1; QSocket sock = _FreeSockets[i]; _FreeSockets.RemoveAt(i); // add it to active list _ActiveSockets.Add(sock); sock.disconnected = false; sock.connecttime = _Time; sock.address = "UNSET ADDRESS"; sock.driver = _DriverLevel; sock.socket = null; sock.driverdata = null; sock.canSend = true; sock.sendNext = false; sock.lastMessageTime = _Time; sock.ackSequence = 0; sock.sendSequence = 0; sock.unreliableSendSequence = 0; sock.sendMessageLength = 0; sock.receiveSequence = 0; sock.unreliableReceiveSequence = 0; sock.receiveMessageLength = 0; return(sock); }
public QSocket Connect(string host) { if (host != "local") { return(null); } _LocalConnectPending = true; if (_Client == null) { _Client = Net.NewSocket(); if (_Client == null) { Con.Print("Loop_Connect: no qsocket available\n"); return(null); } _Client.address = "localhost"; } _Client.ClearBuffers(); _Client.canSend = true; if (_Server == null) { _Server = Net.NewSocket(); if (_Server == null) { Con.Print("Loop_Connect: no qsocket available\n"); return(null); } _Server.address = "LOCAL"; } _Server.ClearBuffers(); _Server.canSend = true; _Client.driverdata = _Server; _Server.driverdata = _Client; return(_Client); }
public int GetMessage(QSocket 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); }
/// <summary> /// SV_CheckForNewClients /// </summary> public static void CheckForNewClients() { // // check for new connections // while (true) { QSocket ret = Net.CheckNewConnections(); if (ret == null) { break; } // // init a new client structure // int i; for (i = 0; i < svs.maxclients; i++) { if (!svs.clients[i].active) { break; } } if (i == svs.maxclients) { Sys.Error("Host_CheckForNewClients: no free clients"); } svs.clients[i].netconnection = ret; ConnectClient(i); Net.ActiveConnections++; } }
public long SocketToSession(QSocket sock) { return((long)sock.driverdata); }
public void Close(QSocket sock) { // nothing to do }
public bool CanSendUnreliableMessage(QSocket sock) { return(true); }
public int SendUnreliableMessage(QSocket sock, MessageWriter data) { throw new NotImplementedException(); }
/// <summary> /// NET_Connect /// called by client to connect to a host. Returns -1 if not able to connect /// </summary> public static QSocket Connect(string host) { int numdrivers = _Drivers.Length;// net_numdrivers; SetNetTime(); if (String.IsNullOrEmpty(host)) { host = null; } if (host != null) { if (Common.SameText(host, "local")) { numdrivers = 1; goto JustDoIt; } if (HostCacheCount > 0) { foreach (Hostcache_t hc in _HostCache) { if (Common.SameText(hc.name, host)) { host = hc.cname; goto JustDoIt; } } } } SlistSilent = (host != null); Slist_f(); while (_SlistInProgress) { Poll(); } if (host == null) { if (HostCacheCount != 1) { return(null); } host = _HostCache[0].cname; Con.Print("Connecting to...\n{0} @ {1}\n\n", _HostCache[0].name, host); } _DriverLevel = 0; foreach (Hostcache_t hc in _HostCache) { if (Common.SameText(host, hc.name)) { host = hc.cname; break; } _DriverLevel++; } JustDoIt: _DriverLevel = 0; foreach (INetDriver drv in _Drivers) { if (!drv.IsInitialized) { continue; } QSocket ret = drv.Connect(host); if (ret != null) { return(ret); } _DriverLevel++; } if (host != null) { Con.Print("\n"); PrintSlistHeader(); PrintSlist(); PrintSlistTrailer(); } return(null); }
/// <summary> /// _Datagram_Connect /// </summary> QSocket InternalConnect(string host) { // see if we can resolve the host name EndPoint sendaddr = Net.LanDriver.GetAddrFromName(host); if (sendaddr == null) { return(null); } Socket newsock = Net.LanDriver.OpenSocket(0); if (newsock == null) { return(null); } QSocket sock = Net.NewSocket(); if (sock == null) { goto ErrorReturn2; } sock.socket = newsock; sock.landriver = Net.LanDriverLevel; // connect to the host if (Net.LanDriver.Connect(newsock, sendaddr) == -1) { goto ErrorReturn; } // send the connection request Con.Print("trying...\n"); Scr.UpdateScreen(); double start_time = Net.Time; int ret = 0; for (int reps = 0; reps < 3; reps++) { Net.Message.Clear(); // save space for the header, filled in later Net.Message.WriteLong(0); Net.Message.WriteByte(CCReq.CCREQ_CONNECT); Net.Message.WriteString("QUAKE"); Net.Message.WriteByte(Net.NET_PROTOCOL_VERSION); Common.WriteInt(Net.Message.Data, 0, Common.BigLong(NetFlags.NETFLAG_CTL | (Net.Message.Length & NetFlags.NETFLAG_LENGTH_MASK))); //*((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK)); Net.LanDriver.Write(newsock, Net.Message.Data, Net.Message.Length, sendaddr); Net.Message.Clear(); EndPoint readaddr = new IPEndPoint(IPAddress.Any, 0); do { ret = Net.Message.FillFrom(newsock, ref readaddr); // if we got something, validate it if (ret > 0) { // is it from the right place? if (sock.LanDriver.AddrCompare(readaddr, sendaddr) != 0) { #if DEBUG Con.Print("wrong reply address\n"); Con.Print("Expected: {0}\n", StrAddr(sendaddr)); Con.Print("Received: {0}\n", StrAddr(readaddr)); Scr.UpdateScreen(); #endif ret = 0; continue; } if (ret < sizeof(int)) { ret = 0; continue; } Net.Reader.Reset(); int control = Common.BigLong(Net.Reader.ReadLong());// BigLong(*((int *)net_message.data)); //MSG_ReadLong(); if (control == -1) { ret = 0; continue; } if ((control & (~NetFlags.NETFLAG_LENGTH_MASK)) != NetFlags.NETFLAG_CTL) { ret = 0; continue; } if ((control & NetFlags.NETFLAG_LENGTH_MASK) != ret) { ret = 0; continue; } } }while ((ret == 0) && (Net.SetNetTime() - start_time) < 2.5); if (ret > 0) { break; } Con.Print("still trying...\n"); Scr.UpdateScreen(); start_time = Net.SetNetTime(); } string reason = String.Empty; if (ret == 0) { reason = "No Response"; Con.Print("{0}\n", reason); Menu.ReturnReason = reason; goto ErrorReturn; } if (ret == -1) { reason = "Network Error"; Con.Print("{0}\n", reason); Menu.ReturnReason = reason; goto ErrorReturn; } ret = Net.Reader.ReadByte(); if (ret == CCRep.CCREP_REJECT) { reason = Net.Reader.ReadString(); Con.Print(reason); Menu.ReturnReason = reason; goto ErrorReturn; } if (ret == CCRep.CCREP_ACCEPT) { IPEndPoint ep = (IPEndPoint)sendaddr; sock.addr = new IPEndPoint(ep.Address, ep.Port); Net.LanDriver.SetSocketPort(sock.addr, Net.Reader.ReadLong()); } else { reason = "Bad Response"; Con.Print("{0}\n", reason); Menu.ReturnReason = reason; goto ErrorReturn; } sock.address = Net.LanDriver.GetNameFromAddr(sendaddr); Con.Print("Connection accepted\n"); sock.lastMessageTime = Net.SetNetTime(); // switch the connection to the specified address if (Net.LanDriver.Connect(newsock, sock.addr) == -1) { reason = "Connect to Game failed"; Con.Print("{0}\n", reason); Menu.ReturnReason = reason; goto ErrorReturn; } Menu.ReturnOnError = false; return(sock); ErrorReturn: Net.FreeSocket(sock); ErrorReturn2: Net.LanDriver.CloseSocket(newsock); if (Menu.ReturnOnError && Menu.ReturnMenu != null) { Menu.ReturnMenu.Show(); Menu.ReturnOnError = false; } return(null); }
/// <summary> /// NET_GetMessage /// returns data in net_message sizebuf /// returns 0 if no data is waiting /// returns 1 if a message was received /// returns 2 if an unreliable message was received /// returns -1 if the connection died /// </summary> public static int GetMessage(QSocket sock) { //int ret; if (sock == null) { return(-1); } if (sock.disconnected) { Con.Print("NET_GetMessage: disconnected socket\n"); return(-1); } SetNetTime(); int ret = _Drivers[sock.driver].GetMessage(sock); // see if this connection has timed out if (ret == 0 && sock.driver != 0) { if (_Time - sock.lastMessageTime > _MessageTimeout.Value) { Close(sock); return(-1); } } if (ret > 0) { if (sock.driver != 0) { sock.lastMessageTime = _Time; if (ret == 1) { _MessagesReceived++; } else if (ret == 2) { _UnreliableMessagesReceived++; } } if (_IsRecording) { _VcrGetMessage.time = Host.Time; _VcrGetMessage.op = VcrOp.VCR_OP_GETMESSAGE; _VcrGetMessage.session = 1;// (long)sock; Uze todo: write somethisng meaningful _VcrGetMessage.ret = ret; byte[] buf = Sys.StructureToBytes(ref _VcrGetMessage); Host.VcrWriter.Write(buf, 0, buf.Length); Host.VcrWriter.Write(Net.Message.Length); Host.VcrWriter.Write(Net.Message.Data, 0, Net.Message.Length); } } else { if (_IsRecording) { _VcrGetMessage.time = Host.Time; _VcrGetMessage.op = VcrOp.VCR_OP_GETMESSAGE; _VcrGetMessage.session = 1; // (long)sock; Uze todo: fix this _VcrGetMessage.ret = ret; byte[] buf = Sys.StructureToBytes(ref _VcrGetMessage); Host.VcrWriter.Write(buf, 0, buf.Length); } } return(ret); }