Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <errno.h>
  5. #include "i_system.h"
  6. #include "d_event.h"
  7. #include "d_net.h"
  8. #include "m_argv.h"
  9. #include "doomstat.h"
  10. #include "i_net.h"
  11.  
  12. #ifndef B_HOST_IS_LENDIAN
  13. #define B_HOST_IS_LENDIAN 1
  14. #endif
  15. #if !defined(sparc) && B_HOST_IS_LENDIAN
  16. #ifndef ntohl
  17. #define ntohl(x) \
  18.         ((unsigned long int)((((unsigned long int)(x) & 0x000000ffU) << 24) | \
  19.                              (((unsigned long int)(x) & 0x0000ff00U) <<  8) | \
  20.                              (((unsigned long int)(x) & 0x00ff0000U) >>  8) | \
  21.                              (((unsigned long int)(x) & 0xff000000U) >> 24)))
  22. #endif
  23.  
  24. #ifndef ntohs
  25. #define ntohs(x) \
  26.         ((unsigned short int)((((unsigned short int)(x) & 0x00ff) << 8) | \
  27.                               (((unsigned short int)(x) & 0xff00) >> 8)))
  28. #endif
  29.          
  30. #ifndef htonl
  31. #define htonl(x) ntohl(x)
  32. #endif
  33. #ifndef htons
  34. #define htons(x) ntohs(x)
  35. #endif
  36. #endif
  37.  
  38. void    NetSend (void);
  39. int NetListen (void);
  40.  
  41.  
  42. //
  43. // NETWORKING
  44. //
  45.  
  46. #ifndef IPPORT_USERRESERVED
  47. #define IPPORT_USERRESERVED     5000
  48. #endif
  49.  
  50. int     DOOMPORT = (IPPORT_USERRESERVED+0x1d);
  51. int     sendsocket[MAXNETNODES];
  52. int     insocket;
  53.  
  54. void    (*netget) (void);
  55. void    (*netsend) (void);
  56.  
  57. static int first_user_port=IPPORT_USERRESERVED+0x1D+0x10;
  58.  
  59. int GetAvailPort(void)
  60. {
  61.  int i,d0;
  62.  //for(i=0;i<1024;i++)
  63.  //{
  64.  // __asm__ __volatile__(
  65.  //     "int $0x40"
  66.  //     :"=a"(d0)
  67.  //     :"0"(53),"b"(9),"c"(first_user_port+i));
  68.  // if(d0==1) return i+first_user_port;
  69.  //}
  70.  I_Error("Unable to get new port\n");
  71.  return -1;
  72. }
  73.  
  74.  
  75. int CreateInputUDPsocket(void)
  76. {
  77.  int d0;
  78.  //__asm__ __volatile__(
  79.  //    "int $0x40"
  80.  //    :"=a"(d0)
  81.  //    :"0"(53),"b"(1),"c"(DOOMPORT),"d"(0),"S"(0));
  82.  //if(d0==0xffffffff)
  83.  //{
  84.   I_Error("Unable to create socket\n");
  85.  //}
  86.  return d0;
  87. }
  88.  
  89. int CreateOutputUDPSocket(int remote_ip)
  90. {
  91.  int d0;
  92.  int p;
  93. // p=GetAvailPort();
  94. // __asm__ __volatile__(
  95. //     "int $0x40"
  96. //     :"=a"(d0)
  97. //     :"0"(53),"b"(0),"c"(p),"d"(DOOMPORT),"S"(remote_ip));
  98. // if(d0==0xffffffff)
  99. // {
  100.   I_Error("Unable to create output socket\n");
  101. // }
  102.  return d0;
  103. }
  104.  
  105. //
  106. // PacketSend
  107. //
  108. void PacketSend (void)
  109. {
  110.  int c;
  111.  doomdata_t sw;                
  112.  // byte swap
  113.  sw.checksum = htonl(netbuffer->checksum);
  114.  sw.player = netbuffer->player;
  115.  sw.retransmitfrom = netbuffer->retransmitfrom;
  116.  sw.starttic = netbuffer->starttic;
  117.  sw.numtics = netbuffer->numtics;
  118.  for (c=0 ; c< netbuffer->numtics ; c++)
  119.  {
  120.   sw.cmds[c].forwardmove = netbuffer->cmds[c].forwardmove;
  121.   sw.cmds[c].sidemove = netbuffer->cmds[c].sidemove;
  122.   sw.cmds[c].angleturn = htons(netbuffer->cmds[c].angleturn);
  123.   sw.cmds[c].consistancy = htons(netbuffer->cmds[c].consistancy);
  124.   sw.cmds[c].chatchar = netbuffer->cmds[c].chatchar;
  125.   sw.cmds[c].buttons = netbuffer->cmds[c].buttons;
  126.  }
  127. // __libclog_printf("Sending packet %u to node %u\n",gametic,
  128. //     doomcom->remotenode);
  129. // __asm__ __volatile__(
  130. //     "int $0x40"
  131. //     :"=a"(c)
  132. //     :"0"(53),"b"(4),"c"(sendsocket[doomcom->remotenode]),
  133. //      "d"(doomcom->datalength),"S"((long)&sw));
  134. // if(c==-1)
  135. // {
  136. //  __libclog_printf("Unable to send packet to remote node\n");
  137. // }
  138. }
  139.  
  140. //
  141. // PacketGet
  142. //
  143. void PacketGet (void)
  144. {
  145. }
  146.  
  147. int GetLocalAddress (void)
  148. {
  149.  int d0;
  150. // __asm__ __volatile__(
  151. //     "int $0x40"
  152. //     :"=a"(d0)
  153. //     :"0"(52),"b"(1));
  154.  return d0;
  155. }
  156.  
  157.  
  158. //
  159. // I_InitNetwork
  160. //
  161. void I_InitNetwork (void)
  162. {
  163.     boolean             trueval = true;
  164.     int                 i;
  165.     int                 p;
  166.        
  167.     doomcom = malloc (sizeof (*doomcom) );
  168.     memset (doomcom, 0, sizeof(*doomcom) );
  169.    
  170.     // set up for network
  171.     i = M_CheckParm ("-dup");
  172.     if (i && i< myargc-1)
  173.     {
  174.         doomcom->ticdup = myargv[i+1][0]-'0';
  175.         if (doomcom->ticdup < 1)
  176.             doomcom->ticdup = 1;
  177.         if (doomcom->ticdup > 9)
  178.             doomcom->ticdup = 9;
  179.     }
  180.     else
  181.         doomcom-> ticdup = 1;
  182.        
  183.     if (M_CheckParm ("-extratic"))
  184.         doomcom-> extratics = 1;
  185.     else
  186.         doomcom-> extratics = 0;
  187.                
  188.     p = M_CheckParm ("-port");
  189.     if (p && p<myargc-1)
  190.     {
  191.         DOOMPORT = atoi (myargv[p+1]);
  192. //      __libclog_printf ("using alternate port %i\n",DOOMPORT);
  193.     }
  194.  
  195.     // parse network game options,
  196.     //  -net <consoleplayer> <host> <host> ...
  197.     i = M_CheckParm ("-net");
  198.  
  199.     if (!i)
  200.     {
  201.         // single player game
  202.         netgame = false;
  203.         doomcom->id = DOOMCOM_ID;
  204.         doomcom->numplayers = doomcom->numnodes = 1;
  205.         doomcom->deathmatch = false;
  206.         doomcom->consoleplayer = 0;
  207.         return;
  208.     }
  209.  
  210.     netsend = PacketSend;
  211.     netget = PacketGet;
  212.     netgame = true;
  213.  
  214.     // parse player number and host list
  215.     doomcom->consoleplayer = myargv[i+1][0]-'1';
  216.  
  217.     doomcom->numnodes = 1;      // this node for sure
  218.     doomcom->id = DOOMCOM_ID;
  219.     doomcom->numplayers = doomcom->numnodes;
  220.     sendsocket[0]=0;
  221.     for(i=1;i<MAXNETNODES;i++)
  222.     {
  223.      sendsocket[i]=-1;
  224.     }
  225.     insocket=CreateInputUDPsocket();
  226.  //   __libclog_printf("DOOM: Input UDP socket is %d\n",insocket);
  227. }
  228.  
  229.  
  230. void I_NetCmd (void)
  231. {
  232.     if (doomcom->command == CMD_SEND)
  233.     {
  234.         netsend ();
  235.     }
  236.     else if (doomcom->command == CMD_GET)
  237.     {
  238.         netget ();
  239.     }
  240.     else
  241.         I_Error ("Bad net cmd: %i\n",doomcom->command);
  242. }
  243.