WONStatus PeerAuthServer::HandleRequest(ReadBuffer &theRequest, ByteBufferPtr &theChallenge)
{
	unsigned char authMode = theRequest.ReadByte();
	unsigned char encryptType = theRequest.ReadByte();
	unsigned short encryptFlags = theRequest.ReadShort();

	if(authMode!=1)
		return WS_PeerAuthServer_InvalidAuthMode;

	if(encryptType!=0 && encryptType!=1)
		return WS_PeerAuthServer_InvalidEncryptType;

	bool encrypted = encryptType==1;
	
	mAuthType = encrypted?AUTH_TYPE_PERSISTENT:AUTH_TYPE_PERSISTENT_NOCRYPT;

	unsigned short aLen = theRequest.ReadShort();
	if(mUseAuth2)
		mClientCertificate = new Auth2Certificate(theRequest.ReadBytes(aLen),aLen);
	else
		mClientCertificate = new AuthCertificate(theRequest.ReadBytes(aLen),aLen);
	
	if(!mClientCertificate->IsValid())
		return WS_PeerAuthServer_InvalidClientCertificate;
	else if(mClientCertificate->IsExpired(mPeerData->GetAuthDelta()))
		return WS_PeerAuthServer_ExpiredClientCertificate;
	else if(!mPeerData->Verify(mClientCertificate.get()))
		return WS_PeerAuthServer_FailedToVerifyClientCertificate;

	return GetChallenge1(theChallenge);
}
void StagingLogic::HandleGameMessage(ReadBuffer &theMsg, LobbyClient *theSender)
{
	if(mGame.get()==NULL || theSender==NULL)
		return;		
	try
	{
		unsigned char aMsgType = theMsg.ReadByte();
		if(mJoinGameReply==LobbyGameStatus_None && aMsgType!=LobbyGameMsg_JoinReply) // not in game yet
			return;

		switch(aMsgType)
		{
			case LobbyGameMsg_JoinRequest: HandleJoinGameRequest(theMsg,theSender); break;
			case LobbyGameMsg_JoinReply: HandleJoinGameReply(theMsg,theSender); break;
			case LobbyGameMsg_PlayerJoined: HandlePlayerJoined(theMsg,theSender); break;
			case LobbyGameMsg_ReadyRequest: HandleReadyRequest(theMsg,theSender); break;
			case LobbyGameMsg_PlayerReady: HandlePlayerReady(theMsg,theSender); break;
			case LobbyGameMsg_DissolveGame: HandleDissolveGame(theMsg,theSender); break;
			case LobbyGameMsg_StartGame: HandleStartGameMsg(theMsg,theSender); break;
			case LobbyGameMsg_PingChangedRequest: HandlePingChangedRequest(theMsg,theSender); break;
			case LobbyGameMsg_PingChanged: HandlePingChanged(theMsg,theSender); break;
			case LobbyGameMsg_ClientKicked: HandleClientKicked(theMsg,theSender); break;

			default:
				mGame->HandleGameMessage(aMsgType,theMsg,theSender);
		}
	}
	catch(ReadBufferException&)
	{
	}
}
bool Auth2Certificate::UnpackHook(ReadBuffer &theData)
{
	if(!AuthBase::UnpackHook(theData))
		return false;

	unsigned short aDataCount = theData.ReadShort();
	for(int i=0; i<aDataCount; i++)
	{
		unsigned short aTypeId = theData.ReadShort();
		unsigned short aDataLen = theData.ReadShort();
		int aPos = theData.pos();
		switch(aTypeId)
		{

			case 0:  // standard login data
			{
				mUserId = theData.ReadLong();
				unsigned short aKeyLen = theData.ReadShort();
				if(!mPubKey.SetPublicKey(theData.ReadBytes(aKeyLen),aKeyLen))
					return false;

				theData.ReadWString(mUserName);
				unsigned char aCommunityCount = theData.ReadByte();
				for(int i=0; i<aCommunityCount; i++)
					mCommunityTrustMap[theData.ReadLong()] = theData.ReadShort();
 
			}
			break;

			case 1: // user data
			{
				DWORD aCommunityId = theData.ReadLong();
				mUserDataMap[aCommunityId] = theData.ReadBuf(2);
			}
			break;

			case 2:	// nickname
			{
				wstring aKey, aVal;
				theData.ReadWString(aKey);
				theData.ReadWString(aVal);
				mNicknameMap[aKey] = aVal;
			}
			break;

			case 3: // KeyId data block
			{
				DWORD aCommunityId = theData.ReadLong();
				DWORD aKeyId = theData.ReadLong();
				mKeyIdMap[aCommunityId] = aKeyId;
			}
			break;
		}

		theData.ReadBytes(aDataLen - (theData.pos() - aPos));
	}

	return true;
}
bool LobbyGame::ReadSummary(ReadBuffer &theMsg)
{
	try
	{
		if(mGameType==LobbyGameType_Internet)
			mIPAddr.SetSixByte(theMsg.ReadBytes(6));
		else
		{
			unsigned short aLanProductId = theMsg.ReadShort();
			if(aLanProductId!=LobbyMisc::GetLanProductId())
				return false;
		}
			
		mInProgress = theMsg.ReadBool();
		if(mGameType!=LobbyGameType_Internet)
		{
			std::wstring aName;
			theMsg.ReadWString(aName);
			mName = aName;
		}

		mSkillLevel = (LobbySkillLevel)theMsg.ReadByte();
		if(mSkillLevel<LobbySkillLevel_None || mSkillLevel>=LobbySkillLevel_Max)
			mSkillLevel = LobbySkillLevel_None;

		if(mGameType!=LobbyGameType_Internet)
		{
			unsigned char aProtectionFlags = theMsg.ReadByte();
			mHasPassword = (aProtectionFlags & 0x01)?true:false;
			mInviteOnly = (aProtectionFlags & 0x02)?true:false;
			mAskToJoin = (aProtectionFlags & 0x04)?true:false;
		}

		mNumPlayers = theMsg.ReadShort();
		mMaxPlayers = theMsg.ReadShort();

		return ReadSummaryHook(theMsg);
	}
	catch(ReadBufferException&)
	{
	}
	return false;
}
WONStatus RoutingGetMembersOfGroupOp::HandleReply(unsigned char theMsgType, ReadBuffer &theMsg)
{
	if(theMsgType!=RoutingGetMembersOfGroupReply)
		return WS_RoutingOp_DontWantReply;

	WONStatus aStatus = (WONStatus)theMsg.ReadShort();
	unsigned short aGroupId = theMsg.ReadShort();
	if(aGroupId!=mGroupId)
		return WS_RoutingOp_DontWantReply;

	if(aStatus==WS_Success)
	{
		unsigned char aFlags = theMsg.ReadByte();
		mHasClientNames = ((aFlags & RoutingGroupAsyncFlag_DistributeClientName) != 0);
		mHasClientFlags = ((aFlags & RoutingGroupAsyncFlag_DistributeClientFlags) != 0);
		mObserverCount = theMsg.ReadShort();
		unsigned short aNumMembers = theMsg.ReadShort();
		for(int j=0; j<aNumMembers; j++)
		{
			unsigned short aLen = theMsg.ReadShort();
			unsigned long aBeginPos = theMsg.pos();

			RoutingMemberInfoPtr aMemberInfo = GetNewMemberInfo();
			aMemberInfo->mClientId = theMsg.ReadShort();
			aMemberInfo->mFlags = theMsg.ReadByte();
			mMemberMap[aMemberInfo->mClientId] = aMemberInfo;

			if(aFlags!=0)
				aMemberInfo->mClientInfo = GetNewClientInfo();

			if(mHasClientNames)  
				theMsg.ReadWString(aMemberInfo->mClientInfo->mName);

			if(mHasClientFlags)
				aMemberInfo->mClientInfo->mFlags = theMsg.ReadLong();

			theMsg.ReadBytes(aLen - (theMsg.pos() - aBeginPos));
		}			
	}

	return aStatus;
}
WONStatus RoutingGroupJoinAttemptOp::HandleReply(unsigned char theMsgType, ReadBuffer &theMsg)
{
	if(theMsgType!=RoutingGroupJoinAttempt)
		return WS_RoutingOp_DontWantReply;

	mClientId = theMsg.ReadShort();
	mGroupId = theMsg.ReadShort();
	theMsg.ReadWString(mComment);
	mJoinGroupFlags = theMsg.ReadByte();

	return WS_Success;
}
void RoutingGetGroupListOp::ParseReplyExceptForStatus(ReadBuffer &theMsg)
{
	mGroupMap.clear();

	mFlags = theMsg.ReadShort();
	unsigned short aNumGroups = theMsg.ReadShort();
	for(int i=0; i<aNumGroups; i++)
	{
		unsigned short aLen = theMsg.ReadShort();
		unsigned long aBeginPos = theMsg.pos();

		RoutingGroupInfoPtr anInfo = GetNewGroupInfo();
		anInfo->mId = theMsg.ReadShort();
		if (HasGroupName())
			theMsg.ReadWString(anInfo->mName);
		if (HasCaptainId())
			anInfo->mCaptainId = theMsg.ReadShort();
		if (HasMaxPlayers())
			anInfo->mMaxPlayers = theMsg.ReadShort();
		if (HasGroupFlags())
			anInfo->mFlags = theMsg.ReadLong();
		if (HasAsyncFlags())
			anInfo->mAsyncFlags = theMsg.ReadLong();
		if (HasObserverCount())
			anInfo->mObserverCount = theMsg.ReadShort();
		if (HasMemberCount())
			anInfo->mMemberCount = theMsg.ReadShort();
	
		if (HasMembers())
		{
			for(int j=0; j<anInfo->mMemberCount; j++)
			{
				RoutingMemberInfoPtr aMemberInfo = GetNewMemberInfo();
				aMemberInfo->mClientId = theMsg.ReadShort();
				aMemberInfo->mFlags = theMsg.ReadByte();
				anInfo->mMemberMap[aMemberInfo->mClientId] = aMemberInfo;
			}
		}

	//	if (theMsg.HasMoreBytes())
	//		anInfo->mParentId = theMsg.ReadShort();

		mGroupMap[anInfo->mId] = anInfo;
		
		theMsg.ReadBytes(aLen - (theMsg.pos() - aBeginPos));
	}

	if(HasClientCount() && theMsg.HasMoreBytes())
		mClientCount = theMsg.ReadShort();

}