File: network_server.cpp

package info (click to toggle)
warmux 1%3A11.04.1%2Brepack2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid
  • size: 126,388 kB
  • sloc: cpp: 186,040; xml: 8,909; sh: 3,358; makefile: 1,052; ansic: 713
file content (172 lines) | stat: -rw-r--r-- 5,031 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/******************************************************************************
 *  Warmux is a convivial mass murder game.
 *  Copyright (C) 2001-2011 Warmux Team.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 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.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 ******************************************************************************
 * Network server layer for Warmux.
 *****************************************************************************/

#include <algorithm>
#include <sys/types.h>
#ifdef LOG_NETWORK
#  include <sys/stat.h>
#  include <fcntl.h>
#  ifdef WIN32
#    include <io.h>
#  endif
#endif

#include <WARMUX_socket.h>
#include <WARMUX_distant_cpu.h>
#include <SDL_thread.h>

#include "include/action_handler.h"
#include "network/network_server.h"
#include "tool/string_tools.h"

//-----------------------------------------------------------------------------

NetworkServer::NetworkServer(const std::string& game_name, const std::string& password) :
  Network(game_name, password),
  port(0)
{
#ifdef LOG_NETWORK
  fin = open("./network_server.in", O_CREAT | O_TRUNC | O_WRONLY | O_SYNC, S_IRUSR | S_IWUSR | S_IRGRP);
  fout = open("./network_server.out", O_CREAT | O_TRUNC | O_WRONLY | O_SYNC, S_IRUSR | S_IWUSR | S_IRGRP);
#endif
  game_master_player = true;
}

NetworkServer::~NetworkServer()
{
}

void NetworkServer::HandleAction(Action* a, DistantComputer* sender)
{
  // Repeat the packet to other clients
  SendActionToAllExceptOne(*a, sender);

  ActionHandler::GetInstance()->NewAction(a, false);
}

uint NetworkServer::NextPlayerId() const
{
  uint player_id = GetPlayer().GetId() + 1;

  const std::list<DistantComputer*>& hosts = LockRemoteHosts();
  std::list<DistantComputer*>::const_iterator it;
  std::list<Player>::const_iterator player;
  for (it = hosts.begin(); it != hosts.end(); it++) {

    const std::list<Player>& players = (*it)->GetPlayers();

    for (player = players.begin(); player != players.end(); player++) {
      if (player_id <= player->GetId()) {
        player_id = player->GetId() + 1;
      }
    }
  }
  UnlockRemoteHosts();

  return player_id;
}

bool NetworkServer::HandShake(WSocket& client_socket,
                              std::string& nickname,
                              uint player_id) const
{
  return WNet::Server_HandShake(client_socket, GetGameName(), GetPassword(),
                                nickname, player_id, false);
}

void NetworkServer::WaitActionSleep()
{
  if (server_socket.IsConnected()) {

    // Check for an incoming connection
    WSocket* incoming = server_socket.LookForClient();
    if (incoming) {
      std::string nickname;
      uint player_id = NextPlayerId();

      if (!HandShake(*incoming, nickname, player_id))
        return;

      socket_set->AddSocket(incoming);

      DistantComputer* client = new DistantComputer(incoming, nickname, player_id);
      AddRemoteHost(client);
      SendInitialGameInfo(client, player_id);

      if (GetNbPlayersConnected() >= max_nb_players)
        RejectIncoming();
    }
  }
}

void NetworkServer::RejectIncoming()
{
  server_socket.Disconnect();
}

//-----------------------------------------------------------------------------

connection_state_t NetworkServer::StartServer(const std::string &net_port, uint _max_nb_players)
{
  WNet::Init();

  // The server starts listening for clients
  printf("o Starting server on port %s...\n", net_port.c_str());

  std::list<DistantComputer*>& hosts = LockRemoteHosts();
  hosts.clear();
  UnlockRemoteHosts();

  // Convert port number (std::string port) into SDL port number format:
  if (!str2int(net_port, port)) {
    return CONN_BAD_PORT;
  }

  // Open the port to listen to
  if (!server_socket.AcceptIncoming(port)) {
    return CONN_BAD_PORT;
  }

  printf("o Server successfully started\n");
  max_nb_players = _max_nb_players;
  socket_set = WSocketSet::GetSocketSet(max_nb_players);
  if (!socket_set) {
    return CONN_REJECTED;
  }

  NetworkThread::Start();

  SetState(WNet::NETWORK_MENU_INIT);
  return CONNECTED;
}


void NetworkServer::CloseConnection(std::list<DistantComputer*>::iterator closed)
{
  RemoveRemoteHost(closed);

  if (GetNbPlayersConnected() == max_nb_players) {
    // A new player will be able to connect, so we reopen the socket
    // For incoming connections
    printf("Allowing new connections\n");
    server_socket.AcceptIncoming(port);
  }
}