rewrote memory system entirely (hunk, cache, and zone are gone, memory pools replaced...
[xonotic/darkplaces.git] / net_main.c
index 0add1f2..df40cad 100644 (file)
@@ -8,7 +8,7 @@ of the License, or (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
 See the GNU General Public License for more details.
 
@@ -20,13 +20,11 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 // net_main.c
 
 #include "quakedef.h"
-#include "net_vcr.h"
 
 qsocket_t      *net_activeSockets = NULL;
 qsocket_t      *net_freeSockets = NULL;
 int                    net_numsockets = 0;
 
-qboolean       serialAvailable = false;
 qboolean       ipxAvailable = false;
 qboolean       tcpipAvailable = false;
 
@@ -36,11 +34,6 @@ int                  DEFAULTnet_hostport = 26000;
 char           my_ipx_address[NET_NAMELEN];
 char           my_tcpip_address[NET_NAMELEN];
 
-void (*GetComPortConfig) (int portNumber, int *port, int *irq, int *baud, qboolean *useModem);
-void (*SetComPortConfig) (int portNumber, int port, int irq, int baud, qboolean useModem);
-void (*GetModemConfig) (int portNumber, char *dialType, char *clear, char *init, char *hangup);
-void (*SetModemConfig) (int portNumber, char *dialType, char *clear, char *init, char *hangup);
-
 static qboolean        listening = false;
 
 qboolean       slistInProgress = false;
@@ -63,21 +56,10 @@ int messagesReceived = 0;
 int unreliableMessagesSent = 0;
 int unreliableMessagesReceived = 0;
 
-cvar_t net_messagetimeout = {"net_messagetimeout","300"};
-cvar_t hostname = {"hostname", "UNNAMED"};
+cvar_t net_messagetimeout = {0, "net_messagetimeout","300"};
+cvar_t hostname = {CVAR_SAVE, "hostname", "UNNAMED"};
 
 qboolean       configRestored = false;
-cvar_t config_com_port = {"_config_com_port", "0x3f8", true};
-cvar_t config_com_irq = {"_config_com_irq", "4", true};
-cvar_t config_com_baud = {"_config_com_baud", "57600", true};
-cvar_t config_com_modem = {"_config_com_modem", "1", true};
-cvar_t config_modem_dialtype = {"_config_modem_dialtype", "T", true};
-cvar_t config_modem_clear = {"_config_modem_clear", "ATZ", true};
-cvar_t config_modem_init = {"_config_modem_init", "", true};
-cvar_t config_modem_hangup = {"_config_modem_hangup", "AT H", true};
-
-int    vcrFile = -1;
-qboolean recording = false;
 
 // these two macros are to make the code more readable
 #define sfunc  net_drivers[sock->driver]
@@ -90,7 +72,7 @@ double                        net_time;
 
 double SetNetTime(void)
 {
-       net_time = Sys_FloatTime();
+       net_time = Sys_DoubleTime();
        return net_time;
 }
 
@@ -302,7 +284,7 @@ void NET_Slist_f (void)
        }
 
        slistInProgress = true;
-       slistStartTime = Sys_FloatTime();
+       slistStartTime = Sys_DoubleTime();
 
        SchedulePollProcedure(&slistSendProcedure, 0.0);
        SchedulePollProcedure(&slistPollProcedure, 0.1);
@@ -322,7 +304,7 @@ static void Slist_Send(void)
                dfunc.SearchForHosts (true);
        }
 
-       if ((Sys_FloatTime() - slistStartTime) < 0.5)
+       if ((Sys_DoubleTime() - slistStartTime) < 0.5)
                SchedulePollProcedure(&slistSendProcedure, 0.75);
 }
 
@@ -341,7 +323,7 @@ static void Slist_Poll(void)
        if (! slistSilent)
                PrintSlist();
 
-       if ((Sys_FloatTime() - slistStartTime) < 1.5)
+       if ((Sys_DoubleTime() - slistStartTime) < 1.5)
        {
                SchedulePollProcedure(&slistPollProcedure, 0.1);
                return;
@@ -446,13 +428,6 @@ NET_CheckNewConnections
 ===================
 */
 
-struct
-{
-       double  time;
-       int             op;
-       long    session;
-} vcrConnect;
-
 qsocket_t *NET_CheckNewConnections (void)
 {
        qsocket_t       *ret;
@@ -467,25 +442,7 @@ qsocket_t *NET_CheckNewConnections (void)
                        continue;
                ret = dfunc.CheckNewConnections ();
                if (ret)
-               {
-                       if (recording)
-                       {
-                               vcrConnect.time = host_time;
-                               vcrConnect.op = VCR_OP_CONNECT;
-                               vcrConnect.session = (long)ret;
-                               Sys_FileWrite (vcrFile, &vcrConnect, sizeof(vcrConnect));
-                               Sys_FileWrite (vcrFile, ret->address, NET_NAMELEN);
-                       }
                        return ret;
-               }
-       }
-       
-       if (recording)
-       {
-               vcrConnect.time = host_time;
-               vcrConnect.op = VCR_OP_CONNECT;
-               vcrConnect.session = 0;
-               Sys_FileWrite (vcrFile, &vcrConnect, sizeof(vcrConnect));
        }
 
        return NULL;
@@ -525,15 +482,6 @@ returns -1 if connection is invalid
 =================
 */
 
-struct
-{
-       double  time;
-       int             op;
-       long    session;
-       int             ret;
-       int             len;
-} vcrGetMessage;
-
 extern void PrintStats(qsocket_t *s);
 
 int    NET_GetMessage (qsocket_t *sock)
@@ -574,28 +522,6 @@ int        NET_GetMessage (qsocket_t *sock)
                        else if (ret == 2)
                                unreliableMessagesReceived++;
                }
-
-               if (recording)
-               {
-                       vcrGetMessage.time = host_time;
-                       vcrGetMessage.op = VCR_OP_GETMESSAGE;
-                       vcrGetMessage.session = (long)sock;
-                       vcrGetMessage.ret = ret;
-                       vcrGetMessage.len = net_message.cursize;
-                       Sys_FileWrite (vcrFile, &vcrGetMessage, 24);
-                       Sys_FileWrite (vcrFile, net_message.data, net_message.cursize);
-               }
-       }
-       else
-       {
-               if (recording)
-               {
-                       vcrGetMessage.time = host_time;
-                       vcrGetMessage.op = VCR_OP_GETMESSAGE;
-                       vcrGetMessage.session = (long)sock;
-                       vcrGetMessage.ret = ret;
-                       Sys_FileWrite (vcrFile, &vcrGetMessage, 20);
-               }
        }
 
        return ret;
@@ -613,14 +539,6 @@ returns 1 if the message was sent properly
 returns -1 if the connection died
 ==================
 */
-struct
-{
-       double  time;
-       int             op;
-       long    session;
-       int             r;
-} vcrSendMessage;
-
 int NET_SendMessage (qsocket_t *sock, sizebuf_t *data)
 {
        int             r;
@@ -638,15 +556,6 @@ int NET_SendMessage (qsocket_t *sock, sizebuf_t *data)
        r = sfunc.QSendMessage(sock, data);
        if (r == 1 && sock->driver)
                messagesSent++;
-
-       if (recording)
-       {
-               vcrSendMessage.time = host_time;
-               vcrSendMessage.op = VCR_OP_SENDMESSAGE;
-               vcrSendMessage.session = (long)sock;
-               vcrSendMessage.r = r;
-               Sys_FileWrite (vcrFile, &vcrSendMessage, 20);
-       }
        
        return r;
 }
@@ -670,15 +579,6 @@ int NET_SendUnreliableMessage (qsocket_t *sock, sizebuf_t *data)
        if (r == 1 && sock->driver)
                unreliableMessagesSent++;
 
-       if (recording)
-       {
-               vcrSendMessage.time = host_time;
-               vcrSendMessage.op = VCR_OP_SENDMESSAGE;
-               vcrSendMessage.session = (long)sock;
-               vcrSendMessage.r = r;
-               Sys_FileWrite (vcrFile, &vcrSendMessage, 20);
-       }
-       
        return r;
 }
 
@@ -705,15 +605,6 @@ qboolean NET_CanSendMessage (qsocket_t *sock)
 
        r = sfunc.CanSendMessage(sock);
        
-       if (recording)
-       {
-               vcrSendMessage.time = host_time;
-               vcrSendMessage.op = VCR_OP_CANSENDMESSAGE;
-               vcrSendMessage.session = (long)sock;
-               vcrSendMessage.r = r;
-               Sys_FileWrite (vcrFile, &vcrSendMessage, 20);
-       }
-       
        return r;
 }
 
@@ -750,7 +641,7 @@ int NET_SendToAll(sizebuf_t *data, int blocktime)
                }
        }
 
-       start = Sys_FloatTime();
+       start = Sys_DoubleTime();
        while (count)
        {
                count = 0;
@@ -785,7 +676,7 @@ int NET_SendToAll(sizebuf_t *data, int blocktime)
                                continue;
                        }
                }
-               if ((Sys_FloatTime() - start) > blocktime)
+               if ((Sys_DoubleTime() - start) > blocktime)
                        break;
        }
        return count;
@@ -800,21 +691,14 @@ NET_Init
 ====================
 */
 
+static mempool_t *net_mempool;
+
 void NET_Init (void)
 {
        int                     i;
        int                     controlSocket;
        qsocket_t       *s;
 
-       if (COM_CheckParm("-playback"))
-       {
-               net_numdrivers = 1;
-               net_drivers[0].Init = VCR_Init;
-       }
-
-       if (COM_CheckParm("-record"))
-               recording = true;
-
        i = COM_CheckParm ("-port");
        if (!i)
                i = COM_CheckParm ("-udpport");
@@ -838,27 +722,21 @@ void NET_Init (void)
 
        SetNetTime();
 
+       net_mempool = Mem_AllocPool("qsocket");
+       s = Mem_Alloc(net_mempool, net_numsockets * sizeof(qsocket_t));
        for (i = 0; i < net_numsockets; i++)
        {
-               s = (qsocket_t *)Hunk_AllocName(sizeof(qsocket_t), "qsocket");
                s->next = net_freeSockets;
                net_freeSockets = s;
                s->disconnected = true;
+               s++;
        }
 
        // allocate space for network message buffer
-       SZ_Alloc (&net_message, NET_MAXMESSAGE);
+       SZ_Alloc (&net_message, NET_MAXMESSAGE, "net_message");
 
        Cvar_RegisterVariable (&net_messagetimeout);
        Cvar_RegisterVariable (&hostname);
-       Cvar_RegisterVariable (&config_com_port);
-       Cvar_RegisterVariable (&config_com_irq);
-       Cvar_RegisterVariable (&config_com_baud);
-       Cvar_RegisterVariable (&config_com_modem);
-       Cvar_RegisterVariable (&config_modem_dialtype);
-       Cvar_RegisterVariable (&config_modem_clear);
-       Cvar_RegisterVariable (&config_modem_init);
-       Cvar_RegisterVariable (&config_modem_hangup);
 
        Cmd_AddCommand ("slist", NET_Slist_f);
        Cmd_AddCommand ("listen", NET_Listen_f);
@@ -910,11 +788,7 @@ void               NET_Shutdown (void)
                }
        }
 
-       if (vcrFile != -1)
-       {
-               Con_Printf ("Closing vcrfile.\n");
-               Sys_FileClose(vcrFile);
-       }
+       Mem_FreePool(&net_mempool);
 }
 
 
@@ -923,21 +797,9 @@ static PollProcedure *pollProcedureList = NULL;
 void NET_Poll(void)
 {
        PollProcedure *pp;
-       qboolean        useModem;
 
        if (!configRestored)
-       {
-               if (serialAvailable)
-               {
-                       if (config_com_modem.value == 1.0)
-                               useModem = true;
-                       else
-                               useModem = false;
-                       SetComPortConfig (0, (int)config_com_port.value, (int)config_com_irq.value, (int)config_com_baud.value, useModem);
-                       SetModemConfig (0, config_modem_dialtype.string, config_modem_clear.string, config_modem_init.string, config_modem_hangup.string);
-               }
                configRestored = true;
-       }
 
        SetNetTime();
 
@@ -955,7 +817,7 @@ void SchedulePollProcedure(PollProcedure *proc, double timeOffset)
 {
        PollProcedure *pp, *prev;
 
-       proc->nextTime = Sys_FloatTime() + timeOffset;
+       proc->nextTime = Sys_DoubleTime() + timeOffset;
        for (pp = pollProcedureList, prev = NULL; pp; pp = pp->next)
        {
                if (pp->nextTime >= proc->nextTime)