⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 server_connector.hpp

📁 本程序是主要是扫雷
💻 HPP
字号:
/*     Copyright(c) Ben Bear 2003-2004  */

//  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.

/*  This file is net part of the server.  It connects to all players,
 *  receive from the them and send data to the them.
 */

//  This version will only use single thread.  

#ifndef __server_connector_hpp
#define __server_connector_hpp

#include <cassert>

#include "info.hpp"
#include "connector.hpp"

class server_con: public connector
{
protected:
  // main socket of the server
  int server_sock;

  // player's socket
  int player_sock[6];

  // temp of socket
  int tmp_sock;

public:
  server_con ();
  ~server_con ();
  
  // start to listen at the port
  bool start_net ();

  // close all the socket
  void close_net ();

  // wait for a new player
  bool wait_player (char* name);

  // send a message[m] to the wait player the close
  void waiter_close (int m);

  // save the socket to player[p]
  void save_sock (int p);

  // delete and close the socket of player[p]
  void delete_sock (int p);

  // receive an int from player[p]
  int recv_int (int p);

  // send an int[n] to player[p]
  void send_int (int p, int n);

  // send a bool[b] to player[p]
  void send_bool (int p, bool b);

  // send player[p]'s [name]
  void send_name (int p, const char* name);
};

server_con::server_con ()
{

  server_sock = -1;

  for (int i = 0; i < 6; i++)
    player_sock[i] = -1;

  tmp_sock = -1;
}

server_con::~server_con ()
{
  if (server_sock != -1)
    close_net ();
}

bool
server_con::start_net ()
{
  assert (server_sock == -1);

  int length;
  
  server_sock = socket (AF_INET, SOCK_STREAM, 0);
  if (server_sock == INVALID_SOCKET)
    {
      server_sock = -1;
      throw "Can't open the socket!";
    }
  
  struct sockaddr_in server;
  server.sin_family = AF_INET;
  server.sin_addr.s_addr = htonl(INADDR_ANY);
  server.sin_port = htons(SERVER_PORT);

  if (bind (server_sock, (struct sockaddr*) &server, sizeof server)
      == SOCKET_ERROR)
    {
      server_sock = -1;
      throw "Can't bind the socket!";
    }

  if (listen (server_sock, 6) == SOCKET_ERROR)
    {
      server_sock = -1;
      throw "Can't listen the socket";
    }
  return true;
}

void
server_con::close_net ()
{
  assert (server_sock != -1);

  ::closesocket (server_sock);
}

bool
server_con::wait_player (char* name)
{
  assert (server_sock != -1);

  struct sockaddr addr;
#ifdef linux
  socklen_t len = sizeof (addr);
#else
  int len = sizeof (addr);
#endif
  tmp_sock = accept (server_sock, &addr, &len);
  if (tmp_sock == INVALID_SOCKET)
    {
      tmp_sock = -1;
      throw "Can't accept the player!";
    }
  
  connector::recv_name (tmp_sock, name);
  return true;
}

void
server_con::waiter_close (int m)
{
  connector::send_int (tmp_sock, m);

  ::closesocket (tmp_sock);
}

void
server_con::save_sock (int p)
{
  assert ((tmp_sock != -1));
  assert (player_sock[p] == -1);

  player_sock[p] = tmp_sock;
  tmp_sock = -1;
}

void
server_con::delete_sock (int p)
{
  assert (player_sock[p] != -1);

  ::closesocket (player_sock[p]);
  
  player_sock[p] = -1;
}

int
server_con::recv_int (int p)
{
  assert (player_sock[p] != -1);
  int tmp;
  connector::recv_int (player_sock[p], tmp);
  return tmp;
}

void
server_con::send_int (int p, int n)
{
  assert (player_sock[p] != -1);
  connector::send_int (player_sock[p], n);
}

void
server_con::send_bool (int p, bool b)
{
  assert (player_sock[p] != -1);
  connector::send_bool (player_sock[p], b);
}

void
server_con::send_name (int p, const char* name)
{
  assert ((player_sock[p] != -1) && (name != 0));
  connector::send_name (player_sock[p], name);
}

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -