mirror of
https://github.com/danbulant/Cosmos
synced 2026-06-03 06:40:23 +00:00
This commit is contained in:
parent
dee8a56ddc
commit
8fb7b9f262
4 changed files with 35 additions and 15 deletions
|
|
@ -49,6 +49,7 @@
|
||||||
<Compile Include="Boot.cs" />
|
<Compile Include="Boot.cs" />
|
||||||
<Compile Include="Deboot.cs" />
|
<Compile Include="Deboot.cs" />
|
||||||
<Compile Include="Global.cs" />
|
<Compile Include="Global.cs" />
|
||||||
|
<Compile Include="Network\IP4Packet.cs" />
|
||||||
<Compile Include="Network\Packet.cs" />
|
<Compile Include="Network\Packet.cs" />
|
||||||
<Compile Include="Network\UDPPacket.cs" />
|
<Compile Include="Network\UDPPacket.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
|
|
||||||
16
source/Cosmos/Cosmos.System/Network/IP4Packet.cs
Normal file
16
source/Cosmos/Cosmos.System/Network/IP4Packet.cs
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace Cosmos.Sys.Network {
|
||||||
|
public abstract class IP4Packet : Packet {
|
||||||
|
protected int mHeaderSize = 20;
|
||||||
|
protected int mHeaderBegin;
|
||||||
|
|
||||||
|
protected override int Initialize(byte[] aData, int aHeaderSize) {
|
||||||
|
mHeaderBegin = base.Initialize(aData, mHeaderSize + aHeaderSize);
|
||||||
|
return mHeaderBegin;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -11,16 +11,17 @@ namespace Cosmos.Sys.Network {
|
||||||
// user that it is not a direct access, but incurs a performance
|
// user that it is not a direct access, but incurs a performance
|
||||||
// penalty to call
|
// penalty to call
|
||||||
public byte[] GetData() {
|
public byte[] GetData() {
|
||||||
Finalize();
|
Conclude();
|
||||||
return mData;
|
return mData;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void Initialize(byte[] aData, int aHeaderSize) {
|
protected virtual int Initialize(byte[] aData, int aHeaderSize) {
|
||||||
mData = new byte[aData.Length + 8];
|
mData = new byte[aData.Length + 8];
|
||||||
aData.CopyTo(mData, 8);
|
aData.CopyTo(mData, 8);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void Finalize() {
|
protected virtual void Conclude() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,26 +5,28 @@ using System.Text;
|
||||||
|
|
||||||
namespace Cosmos.Sys.Network {
|
namespace Cosmos.Sys.Network {
|
||||||
// http://en.wikipedia.org/wiki/User_Datagram_Protocol
|
// http://en.wikipedia.org/wiki/User_Datagram_Protocol
|
||||||
public class UDPPacket : Packet {
|
public class UDPPacket : IP4Packet {
|
||||||
public UDPPacket(int aSrcPort, int aDestPort, byte[] aData) {
|
public UDPPacket(int aSrcPort, int aDestPort, byte[] aData) {
|
||||||
Initialize(aData, 8);
|
mHeaderBegin = Initialize(aData, 8);
|
||||||
// Source Port
|
// Source Port
|
||||||
mData[0] = (byte)(aSrcPort >> 8);
|
mData[mHeaderBegin] = (byte)(aSrcPort >> 8);
|
||||||
mData[1] = (byte)(aSrcPort & 0xFF);
|
mData[mHeaderBegin + 1] = (byte)(aSrcPort & 0xFF);
|
||||||
// Destination Port
|
// Destination Port
|
||||||
mData[2] = (byte)(aDestPort >> 8);
|
mData[mHeaderBegin + 2] = (byte)(aDestPort >> 8);
|
||||||
mData[3] = (byte)(aDestPort & 0xFF);
|
mData[mHeaderBegin + 3] = (byte)(aDestPort & 0xFF);
|
||||||
// Length
|
// Length
|
||||||
mData[4] = (byte)(mData.Length >> 8);
|
mData[mHeaderBegin + 4] = (byte)(mData.Length >> 8);
|
||||||
mData[5] = (byte)(mData.Length & 0xFF);
|
mData[mHeaderBegin + 5] = (byte)(mData.Length & 0xFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void Finalize() {
|
protected new int mHeaderBegin = 0;
|
||||||
base.Finalize();
|
|
||||||
|
protected override void Conclude() {
|
||||||
|
base.Conclude();
|
||||||
// Checksum
|
// Checksum
|
||||||
//TODO: Uses info from IPHeader to create check sum as well
|
//TODO: Uses info from IPHeader to create check sum as well
|
||||||
mData[6] = 0;
|
mData[mHeaderBegin + 6] = 0;
|
||||||
mData[7] = 0;
|
mData[mHeaderBegin + 7] = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue