source: golgotha/src/golg/net/server.cc

Last change on this file was 80, checked in by Sam Hocevar, 12 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 3.6 KB
Line 
1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9#include "net/server.hh"
10#include "net/command.hh"
11#include "network/net_prot.hh"
12#include "file/ram_file.hh"
13
14g1_server_class *g1_server=0;
15
16void g1_server_class::client::cleanup()
17{
18  if (addr)
19  {
20    delete addr;
21    delete username;
22  }
23  if (send)
24    delete send;
25  addr=0;
26  send=0;
27}
28
29g1_server_class::g1_server_class(int use_port, i4_net_protocol *protocol)
30  : protocol(protocol)
31
32  map_name=new i4_str(i4gets("tmp_savename"));
33  udp_port=0;
34  memset(clients, 0, sizeof(clients));
35  list_changed=i4_F;
36
37  if (protocol)
38  {
39    udp_port=protocol->listen(use_port, I4_PACKETED_DATA);
40    if (!udp_port)
41      i4_warning("could not bind to port, server already running?");
42   
43
44
45  }
46}
47
48void start_game() { ; }
49
50
51void g1_server_class::send_player_joined(int client_num)
52{
53  w8 packet[512];
54  i4_ram_file_class r(packet, sizeof(packet));
55
56  r.write_8(G1_PK_YOU_HAVE_JOINED);
57  r.write_16(client_num+1);
58  r.write_counted_str(*map_name);
59
60  clients[client_num].send->write(packet, r.tell());
61}
62
63void g1_server_class::process_client_packet(w8 *packet,
64                                            int packet_length,
65                                            int client_num)
66{
67  clients[client_num].last_data.get();
68
69  i4_ram_file_class r(packet, packet_length);
70  if (r.read_8()==G1_PK_I_WANNA_JOIN)
71  {
72    r.read_16();  // skip use port
73
74    delete clients[client_num].username;
75    clients[client_num].username=r.read_counted_str();
76    send_player_joined(client_num);
77  }   
78}
79
80
81void g1_server_class::poll()
82{
83  int i;
84
85  while (udp_port && udp_port->ready_to_read())
86  {
87    w8 packet[512];
88    i4_net_address *a;
89    int len=udp_port->read_from(packet, sizeof(packet), a);
90   
91    // see if this was from one of our clients
92
93    int found=0, free_spot=-1;
94
95    for (i=0; i<G1_MAX_PLAYERS; i++)
96    {
97      if (clients[i].addr)
98      {
99        if (clients[i].addr->equals(a))
100        {
101          process_client_packet(packet, len, i);
102          found=1;
103        }
104      }
105      else free_spot=i;
106    }
107
108    if (!found && free_spot!=-1)
109    {
110      i4_ram_file_class r(packet, len);
111      if (r.read_8()==G1_PK_I_WANNA_JOIN)
112      {
113        clients[free_spot].addr=a->copy();
114        clients[free_spot].addr->set_port(r.read_16());
115        clients[free_spot].username=r.read_counted_str();
116       
117        clients[free_spot].send=protocol->connect(clients[free_spot].addr, I4_PACKETED_DATA);
118
119        if (clients[free_spot].send)
120          send_player_joined(free_spot);
121        else
122          clients[free_spot].cleanup();
123
124        list_changed=i4_T;
125      }   
126    }
127
128    delete a;
129  }
130
131  if (state==WAITING_FOR_PLAYERS)
132  {
133    i4_time_class now;
134    for (i=0; i<G1_MAX_PLAYERS; i++)
135    {     
136      if (clients[i].addr && now.milli_diff(clients[i].last_data)>1000)
137      {
138        clients[i].cleanup();
139        list_changed=i4_T;
140      }
141
142    }
143  }
144
145}
146
147
148g1_server_class::~g1_server_class()
149{
150  delete map_name;
151
152  for (int i=0; i<G1_MAX_PLAYERS; i++)
153    if (clients[i].addr)
154      delete clients[i].addr;
155  delete udp_port;
156}
Note: See TracBrowser for help on using the repository browser.