/// <summary> /// Con_Printf /// </summary> public static void Print(string fmt, params object[] args) { string msg = (args.Length > 0 ? String.Format(fmt, args) : fmt); // log all messages to file if (_DebugLog) { DebugLog(msg); } if (!_IsInitialized) { return; } if (Client.cls.state == cactive_t.ca_dedicated) { return; // no graphics mode } // write it to the scrollable buffer Print(msg); // update the screen if the console is displayed if (Client.cls.signon != Client.SIGNONS && !Scr.IsDisabledForLoading) { Scr.UpdateScreen(); } }
// _Host_Frame // //Runs all active servers static void InternalFrame(double time) { // keep the random time dependent Sys.Random(); // decide the simulation time if (!FilterTime(time)) { return; // don't run too fast, or packets will flood out } // get new key events Sys.SendKeyEvents(); // allow mice or other external controllers to add commands Input.Commands(); // process console commands Cbuf.Execute(); Net.Poll(); // if running the server locally, make intentions now if (Server.sv.active) { Client.SendCmd(); } //------------------- // // server operations // //------------------- // check for commands typed to the host GetConsoleCommands(); if (Server.sv.active) { ServerFrame(); } //------------------- // // client operations // //------------------- // if running the server remotely, send intentions now after // the incoming messages have been read if (!Server.sv.active) { Client.SendCmd(); } _Time += FrameTime; // fetch results from server if (Client.cls.state == cactive_t.ca_connected) { Client.ReadFromServer(); } // update video if (_Speeds.Value != 0) { _Time1 = Sys.GetFloatTime(); } Scr.UpdateScreen(); if (_Speeds.Value != 0) { _Time2 = Sys.GetFloatTime(); } // update audio if (Client.cls.signon == Client.SIGNONS) { Sound.Update(ref Render.Origin, ref Render.ViewPn, ref Render.ViewRight, ref Render.ViewUp); Client.DecayLights(); } else { Sound.Update(ref Common.ZeroVector, ref Common.ZeroVector, ref Common.ZeroVector, ref Common.ZeroVector); } CDAudio.Update(); if (_Speeds.Value != 0) { int pass1 = (int)((_Time1 - _Time3) * 1000); _Time3 = Sys.GetFloatTime(); int pass2 = (int)((_Time2 - _Time1) * 1000); int pass3 = (int)((_Time3 - _Time2) * 1000); Con.Print("{0,3} tot {1,3} server {2,3} gfx {3,3} snd\n", pass1 + pass2 + pass3, pass1, pass2, pass3); } _FrameCount++; }
/// <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); }
/// <summary> /// Key_Console /// Interactive line editing and console scrollback /// </summary> static void KeyConsole(int key) { if (key == K_ENTER) { string line = new String(_Lines[_EditLine]).TrimEnd('\0', ' '); string cmd = line.Substring(1); Cbuf.AddText(cmd); // skip the > Cbuf.AddText("\n"); Con.Print("{0}\n", line); _EditLine = (_EditLine + 1) & 31; _HistoryLine = _EditLine; _Lines[_EditLine][0] = ']'; Key.LinePos = 1; if (Client.cls.state == cactive_t.ca_disconnected) { Scr.UpdateScreen(); // force an update, because the command } // may take some time return; } if (key == K_TAB) { // command completion string txt = new String(_Lines[_EditLine], 1, MAXCMDLINE - 1).TrimEnd('\0', ' '); string[] cmds = Cmd.Complete(txt); string[] vars = Cvar.CompleteName(txt); string match = null; if (cmds != null) { if (cmds.Length > 1 || vars != null) { Con.Print("\nCommands:\n"); foreach (string s in cmds) { Con.Print(" {0}\n", s); } } else { match = cmds[0]; } } if (vars != null) { if (vars.Length > 1 || cmds != null) { Con.Print("\nVariables:\n"); foreach (string s in vars) { Con.Print(" {0}\n", s); } } else if (match == null) { match = vars[0]; } } if (!String.IsNullOrEmpty(match)) { int len = Math.Min(match.Length, MAXCMDLINE - 3); for (int i = 0; i < len; i++) { _Lines[_EditLine][i + 1] = match[i]; } Key.LinePos = len + 1; _Lines[_EditLine][Key.LinePos] = ' '; Key.LinePos++; _Lines[_EditLine][Key.LinePos] = '\0'; return; } } if (key == K_BACKSPACE || key == K_LEFTARROW) { if (Key.LinePos > 1) { Key.LinePos--; } return; } if (key == K_UPARROW) { do { _HistoryLine = (_HistoryLine - 1) & 31; } while (_HistoryLine != _EditLine && (_Lines[_HistoryLine][1] == 0)); if (_HistoryLine == _EditLine) { _HistoryLine = (_EditLine + 1) & 31; } Array.Copy(_Lines[_HistoryLine], _Lines[_EditLine], MAXCMDLINE); Key.LinePos = 0; while (_Lines[_EditLine][Key.LinePos] != '\0' && Key.LinePos < MAXCMDLINE) { Key.LinePos++; } return; } if (key == K_DOWNARROW) { if (_HistoryLine == _EditLine) { return; } do { _HistoryLine = (_HistoryLine + 1) & 31; }while (_HistoryLine != _EditLine && (_Lines[_HistoryLine][1] == '\0')); if (_HistoryLine == _EditLine) { _Lines[_EditLine][0] = ']'; Key.LinePos = 1; } else { Array.Copy(_Lines[_HistoryLine], _Lines[_EditLine], MAXCMDLINE); Key.LinePos = 0; while (_Lines[_EditLine][Key.LinePos] != '\0' && Key.LinePos < MAXCMDLINE) { Key.LinePos++; } } return; } if (key == K_PGUP || key == K_MWHEELUP) { Con.BackScroll += 2; if (Con.BackScroll > Con.TotalLines - (Scr.vid.height >> 3) - 1) { Con.BackScroll = Con.TotalLines - (Scr.vid.height >> 3) - 1; } return; } if (key == K_PGDN || key == K_MWHEELDOWN) { Con.BackScroll -= 2; if (Con.BackScroll < 0) { Con.BackScroll = 0; } return; } if (key == K_HOME) { Con.BackScroll = Con.TotalLines - (Scr.vid.height >> 3) - 1; return; } if (key == K_END) { Con.BackScroll = 0; return; } if (key < 32 || key > 127) { return; // non printable } if (Key.LinePos < MAXCMDLINE - 1) { _Lines[_EditLine][Key.LinePos] = (char)key; Key.LinePos++; _Lines[_EditLine][Key.LinePos] = '\0'; } }