JD2022-TU1/main/extern/Camcam/LIBS/Net/NET_HTable.cpp

183 lines
4 KiB
C++

#include <windows.h>
#include <stdio.h>
#include "NET.h"
u32 NET_GetBitNumber(u32 n)
{
register unsigned int tmpA;
tmpA = (n & 0x55555555) + ((n>>1) & 0x55555555);
tmpA = (tmpA & 0x33333333) + ((tmpA>>2) & 0x33333333);
tmpA = (tmpA & 0x0f0f0f0f) + ((tmpA>>4) & 0x0f0f0f0f);
tmpA = (tmpA & 0x00ff00ff) + ((tmpA>>8) & 0x00ff00ff);
tmpA = (tmpA & 0x0000ffff) + ((tmpA>>16) & 0x0000ffff);
return tmpA;
}//*/
int NET_GetLogUpperPO2(float Value)
{
u32 Local;
*(float *)&Local = Value;
Local &= 0x7fffffff;
return (((Local-1) >> 23) - 126);
}
int NET_GetUpperPO2(float Value)
{
return 1<<NET_GetLogUpperPO2(Value);
}
int NET_GetFirstBit(u32 Mask)
{
u32 Index;
if (!Mask) return -1;
Index = 0;
if ((Mask & 0xffff) == 0) {Mask >>= 16;Index+=16;};
if ((Mask & 0xff) == 0) {Mask >>= 8;Index+=8;};
if ((Mask & 0xf) == 0) {Mask >>= 4;Index+=4;};
if ((Mask & 0x3) == 0) {Mask >>= 2;Index+=2;};
if ((Mask & 0x1) == 0) {Mask >>= 1;Index+=1;};
return Index;
}
void OneBuf::SetIP(u32 IP , u16 Port )
{
Sender.sin_family = AF_INET;
Sender.sin_port = htons( Port );
Sender.sin_addr.s_addr = htonl( IP );
}
void OneBuf::SetIP(u32 IP )
{
Sender.sin_family = AF_INET;
Sender.sin_addr.s_addr = htonl( IP );
}
void OneBuf::SetBufPort(u32 Port )
{
Sender.sin_family = AF_INET;
Sender.sin_port = htons( Port );
}
u32 OneBuf::GetIP()
{
return ( ntohl( Sender.sin_addr.s_addr ) ) ;
}
u16 OneBuf::GetPort()
{
return ( ntohs( Sender.sin_port ) ) ;
}
u32 NET_IO::IP_TO_H(u32 IP,u16 Port)
{
return ((IP & 0xff) + ((IP>>8) & 0xff) + ((IP>>16) & 0xff) + ((IP>>24) & 0xff) + ((Port>>0)&0xff) + ((Port>>8)&0xff)) & 0xff;
};
void NET_IO::BuildHTable()
{
memset(HTable,0,sizeof(HTable));
u32 Counter;
Counter = 32;
ValidMask = 0;
while (Counter--)
{
if (LinkedIP[Counter].ulIP)
{
ValidMask |= 1<<Counter;
HTable[IP_TO_H(LinkedIP[Counter].ulIP,LinkedIP[Counter].Port)] |= 1 << Counter;
}
}
};
u32 NET_IO::GetIndexFromIP(u32 ulIP,u16 Port)
{
u32 Mask,BN;
Mask = HTable[IP_TO_H(ulIP,Port)];
BN = NET_GetBitNumber(Mask);
if (BN == 0) return -1;
if (BN == 1)
{
return NET_GetLogUpperPO2((float)Mask);
}
u32 Index = 0;
while (Mask)
{
if ((Mask & 0xffff) == 0) {Mask >>= 16;Index+=16;};
if ((Mask & 0xff) == 0) {Mask >>= 8;Index+=8;};
if ((Mask & 0xf) == 0) {Mask >>= 4;Index+=4;};
if ((Mask & 0x3) == 0) {Mask >>= 2;Index+=2;};
if ((Mask & 0x1) == 0) {Mask >>= 1;Index+=1;};
if ((ulIP == LinkedIP[Index].ulIP)&&(Port == LinkedIP[Index].Port)) return Index;
}
return -1;
}
void NET_IO::AddToStat(OneBuf *ToSet)
{
u32 Index;
Index = GetIndexFromIP(ToSet->GetIP(),ToSet->GetPort());
if ((Index == -1) && (NET_GetBitNumber(ValidMask) != 32))
{
/* Auto link */
LinkToIP(ToSet->GetIP(),ToSet->GetPort());
Index = GetIndexFromIP(ToSet->GetIP(),ToSet->GetPort());
}
if (Index != -1)
{
/* if (LinkedIP[Index].ulIP != ToSet->GetIP())
{
*(u32 *)0 = *(u32 *)4;
}*/
LinkedIP[Index].AddPacket(ToSet,this,Owner);
LinkedIP[Index].LastConnectionTime = timeGetTime();
} else
{
/* The sender is not in the group */
/* Thrash it */
InputData.THREAD_STACK.Add(ToSet);
}
}
u32 NET_IO::LinkToIP(u32 IP,u16 Port)
{
if (IsLinked(IP,Port)) return GetIndexFromIP(IP,Port);
if (ValidMask == -1) return -1;
u32 Index = NET_GetFirstBit(ValidMask ^ -1);
LinkedIP[Index].Reset(this);
LinkedIP[Index].ulIP = IP;
LinkedIP[Index].Port = Port;
LinkedIP[Index].GoodPackets = 1;
BuildHTable();
OneBuf ToSend;
*((u32 *)ToSend.BufDATA) = 'LINK';
ToSend.SetIP(LinkedIP[Index].ulIP,MyPort);
memcpy(&ToSend.BufDATA[4],MyComputerName,256);
ToSend.Size = 4 + 256;
NET_DataSend(&ToSend);
NET_DataSend(&ToSend);
NET_DataSend(&ToSend);
NET_DataSend(&ToSend);
return 0;
}
void NET_IO::UnLinkToIP(u32 IP,u16 Port)
{
u32 Index;
Index = GetIndexFromIP(IP,Port);
if (Index == -1) return;
/* this->mo
ConnectedMask &= ~(1<<Index);*/
if (WAIT_OBJECT_0 == WaitForSingleObject(InputData.AccesMutex, INFINITE))
{
/* Release all packets */
LinkedIP[Index].Reset(this);
ReleaseMutex(InputData.AccesMutex);
}
return;
}