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

📄 rpgnetinterface.cc

📁 五行MMORPG引擎系统V1.0
💻 CC
字号:
/*///////////////////////////////////////////////////////////////////////////
UDP连接封装管理


李亦
2006.6.15
/*///////////////////////////////////////////////////////////////////////////

#include "platform/platform.h"
#include "platform/event.h"
#include "sim/netConnection.h"
#include "server/RPGNetInterface.h"
#include "core/bitStream.h"
#include "math/mRandom.h"
#include "platform/gameInterface.h"

//namespace RPGServer
//{
NetInterface *GNet = NULL;

NetInterface::NetInterface()
{
   AssertFatal(GNet == NULL, "ERROR: Multiple net interfaces declared.");
   GNet = this;

   mLastTimeoutCheckTime = 0;
   mAllowConnections = true;

}

//void NetInterface::initRandomData()
//{
//   mRandomDataInitialized = true;
//   U32 seed = Platform::getRealMilliseconds();
//
//   if(Game->isJournalReading())
//      Game->journalRead(&seed);
//   else if(Game->isJournalWriting())
//      Game->journalWrite(seed);
//
//   MRandomR250 myRandom(seed);
//   for(U32 i = 0; i < 12; i++)
//      mRandomHashData[i] = myRandom.randI();
//}

void NetInterface::addPendingConnection(NetConnection *connection)
{
   Con::printf("Adding a pending connection");
   mPendingConnections.push_back(connection);
}

void NetInterface::removePendingConnection(NetConnection *connection)
{
   for(U32 i = 0; i < mPendingConnections.size(); i++)
      if(mPendingConnections[i] == connection)
         mPendingConnections.erase(i);
}

NetConnection *NetInterface::findPendingConnection(const NetAddress *address, U32 connectSequence)
{
   for(U32 i = 0; i < mPendingConnections.size(); i++)
      if(Net::compareAddresses(address, mPendingConnections[i]->getNetAddress()) &&
            connectSequence == mPendingConnections[i]->getSequence())
         return mPendingConnections[i];
   return NULL;
}

void NetInterface::processPacketReceiveEvent(PacketReceiveEvent *prEvent)
{

   //U32 dataSize = prEvent->size - PacketReceiveEventHeaderSize;
   //BitStream pStream(prEvent->data, dataSize);

   //// Determine what to do with this packet:

   //if(prEvent->data[0] & 0x01) // it's a protocol packet...
   //{
   //   // if the LSB of the first byte is set, it's a game data packet
   //   // so pass it to the appropriate connection.

   //   // lookup the connection in the addressTable
   //   NetConnection *conn = NetConnection::lookup(&prEvent->sourceAddress);
   //   if(conn)
   //      conn->processRawPacket(&pStream);
   //}
}


void NetInterface::handleInfoPacket(const NetAddress *address, U8 packetType, BitStream *stream)
{
}

void NetInterface::processClient()
{
   NetObject::collapseDirtyList(); // collapse all the mask bits...

	NetConnection *walk = NetConnection::getConnectionList();
   for(; walk; walk = walk->getNext())
   {
      if(walk->isConnectionToServer() && (walk->isLocalConnection() || walk->isNetworkConnection()))
		{
			walk->checkPacketRecv(false);
			walk->checkPacketSend(false);
		}
   }

}

void NetInterface::processServer()
{
   NetObject::collapseDirtyList(); // collapse all the mask bits...
	NetConnection *walk = NetConnection::getConnectionList();
   for(; walk; walk = walk->getNext())
   {
      if(!walk->isConnectionToServer() && (walk->isLocalConnection() || walk->isNetworkConnection()))
		{
			walk->checkPacketRecv(false);
         walk->checkPacketSend(false);
		}
   }
}

void NetInterface::startConnection(NetConnection *conn)
{
   addPendingConnection(conn);
   conn->mConnectionSendCount = 0;
   conn->setConnectSequence(Platform::getVirtualMilliseconds());
   conn->setConnectionState(NetConnection::AwaitingChallengeResponse);

   // This is a the client side of the connection, so set the connection to
   // server flag. We need to set this early so that if the connection times
   // out, its onRemove() will handle the cleanup properly.
   conn->setIsConnectionToServer();
	
	AssertWarn(0,"需要调整 NetInterface::startConnection");
   // Everything set, so send off the request.
   //sendConnectChallengeRequest(conn);
}

//void NetInterface::sendDisconnectPacket(NetConnection *conn, const char *reason)
//{
//   Con::printf("Issuing Disconnect packet.");
//
//   // send a disconnect packet...
//   U32 connectSequence = conn->getSequence();
//
//   BitStream *out = BitStream::getPacketStream();
//   out->write(U8(Disconnect));
//   out->write(connectSequence);
//   out->writeString(reason);
//
//   BitStream::sendPacketStream(conn->getNetAddress());
//}

void NetInterface::checkTimeouts()
{
   U32 time = Platform::getVirtualMilliseconds();
   if(time > mLastTimeoutCheckTime + TimeoutCheckInterval)
   {
      //for(U32 i = 0; i < mPendingConnections.size();)
      //{
      //   NetConnection *pending = mPendingConnections[i];

      //   if(pending->getConnectionState() == NetConnection::AwaitingChallengeResponse &&
      //      time > pending->mConnectLastSendTime + ChallengeRetryTime)
      //   {
      //      if(pending->mConnectSendCount > ChallengeRetryCount)
      //      {
      //         pending->onConnectTimedOut();
      //         removePendingConnection(pending);
      //         pending->deleteObject();
      //         continue;
      //      }
      //      else
      //         sendConnectChallengeRequest(pending);
      //   }
      //   else if(pending->getConnectionState() == NetConnection::AwaitingConnectResponse &&
      //      time > pending->mConnectLastSendTime + ConnectRetryTime)
      //   {
      //      if(pending->mConnectSendCount > ConnectRetryCount)
      //      {
      //         pending->onConnectTimedOut();
      //         removePendingConnection(pending);
      //         pending->deleteObject();
      //         continue;
      //      }
      //      else
      //         sendConnectRequest(pending);
      //   }
      //   i++;
      //}
      mLastTimeoutCheckTime = time;
      NetConnection *walk = NetConnection::getConnectionList();

      while(walk)
      {
         NetConnection *next = walk->getNext();
         if(walk->checkTimeout(time))
         {
            // this baddie timed out
            walk->onTimedOut();
            walk->deleteObject();
         }
         walk = next;
      }
   }
}


ConsoleFunctionGroupBegin(NetInterface, "Global control functions for the netInterfaces.");

ConsoleFunction(allowConnections,void,2,2,"allowConnections(bool);")
{
   argc;
   GNet->setAllowsConnections(dAtob(argv[1]));
}

ConsoleFunctionGroupEnd(NetInterface);

//};//namespace RPGServer

⌨️ 快捷键说明

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