-
Notifications
You must be signed in to change notification settings - Fork 331
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First iteration improved multiplatform support
- Loading branch information
Michael Lidgren
committed
Mar 24, 2015
1 parent
8033a73
commit fa8b1e9
Showing
25 changed files
with
674 additions
and
401 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System; | ||
using System.IO; | ||
using System.Security.Cryptography; | ||
|
||
namespace Lidgren.Network | ||
{ | ||
public abstract class NetCryptoProviderEncryption : NetEncryption | ||
{ | ||
public NetCryptoProviderEncryption(NetPeer peer) | ||
: base(peer) | ||
{ | ||
} | ||
|
||
protected abstract CryptoStream GetEncryptStream(MemoryStream ms); | ||
|
||
protected abstract CryptoStream GetDecryptStream(MemoryStream ms); | ||
|
||
public override bool Encrypt(NetOutgoingMessage msg) | ||
{ | ||
int unEncLenBits = msg.LengthBits; | ||
|
||
var ms = new MemoryStream(); | ||
var cs = GetEncryptStream(ms); | ||
cs.Write(msg.m_data, 0, msg.LengthBytes); | ||
cs.Close(); | ||
|
||
// get results | ||
var arr = ms.ToArray(); | ||
ms.Close(); | ||
|
||
msg.EnsureBufferSize((arr.Length + 4) * 8); | ||
msg.LengthBits = 0; // reset write pointer | ||
msg.Write((uint)unEncLenBits); | ||
msg.Write(arr); | ||
msg.LengthBits = (arr.Length + 4) * 8; | ||
|
||
return true; | ||
} | ||
|
||
public override bool Decrypt(NetIncomingMessage msg) | ||
{ | ||
int unEncLenBits = (int)msg.ReadUInt32(); | ||
|
||
var ms = new MemoryStream(msg.m_data, 4, msg.LengthBytes - 4); | ||
var cs = GetDecryptStream(ms); | ||
|
||
var result = m_peer.GetStorage(unEncLenBits); | ||
cs.Read(result, 0, NetUtility.BytesToHoldBits(unEncLenBits)); | ||
cs.Close(); | ||
|
||
// TODO: recycle existing msg | ||
|
||
msg.m_data = result; | ||
msg.m_bitLength = unEncLenBits; | ||
|
||
return true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.