DEV9: Move NetLib functions into header and mark as inline

This commit is contained in:
TheLastRar 2023-01-16 21:38:06 +00:00 committed by refractionpcsx2
parent a85a2a3cc5
commit 06ef51db2e
5 changed files with 70 additions and 115 deletions

View File

@ -339,7 +339,6 @@ set(pcsx2DEV9Sources
DEV9/PacketReader/IP/IP_Options.cpp
DEV9/PacketReader/IP/IP_Packet.cpp
DEV9/PacketReader/EthernetFrame.cpp
DEV9/PacketReader/NetLib.cpp
DEV9/Sessions/BaseSession.cpp
DEV9/Sessions/ICMP_Session/ICMP_Session.cpp
DEV9/Sessions/TCP_Session/TCP_Session.cpp

View File

@ -1,98 +0,0 @@
/* PCSX2 - PS2 Emulator for PCs
* Copyright (C) 2002-2021 PCSX2 Dev Team
*
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with PCSX2.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "PrecompiledHeader.h"
#include "NetLib.h"
#ifdef _WIN32
#include "winsock.h"
#else
#include <arpa/inet.h>
#endif
using namespace PacketReader;
using namespace PacketReader::IP;
namespace PacketReader::NetLib
{
void WriteByte08(u8* data, int* index, u8 value)
{
data[*index] = value;
*index += sizeof(u8);
}
void WriteUInt16(u8* data, int* index, u16 value)
{
*(u16*)&data[*index] = htons(value);
*index += sizeof(u16);
}
void WriteUInt32(u8* data, int* index, u32 value)
{
*(u32*)&data[*index] = htonl(value);
*index += sizeof(u32);
}
//Special
void WriteMACAddress(u8* data, int* index, MAC_Address value)
{
*(MAC_Address*)&data[*index] = value;
*index += sizeof(MAC_Address);
}
void WriteIPAddress(u8* data, int* index, IP_Address value)
{
*(IP_Address*)&data[*index] = value;
*index += sizeof(IP_Address);
}
void WriteByteArray(u8* data, int* index, int length, u8* value)
{
memcpy(&data[*index], value, length);
*index += length;
}
//Read
void ReadByte08(u8* data, int* index, u8* value)
{
*value = data[*index];
*index += sizeof(u8);
}
void ReadUInt16(u8* data, int* index, u16* value)
{
*value = ntohs(*(u16*)&data[*index]);
*index += sizeof(u16);
}
void ReadUInt32(u8* data, int* index, u32* value)
{
*value = ntohl(*(u32*)&data[*index]);
*index += sizeof(u32);
}
//Special
void ReadMACAddress(u8* data, int* index, MAC_Address* value)
{
*value = *(MAC_Address*)&data[*index];
*index += sizeof(MAC_Address);
}
void ReadIPAddress(u8* data, int* index, IP_Address* value)
{
*value = *(IP_Address*)&data[*index];
*index += sizeof(IP_Address);
}
void ReadByteArray(u8* data, int* index, int length, u8* value)
{
memcpy(value, &data[*index], length);
*index += length;
}
} // namespace PacketReader::NetLib

View File

@ -18,21 +18,79 @@
#include "DEV9/PacketReader/MAC_Address.h"
#include "DEV9/PacketReader/IP/IP_Address.h"
#ifdef _WIN32
#include "winsock.h"
#else
#include <arpa/inet.h>
#endif
namespace PacketReader::NetLib
{
void WriteByte08(u8* data, int* index, u8 value);
void WriteUInt16(u8* data, int* index, u16 value);
void WriteUInt32(u8* data, int* index, u32 value);
// Write.
inline void WriteByte08(u8* data, int* index, u8 value)
{
data[*index] = value;
*index += sizeof(u8);
}
inline void WriteUInt16(u8* data, int* index, u16 value)
{
*(u16*)&data[*index] = htons(value);
*index += sizeof(u16);
}
inline void WriteUInt32(u8* data, int* index, u32 value)
{
*(u32*)&data[*index] = htonl(value);
*index += sizeof(u32);
}
void WriteMACAddress(u8* data, int* index, PacketReader::MAC_Address value);
void WriteIPAddress(u8* data, int* index, PacketReader::IP::IP_Address value);
void WriteByteArray(u8* data, int* index, int length, u8* value);
// Special write.
inline void WriteMACAddress(u8* data, int* index, PacketReader::MAC_Address value)
{
*(PacketReader::MAC_Address*)&data[*index] = value;
*index += sizeof(PacketReader::MAC_Address);
}
inline void WriteIPAddress(u8* data, int* index, PacketReader::IP::IP_Address value)
{
*(PacketReader::IP::IP_Address*)&data[*index] = value;
*index += sizeof(PacketReader::IP::IP_Address);
}
inline void WriteByteArray(u8* data, int* index, int length, u8* value)
{
memcpy(&data[*index], value, length);
*index += length;
}
void ReadByte08(u8* data, int* index, u8* value);
void ReadUInt16(u8* data, int* index, u16* value);
void ReadUInt32(u8* data, int* index, u32* value);
// Read.
inline void ReadByte08(u8* data, int* index, u8* value)
{
*value = data[*index];
*index += sizeof(u8);
}
inline void ReadUInt16(u8* data, int* index, u16* value)
{
*value = ntohs(*(u16*)&data[*index]);
*index += sizeof(u16);
}
inline void ReadUInt32(u8* data, int* index, u32* value)
{
*value = ntohl(*(u32*)&data[*index]);
*index += sizeof(u32);
}
void ReadMACAddress(u8* data, int* index, PacketReader::MAC_Address* value);
void ReadIPAddress(u8* data, int* index, PacketReader::IP::IP_Address* value);
void ReadByteArray(u8* data, int* index, int length, u8* value);
// Special read.
inline void ReadMACAddress(u8* data, int* index, PacketReader::MAC_Address* value)
{
*value = *(PacketReader::MAC_Address*)&data[*index];
*index += sizeof(PacketReader::MAC_Address);
}
inline void ReadIPAddress(u8* data, int* index, PacketReader::IP::IP_Address* value)
{
*value = *(PacketReader::IP::IP_Address*)&data[*index];
*index += sizeof(PacketReader::IP::IP_Address);
}
inline void ReadByteArray(u8* data, int* index, int length, u8* value)
{
memcpy(value, &data[*index], length);
*index += length;
}
} // namespace PacketReader::NetLib

View File

@ -177,7 +177,6 @@
<ClCompile Include="DEV9\PacketReader\IP\UDP\UDP_Packet.cpp" />
<ClCompile Include="DEV9\PacketReader\IP\IP_Options.cpp" />
<ClCompile Include="DEV9\PacketReader\IP\IP_Packet.cpp" />
<ClCompile Include="DEV9\PacketReader\NetLib.cpp" />
<ClCompile Include="DEV9\pcap_io.cpp" />
<ClCompile Include="DEV9\Sessions\ICMP_Session\ICMP_Session.cpp" />
<ClCompile Include="DEV9\Sessions\TCP_Session\TCP_Session.cpp" />

View File

@ -941,9 +941,6 @@
<ClCompile Include="DEV9\PacketReader\EthernetFrame.cpp">
<Filter>System\Ps2\DEV9\PacketReader</Filter>
</ClCompile>
<ClCompile Include="DEV9\PacketReader\NetLib.cpp">
<Filter>System\Ps2\DEV9\PacketReader</Filter>
</ClCompile>
<ClCompile Include="DEV9\pcap_io.cpp">
<Filter>System\Ps2\DEV9</Filter>
</ClCompile>