public QSocket CheckNewConnections() { if (Host.Time != _Next.time || _Next.op != VcrOp.VCR_OP_CONNECT) { Sys.Error("VCR missmatch"); } if (_Next.session == 0) { ReadNext(); return(null); } QSocket 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 qsocket_t 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); }
/// <summary> /// _Datagram_Connect /// </summary> qsocket_t 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_t 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); }