📄 netshakehand.cc
字号:
return; // the existing connection should be kept - the incoming request is stale.
else
connect->deleteObject(); // disconnect this one, and allow the new one to be created.
}
///////////////////////////////////////////////////
//传递connection的类名、连接参数...
char connectionClass[255];
//数据
stream->readString(connectionClass);
ConsoleObject *co = ConsoleObject::create(connectionClass);
NetConnection *conn = dynamic_cast<NetConnection *>(co);
if(!conn || !conn->canRemoteCreate())
{
delete co;
return;
}
conn->registerObject();
conn->setNetAddress(address);
conn->setNetworkConnection(true);
conn->setSequence(connectSequence);
const char *errorString = NULL;
//数据流,连接拒绝处理
if(!conn->readConnectRequest(stream, &errorString))
{
SvrSendConnectReject(/*conn,*/ errorString);
conn->deleteObject();
return;
}
conn->setNetworkConnection(true);
conn->onConnectionEstablished(false);
conn->setEstablished();
conn->setConnectSequence(connectSequence);
SvrSendConnectAccept(/*conn*/);
}
//-----------------------------------------------------------------------------
void NetShakeHand::SvrSendConnectAccept(/*NetConnection *conn*/)
{
AssertFatal(m_pAuthSocket,"需要关联AuthSocket对象");
NetConnection *conn = m_pAuthSocket->GetConnection();
AssertFatal(conn,"需要AuthSocket对象需要关联Connection对象");
BitStream *out = m_pAuthSocket->RequestSendStream(0,FALSE);//BitStream::getPacketStream();
//识别码
out->write(U8(ConnectAccept));
//数据
out->write(conn->getSequence());
//数据接受响应处理
conn->writeConnectAccept(out);
m_pAuthSocket->ProcessSendStream();
//BitStream::sendPacketStream(conn->getNetAddress());
}
//Client
void NetShakeHand::CltHandleConnectAccept(const NetAddress *address, BitStream *stream)
{
U32 connectSequence;
//数据
stream->read(&connectSequence);
NetConnection *conn = m_pAuthSocket->GetConnection();//findPendingConnection(address, connectSequence);
if(!conn || conn->getConnectionState() != NetConnection::AwaitingConnectResponse)
return;
const char *errorString = NULL;
//数据接受响应处理
if(!conn->readConnectAccept(stream, &errorString))
{
conn->handleStartupError(errorString);
//removePendingConnection(conn);
conn->deleteObject();
return;
}
//removePendingConnection(conn); // remove from the pending connection list
conn->setNetworkConnection(true);
conn->onConnectionEstablished(true); // notify the connection that it has been established
conn->setEstablished(); // installs the connection in the connection table, and causes pings/timeouts to happen
conn->setConnectSequence(connectSequence);
}
//Server
void NetShakeHand::SvrSendConnectReject(/*NetConnection *conn,*/ const char *reason)
{
if(!reason)
return; // if the stream is NULL, we reject silently
AssertFatal(m_pAuthSocket,"需要关联AuthSocket对象");
NetConnection *conn = m_pAuthSocket->GetConnection();
AssertFatal(conn,"需要AuthSocket对象需要关联Connection对象");
BitStream *out = m_pAuthSocket->RequestSendStream(0,FALSE);//BitStream::getPacketStream();
//识别码
out->write(U8(ConnectReject));
//数据
out->write(conn->getSequence());
out->writeString(reason);
m_pAuthSocket->ProcessSendStream();
//BitStream::sendPacketStream(conn->getNetAddress());
}
void NetShakeHand::CltHandleConnectReject(const NetAddress *address, BitStream *stream)
{
U32 connectSequence;
//数据
stream->read(&connectSequence);
NetConnection *conn = m_pAuthSocket->GetConnection();//findPendingConnection(address, connectSequence);
if(!conn || (conn->getConnectionState() != NetConnection::AwaitingChallengeResponse &&
conn->getConnectionState() != NetConnection::AwaitingConnectResponse))
return;
//removePendingConnection(conn);
char reason[256];
//数据
stream->readString(reason);
conn->onConnectionRejected(reason);
conn->deleteObject();
}
//Server
void NetShakeHand::SvrSendDisconnectPacket(/*NetConnection *conn,*/ const char *reason)
{
Con::printf("Issuing Disconnect packet.");
AssertFatal(m_pAuthSocket,"需要关联AuthSocket对象");
NetConnection *conn = m_pAuthSocket->GetConnection();
AssertFatal(conn,"需要AuthSocket对象需要关联Connection对象");
BitStream *out = m_pAuthSocket->RequestSendStream(0,FALSE);//BitStream::getPacketStream();
// send a disconnect packet...
U32 connectSequence = conn->getSequence();
//识别码
out->write(U8(Disconnect));
//数据
out->write(connectSequence);
out->writeString(reason);
m_pAuthSocket->ProcessSendStream();
//BitStream::sendPacketStream(conn->getNetAddress());
}
//Client
void NetShakeHand::CltHandleDisconnect(const NetAddress *address, BitStream *stream)
{
NetConnection *conn = m_pAuthSocket->GetConnection();//NetConnection::lookup(address);
if(!conn)
return;
U32 connectSequence;
char reason[256];
//数据
stream->read(&connectSequence);
stream->readString(reason);
if(conn->getSequence() != connectSequence)
return;
conn->onDisconnect(reason);
conn->deleteObject();
}
void NetShakeHand::HandleInfoPacket(const NetAddress *address, U8 packetType, BitStream *stream)
{
}
void NetShakeHand::CltStartConnection(/*NetConnection *conn*/)
{
//addPendingConnection(conn);
AssertFatal(m_pAuthSocket,"需要关联AuthSocket对象");
NetConnection *conn = m_pAuthSocket->GetConnection();
AssertFatal(conn,"需要AuthSocket对象需要关联Connection对象");
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();
// Everything set, so send off the request.
CltSendChallengeRequest(/*conn*/);
}
void NetShakeHand::CheckTimeouts()
{
Parent::CheckTimeouts();
// U32 time = Platform::getVirtualMilliseconds();
// if(time < mLastTimeoutCheckTime + TimeoutCheckInterval)
// return;
//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
// CltSendChallengeRequest(pending);
// }
// else if(pending->getConnectionState() == NetConnection::AwaitingConnectResponse &&
// time > pending->mConnectLastSendTime + ConnectRetryTime)
// {
// if(pending->mConnectSendCount > ConnectRetryCount)
// {
// pending->onConnectTimedOut();
// removePendingConnection(pending);
// pending->deleteObject();
// continue;
// }
// else
// CltSendConnectRequest(pending);
// }
// i++;
//}
}
//};//namespace RPGServer
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -