|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Security.Cryptography; |
| 5 | +using UnityEngine.Networking; |
| 6 | + |
| 7 | +namespace MLAPI |
| 8 | +{ |
| 9 | + public class NetworkingConfiguration |
| 10 | + { |
| 11 | + public ushort ProtocolVersion = 0; |
| 12 | + public Dictionary<string, QosType> Channels = new Dictionary<string, QosType>(); |
| 13 | + public List<string> MessageTypes = new List<string>(); |
| 14 | + public int MessageBufferSize = 65536; |
| 15 | + public int MaxMessagesPerFrame = 150; |
| 16 | + public int MaxConnections = 100; |
| 17 | + public int Port = 7777; |
| 18 | + public string Address; |
| 19 | + public int ClientConnectionBufferTimeout = 10; |
| 20 | + public bool ConnectionApproval = false; |
| 21 | + public Action<byte[], int, Action<int, bool>> ConnectionApprovalCallback; |
| 22 | + public byte[] ConnectionData; |
| 23 | + public bool HandleObjectSpawning = true; |
| 24 | + //TODO |
| 25 | + public bool CompressMessages = false; |
| 26 | + //Should only be used for dedicated servers and will require the servers RSA keypair being hard coded into clients in order to exchange a AES key |
| 27 | + //TODO |
| 28 | + public bool EncryptMessages = false; |
| 29 | + |
| 30 | + |
| 31 | + //Cached config hash |
| 32 | + private byte[] ConfigHash = null; |
| 33 | + public byte[] GetConfig(bool cache = true) |
| 34 | + { |
| 35 | + if (ConfigHash != null && cache) |
| 36 | + return ConfigHash; |
| 37 | + |
| 38 | + using(MemoryStream writeStream = new MemoryStream()) |
| 39 | + { |
| 40 | + using(BinaryWriter writer = new BinaryWriter(writeStream)) |
| 41 | + { |
| 42 | + writer.Write(ProtocolVersion); |
| 43 | + foreach (KeyValuePair<string, QosType> pair in Channels) |
| 44 | + { |
| 45 | + writer.Write(pair.Key); |
| 46 | + writer.Write((int)pair.Value); |
| 47 | + } |
| 48 | + for (int i = 0; i < MessageTypes.Count; i++) |
| 49 | + { |
| 50 | + writer.Write(MessageTypes[i]); |
| 51 | + } |
| 52 | + writer.Write(HandleObjectSpawning); |
| 53 | + writer.Write(CompressMessages); |
| 54 | + writer.Write(EncryptMessages); |
| 55 | + } |
| 56 | + using(SHA256Managed sha256 = new SHA256Managed()) |
| 57 | + { |
| 58 | + //Returns a 256 bit / 32 byte long checksum of the config |
| 59 | + if (cache) |
| 60 | + { |
| 61 | + ConfigHash = sha256.ComputeHash(writeStream); |
| 62 | + return ConfigHash; |
| 63 | + } |
| 64 | + return sha256.ComputeHash(writeStream); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public bool CompareConfig(byte[] hash) |
| 70 | + { |
| 71 | + return hash == GetConfig(); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments