FreeSocket() public static method

NET_FreeQSocket
public static FreeSocket ( qsocket_t sock ) : void
sock qsocket_t
return void
コード例 #1
0
        /// <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);
        }