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

📄 server.cpp

📁 这是一个服务端/客户端模式的小型网络游戏
💻 CPP
字号:
#include <windows.h>
#include <stdio.h>

#include "Core_Global.h"
#include "Server.h"

cServer::cServer(){
  // Allocate message queue and reset message pointers
  m_Messages = new sMessage[NUM_MESSAGES]();
  m_MsgHead = m_MsgTail = 0;
}

void cServer::init(LPCRITICAL_SECTION MessageCS){
  m_MessageCS = MessageCS;
}

///////////////////////////////////////////////////////////
// Network send and receive functions
///////////////////////////////////////////////////////////
BOOL cServer::SendNetworkMessage(void *Msg, long SendFlags, int To)
{
  sMessageHeader *mh = (sMessageHeader*)Msg;
  unsigned long Size;

  // Get size of message to send
  if((Size = mh->Size) == 0)
    return FALSE;

  // Set destination to all players if To == -1
  if(To == -1)
    To = DPNID_ALL_PLAYERS_GROUP;

  // Send the message
  return Send(To, Msg, Size, SendFlags);
}

BOOL cServer::CreatePlayer(DPNMSG_CREATE_PLAYER *Msg)
{
  sCreatePlayerMessage cpm;

  // Setup message data
  cpm.Header.Type     = MSG_CREATE_PLAYER;
  cpm.Header.Size     = sizeof(sCreatePlayerMessage);
  cpm.Header.PlayerID = Msg->dpnidPlayer;

  QueueMessage(&cpm); // Queue the message
  
  return TRUE;
}

BOOL cServer::DestroyPlayer(DPNMSG_DESTROY_PLAYER *Msg)
{
  sDestroyPlayerMessage dpm;

  // Setup message data
  dpm.Header.Type     = MSG_DESTROY_PLAYER;
  dpm.Header.Size     = sizeof(sDestroyPlayerMessage);
  dpm.Header.PlayerID = Msg->dpnidPlayer;
  
  QueueMessage(&dpm); // Queue the message

  return TRUE;
}

BOOL cServer::Receive(DPNMSG_RECEIVE *Msg)
{
  sMessageHeader *mh = (sMessageHeader*)Msg->pReceiveData;

  // Make sure it's a valid message type and queue it
  switch(mh->Type) {
    case MSG_ASSIGNID: 
      // Store player ID before continuing
      mh->PlayerID = Msg->dpnidSender;
    default:
      // Add message to queue
      QueueMessage((void*)Msg->pReceiveData);
      break;
  }

  return TRUE;
}

///////////////////////////////////////////////////////////
// Message queue functions
///////////////////////////////////////////////////////////
BOOL cServer::QueueMessage(void *Msg)
{
  sMessageHeader *mh = (sMessageHeader*)Msg;

  // Error checking - make sure there's a message array
  if(m_Messages == NULL)
    return FALSE;

  // Return if no room left in queue
  if(((m_MsgHead+1) % NUM_MESSAGES) == m_MsgTail)
    return FALSE;

  // Stuff message into queue
  if(mh->Size <= sizeof(sMessage)) {
    // Start the critical section
    EnterCriticalSection(m_MessageCS);

    memcpy(&m_Messages[m_MsgHead], Msg, mh->Size);

    // Go to next empty message (flip around if at end)
    m_MsgHead++;
    if(m_MsgHead >= NUM_MESSAGES)
      m_MsgHead = 0;

    // Leave the critical section
    LeaveCriticalSection(m_MessageCS);
  }

  return TRUE;
}

BOOL cServer::Free()
{
  // Delete message array
  delete [] m_Messages;
  m_Messages = NULL;

  return TRUE;
}

⌨️ 快捷键说明

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