source: golgotha/src/golg/net/client.cc @ 80

Last change on this file since 80 was 80, checked in by Sam Hocevar, 11 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: 2.4 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 "network/net_addr.hh"
10#include "network/net_sock.hh"
11#include "network/net_prot.hh"
12#include "net/client.hh"
13#include "net/command.hh"
14#include "file/ram_file.hh"
15#include "resources.hh"
16
17g1_client_class *g1_client=0;
18
19g1_client_class::g1_client_class(i4_net_address *server_address,
20                                 int use_port,
21                                 i4_net_protocol *protocol)
22  : server_address(server_address->copy()),
23    use_port(use_port)
24{
25  listen=0;
26  send=0;
27  map_name=0;
28
29  if (protocol)
30  {
31    listen=protocol->listen(use_port, I4_PACKETED_DATA);
32    send=protocol->connect(server_address, I4_PACKETED_DATA);
33  }
34}
35
36i4_bool g1_client_class::poll()  // returns false if server is not responding
37{
38  if (!listen || !send)
39    return 0;
40
41  if (state==JOIN_START)
42  {
43    i4_time_class now;
44    if (now.milli_diff(last_responce_time)>5000)  // if it's been 5 secs assume server dead
45      return i4_F;
46
47   
48    w8 packet[512];
49    i4_ram_file_class r(packet, sizeof(packet));
50
51    r.write_8(G1_PK_I_WANNA_JOIN);
52    r.write_16(use_port);
53    r.write_counted_str(*g1_resources.username);
54
55    if (!send->write(packet, r.tell()))
56      return i4_F;
57
58  }
59
60  if (listen->ready_to_read())
61  {
62    w8 packet[512];
63    i4_net_address *a;
64    int s=listen->read_from(packet, sizeof(packet), a);
65    if (a->equals(server_address))
66    {
67      i4_ram_file_class r(packet, sizeof(packet));
68
69      if (r.read_8()==G1_PK_YOU_HAVE_JOINED)
70      {
71        player_num=r.read_16();
72       
73        if (map_name)
74          delete map_name;
75        map_name=r.read_counted_str();
76        state=JOIN_START;
77        last_responce_time.get();
78      }
79    }
80    delete a;
81  }
82  return i4_T;
83}
84
85
86g1_client_class::~g1_client_class()
87{
88  delete server_address;
89
90  if (map_name)
91    delete map_name;
92
93  if (listen)
94    delete listen;
95
96  if (send)
97    delete send;
98}
Note: See TracBrowser for help on using the repository browser.