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

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

#ifndef __connector_hpp
#define __connector_hpp

#include <cstring>
#include <cassert>

#ifdef WIN32
#include <winsock2.h>
#endif  // end of WIN32

#ifdef linux
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>

#define SOCKET_ERROR -1
#define INVALID_SOCKET -1

#define closesocket close
#endif  // end of linux

class connector
{
protected:
  connector ();
  ~connector ();

  void recv_int (int sock, int& n);
  void send_int (int sock, int n);
  void recv_bool (int sock, bool& b);
  void send_bool (int sock, bool b);
  void recv_name (int sock, char* name);
  void send_name (int sock, const char* name);
};

connector::connector ()
{
#ifdef WIN32
  // the Windows Socket need WSAStartup
  WORD version;
  WSADATA data;
  
  version = MAKEWORD (1, 1);
  if (::WSAStartup (version, &data) != 0)
    throw "Windows Socket Startup Error!";

  if ((LOBYTE (data.wVersion) != 1) || (HIBYTE (data.wVersion) != 1))
    {
      ::WSACleanup ();
      throw "Windows Socket Version Error!";
    }
#endif
}

connector::~connector ()
{
#ifdef WIN32
  ::WSACleanup();
#endif
}

void
connector::recv_int (int sock, int& n)
{
  union
  {
    int i;
    char b[4];
  };

  if (::recv (sock, b, 4, 0) == SOCKET_ERROR)
    {
      throw "receive int error!";
    }

  n = ::ntohl (i);
}

void
connector::send_int (int sock, int n)
{
  union
  {
    int i;
    char b[4];
  };

  i = ::htonl (n);
  
  if (::send (sock, b, 4, 0) == SOCKET_ERROR)
    {
      throw "send int error!";
    }
}

void
connector::recv_bool (int sock, bool& b)
{
  int t;
  recv_int (sock, t);
  b = (t == 1);
}

void
connector::send_bool (int sock, bool b)
{
  send_int (sock, b ? 1 : 0);
}

void
connector::recv_name (int sock, char* name)
{
  assert ((sock != -1) && (name != 0));

  int len;
  try
    {
      recv_int (sock, len);
    }
  catch (char*)
    {
      throw "receive name length error!";
    }
  if (::recv (sock, name, len, 0) == SOCKET_ERROR)
    {
      throw "receive name error!";
    }
  name[len] = '\0';
}

void
connector::send_name (int sock, const char* name)
{
  assert ((sock != -1) && (name != 0));

  int len = std::strlen (name);
  try
    {
      send_int (sock, len);
    }
  catch (char*)
    {
      throw "send name length error!";
    }
  if (::send (sock, name, len, 0) == SOCKET_ERROR)
    {
      throw "send name error!";
    }
}

#endif

⌨️ 快捷键说明

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