|
10 | 10 | using MLAPI.NetworkingManagerComponents.Cryptography;
|
11 | 11 | using MLAPI.NetworkingManagerComponents.Core;
|
12 | 12 | using UnityEngine.SceneManagement;
|
| 13 | +using MLAPI.NetworkingManagerComponents.Binary; |
13 | 14 |
|
14 | 15 | namespace MLAPI.MonoBehaviours.Core
|
15 | 16 | {
|
@@ -821,200 +822,198 @@ private IEnumerator ApprovalTimeout(uint clientId)
|
821 | 822 |
|
822 | 823 | private void HandleIncomingData(uint clientId, byte[] data, int channelId)
|
823 | 824 | {
|
824 |
| - using(MemoryStream readStream = new MemoryStream(data)) |
| 825 | + BitReader reader = new BitReader(data); |
| 826 | + |
| 827 | + ushort messageType = reader.ReadUShort(); |
| 828 | + bool targeted = reader.ReadBool(); |
| 829 | + uint targetNetworkId = 0; |
| 830 | + ushort networkOrderId = 0; |
| 831 | + if (targeted) |
825 | 832 | {
|
826 |
| - using (BinaryReader reader = new BinaryReader(readStream)) |
827 |
| - { |
828 |
| - ushort messageType = reader.ReadUInt16(); |
829 |
| - bool targeted = reader.ReadBoolean(); |
830 |
| - uint targetNetworkId = 0; |
831 |
| - ushort networkOrderId = 0; |
832 |
| - if(targeted) |
833 |
| - { |
834 |
| - targetNetworkId = reader.ReadUInt32(); |
835 |
| - networkOrderId = reader.ReadUInt16(); |
836 |
| - } |
837 |
| - bool isPassthrough = reader.ReadBoolean(); |
| 833 | + targetNetworkId = reader.ReadUInt(); |
| 834 | + networkOrderId = reader.ReadUShort(); |
| 835 | + } |
| 836 | + bool isPassthrough = reader.ReadBool(); |
838 | 837 |
|
839 |
| - uint passthroughOrigin = 0; |
840 |
| - uint passthroughTarget = 0; |
| 838 | + uint passthroughOrigin = 0; |
| 839 | + uint passthroughTarget = 0; |
841 | 840 |
|
842 |
| - if (isPassthrough && isServer) |
843 |
| - passthroughTarget = reader.ReadUInt32(); |
844 |
| - else if (isPassthrough && !isServer) |
845 |
| - passthroughOrigin = reader.ReadUInt32(); |
| 841 | + if (isPassthrough && isServer) |
| 842 | + passthroughTarget = reader.ReadUInt(); |
| 843 | + else if (isPassthrough && !isServer) |
| 844 | + passthroughOrigin = reader.ReadUInt(); |
846 | 845 |
|
847 | 846 |
|
848 |
| - //Client tried to send a network message that was not the connection request before he was accepted. |
849 |
| - if (isServer && pendingClients.Contains(clientId) && messageType != 0) |
850 |
| - { |
851 |
| - Debug.LogWarning("MLAPI: Message recieved from clientId " + clientId + " before it has been accepted"); |
852 |
| - return; |
853 |
| - } |
| 847 | + //Client tried to send a network message that was not the connection request before he was accepted. |
| 848 | + if (isServer && pendingClients.Contains(clientId) && messageType != 0) |
| 849 | + { |
| 850 | + Debug.LogWarning("MLAPI: Message recieved from clientId " + clientId + " before it has been accepted"); |
| 851 | + return; |
| 852 | + } |
| 853 | + |
| 854 | + |
| 855 | + //ushort bytesToRead = reader.ReadUShort(); |
| 856 | + reader.SkipPadded(); |
| 857 | + |
| 858 | + byte[] incommingData = reader.ReadByteArray(); |
| 859 | + if (NetworkConfig.EncryptedChannelsHashSet.Contains(MessageManager.reverseChannels[channelId])) |
| 860 | + { |
| 861 | + //Encrypted message |
| 862 | + if (isServer) |
| 863 | + incommingData = CryptographyHelper.Decrypt(incommingData, connectedClients[clientId].AesKey); |
| 864 | + else |
| 865 | + incommingData = CryptographyHelper.Decrypt(incommingData, clientAesKey); |
| 866 | + } |
854 | 867 |
|
| 868 | + if (isServer && isPassthrough && !NetworkConfig.PassthroughMessageHashSet.Contains(messageType)) |
| 869 | + { |
| 870 | + Debug.LogWarning("MLAPI: Client " + clientId + " tried to send a passthrough message for a messageType not registered as passthrough"); |
| 871 | + return; |
| 872 | + } |
| 873 | + else if (isClient && isPassthrough && !NetworkConfig.PassthroughMessageHashSet.Contains(messageType)) |
| 874 | + { |
| 875 | + Debug.LogWarning("MLAPI: Server tried to send a passthrough message for a messageType not registered as passthrough"); |
| 876 | + return; |
| 877 | + } |
| 878 | + else if (isServer && isPassthrough) |
| 879 | + { |
| 880 | + if (!connectedClients.ContainsKey(passthroughTarget)) |
| 881 | + { |
| 882 | + Debug.LogWarning("MLAPI: Passthrough message was sent with invalid target: " + passthroughTarget + " from client " + clientId); |
| 883 | + return; |
| 884 | + } |
| 885 | + uint? netIdTarget = null; |
| 886 | + ushort? netOrderId = null; |
| 887 | + if (targeted) |
| 888 | + { |
| 889 | + netIdTarget = targetNetworkId; |
| 890 | + netOrderId = networkOrderId; |
| 891 | + } |
| 892 | + InternalMessageHandler.PassthroughSend(passthroughTarget, clientId, messageType, channelId, incommingData, netIdTarget, netOrderId); |
| 893 | + return; |
| 894 | + } |
855 | 895 |
|
856 |
| - ushort bytesToRead = reader.ReadUInt16(); |
857 |
| - byte[] incommingData = reader.ReadBytes(bytesToRead); |
858 |
| - if(NetworkConfig.EncryptedChannelsHashSet.Contains(MessageManager.reverseChannels[channelId])) |
| 896 | + if (messageType >= 32) |
| 897 | + { |
| 898 | + #region CUSTOM MESSAGE |
| 899 | + //Custom message, invoke all message handlers |
| 900 | + if (targeted) |
| 901 | + { |
| 902 | + if (!SpawnManager.spawnedObjects.ContainsKey(targetNetworkId)) |
859 | 903 | {
|
860 |
| - //Encrypted message |
861 |
| - if (isServer) |
862 |
| - incommingData = CryptographyHelper.Decrypt(incommingData, connectedClients[clientId].AesKey); |
863 |
| - else |
864 |
| - incommingData = CryptographyHelper.Decrypt(incommingData, clientAesKey); |
| 904 | + Debug.LogWarning("MLAPI: No target for message found"); |
| 905 | + return; |
865 | 906 | }
|
866 |
| - |
867 |
| - if (isServer && isPassthrough && !NetworkConfig.PassthroughMessageHashSet.Contains(messageType)) |
| 907 | + else if (!SpawnManager.spawnedObjects[targetNetworkId].targetMessageActions.ContainsKey(networkOrderId)) |
868 | 908 | {
|
869 |
| - Debug.LogWarning("MLAPI: Client " + clientId + " tried to send a passthrough message for a messageType not registered as passthrough"); |
| 909 | + Debug.LogWarning("MLAPI: No target messageType for message found"); |
870 | 910 | return;
|
871 | 911 | }
|
872 |
| - else if(isClient && isPassthrough && !NetworkConfig.PassthroughMessageHashSet.Contains(messageType)) |
| 912 | + else if (!SpawnManager.spawnedObjects[targetNetworkId].targetMessageActions[networkOrderId].ContainsKey(messageType)) |
873 | 913 | {
|
874 |
| - Debug.LogWarning("MLAPI: Server tried to send a passthrough message for a messageType not registered as passthrough"); |
| 914 | + Debug.LogWarning("MLAPI: No target found with the given messageType"); |
875 | 915 | return;
|
876 | 916 | }
|
877 |
| - else if(isServer && isPassthrough) |
| 917 | + SpawnManager.spawnedObjects[targetNetworkId].targetMessageActions[networkOrderId][messageType].Invoke(clientId, incommingData); |
| 918 | + } |
| 919 | + else |
| 920 | + { |
| 921 | + foreach (KeyValuePair<int, Action<uint, byte[]>> pair in MessageManager.messageCallbacks[messageType]) |
878 | 922 | {
|
879 |
| - if (!connectedClients.ContainsKey(passthroughTarget)) |
| 923 | + if (isPassthrough) |
| 924 | + pair.Value(passthroughOrigin, incommingData); |
| 925 | + else |
| 926 | + pair.Value(clientId, incommingData); |
| 927 | + } |
| 928 | + } |
| 929 | + #endregion |
| 930 | + } |
| 931 | + else |
| 932 | + { |
| 933 | + #region INTERNAL MESSAGE |
| 934 | + //MLAPI message |
| 935 | + switch (messageType) |
| 936 | + { |
| 937 | + case 0: //Client to server > sends connection buffer |
| 938 | + if (isServer) |
880 | 939 | {
|
881 |
| - Debug.LogWarning("MLAPI: Passthrough message was sent with invalid target: " + passthroughTarget + " from client " + clientId); |
882 |
| - return; |
| 940 | + InternalMessageHandler.HandleConnectionRequest(clientId, incommingData, channelId); |
883 | 941 | }
|
884 |
| - uint? netIdTarget = null; |
885 |
| - ushort? netOrderId = null; |
886 |
| - if (targeted) |
| 942 | + break; |
| 943 | + case 1: //Server informs client it has been approved: |
| 944 | + if (isClient) |
887 | 945 | {
|
888 |
| - netIdTarget = targetNetworkId; |
889 |
| - netOrderId = networkOrderId; |
| 946 | + InternalMessageHandler.HandleConnectionApproved(clientId, incommingData, channelId); |
890 | 947 | }
|
891 |
| - InternalMessageHandler.PassthroughSend(passthroughTarget, clientId, messageType, channelId, incommingData, netIdTarget, netOrderId); |
892 |
| - return; |
893 |
| - } |
894 |
| - |
895 |
| - if (messageType >= 32) |
896 |
| - { |
897 |
| - #region CUSTOM MESSAGE |
898 |
| - //Custom message, invoke all message handlers |
899 |
| - if(targeted) |
| 948 | + break; |
| 949 | + case 2: |
| 950 | + //Server informs client another client connected |
| 951 | + //MLAPI_ADD_OBJECT |
| 952 | + if (isClient) |
900 | 953 | {
|
901 |
| - if (!SpawnManager.spawnedObjects.ContainsKey(targetNetworkId)) |
902 |
| - { |
903 |
| - Debug.LogWarning("MLAPI: No target for message found"); |
904 |
| - return; |
905 |
| - } |
906 |
| - else if (!SpawnManager.spawnedObjects[targetNetworkId].targetMessageActions.ContainsKey(networkOrderId)) |
907 |
| - { |
908 |
| - Debug.LogWarning("MLAPI: No target messageType for message found"); |
909 |
| - return; |
910 |
| - } |
911 |
| - else if (!SpawnManager.spawnedObjects[targetNetworkId].targetMessageActions[networkOrderId].ContainsKey(messageType)) |
912 |
| - { |
913 |
| - Debug.LogWarning("MLAPI: No target found with the given messageType"); |
914 |
| - return; |
915 |
| - } |
916 |
| - SpawnManager.spawnedObjects[targetNetworkId].targetMessageActions[networkOrderId][messageType].Invoke(clientId, incommingData); |
| 954 | + InternalMessageHandler.HandleAddObject(clientId, incommingData, channelId); |
917 | 955 | }
|
918 |
| - else |
| 956 | + break; |
| 957 | + case 3: |
| 958 | + //Server informs client another client disconnected |
| 959 | + //MLAPI_CLIENT_DISCONNECT |
| 960 | + if (isClient) |
919 | 961 | {
|
920 |
| - foreach (KeyValuePair<int, Action<uint, byte[]>> pair in MessageManager.messageCallbacks[messageType]) |
921 |
| - { |
922 |
| - if (isPassthrough) |
923 |
| - pair.Value(passthroughOrigin, incommingData); |
924 |
| - else |
925 |
| - pair.Value(clientId, incommingData); |
926 |
| - } |
| 962 | + InternalMessageHandler.HandleClientDisconnect(clientId, incommingData, channelId); |
927 | 963 | }
|
928 |
| - #endregion |
929 |
| - } |
930 |
| - else |
931 |
| - { |
932 |
| - #region INTERNAL MESSAGE |
933 |
| - //MLAPI message |
934 |
| - switch (messageType) |
| 964 | + break; |
| 965 | + case 4: |
| 966 | + //Server infroms clients to destroy an object |
| 967 | + if (isClient) |
935 | 968 | {
|
936 |
| - case 0: //Client to server > sends connection buffer |
937 |
| - if (isServer) |
938 |
| - { |
939 |
| - InternalMessageHandler.HandleConnectionRequest(clientId, incommingData, channelId); |
940 |
| - } |
941 |
| - break; |
942 |
| - case 1: //Server informs client it has been approved: |
943 |
| - if (isClient) |
944 |
| - { |
945 |
| - InternalMessageHandler.HandleConnectionApproved(clientId, incommingData, channelId); |
946 |
| - } |
947 |
| - break; |
948 |
| - case 2: |
949 |
| - //Server informs client another client connected |
950 |
| - //MLAPI_ADD_OBJECT |
951 |
| - if (isClient) |
952 |
| - { |
953 |
| - InternalMessageHandler.HandleAddObject(clientId, incommingData, channelId); |
954 |
| - } |
955 |
| - break; |
956 |
| - case 3: |
957 |
| - //Server informs client another client disconnected |
958 |
| - //MLAPI_CLIENT_DISCONNECT |
959 |
| - if (isClient) |
960 |
| - { |
961 |
| - InternalMessageHandler.HandleClientDisconnect(clientId, incommingData, channelId); |
962 |
| - } |
963 |
| - break; |
964 |
| - case 4: |
965 |
| - //Server infroms clients to destroy an object |
966 |
| - if (isClient) |
967 |
| - { |
968 |
| - InternalMessageHandler.HandleDestroyObject(clientId, incommingData, channelId); |
969 |
| - } |
970 |
| - break; |
971 |
| - case 5: |
972 |
| - //Scene switch |
973 |
| - if (isClient) |
974 |
| - { |
975 |
| - InternalMessageHandler.HandleSwitchScene(clientId, incommingData, channelId); |
976 |
| - } |
977 |
| - break; |
978 |
| - case 6: //Spawn pool object |
979 |
| - if (isClient) |
980 |
| - { |
981 |
| - InternalMessageHandler.HandleSpawnPoolObject(clientId, incommingData, channelId); |
982 |
| - } |
983 |
| - break; |
984 |
| - case 7: //Destroy pool object |
985 |
| - if (isClient) |
986 |
| - { |
987 |
| - InternalMessageHandler.HandleDestroyPoolObject(clientId, incommingData, channelId); |
988 |
| - } |
989 |
| - break; |
990 |
| - case 8: //Change owner |
991 |
| - if (isClient) |
992 |
| - { |
993 |
| - InternalMessageHandler.HandleChangeOwner(clientId, incommingData, channelId); |
994 |
| - } |
995 |
| - break; |
996 |
| - case 9: //Syncvar |
997 |
| - if (isClient) |
998 |
| - { |
999 |
| - InternalMessageHandler.HandleSyncVarUpdate(clientId, incommingData, channelId); |
1000 |
| - } |
1001 |
| - break; |
1002 |
| - case 10: |
1003 |
| - if (isClient) //MLAPI_ADD_OBJECTS (plural) |
1004 |
| - { |
1005 |
| - InternalMessageHandler.HandleAddObjects(clientId, incommingData, channelId); |
1006 |
| - } |
1007 |
| - break; |
1008 |
| - case 11: |
1009 |
| - if (isClient) |
1010 |
| - { |
1011 |
| - InternalMessageHandler.HandleTimeSync(clientId, incommingData, channelId); |
1012 |
| - } |
1013 |
| - break; |
| 969 | + InternalMessageHandler.HandleDestroyObject(clientId, incommingData, channelId); |
1014 | 970 | }
|
1015 |
| - #endregion |
1016 |
| - } |
| 971 | + break; |
| 972 | + case 5: |
| 973 | + //Scene switch |
| 974 | + if (isClient) |
| 975 | + { |
| 976 | + InternalMessageHandler.HandleSwitchScene(clientId, incommingData, channelId); |
| 977 | + } |
| 978 | + break; |
| 979 | + case 6: //Spawn pool object |
| 980 | + if (isClient) |
| 981 | + { |
| 982 | + InternalMessageHandler.HandleSpawnPoolObject(clientId, incommingData, channelId); |
| 983 | + } |
| 984 | + break; |
| 985 | + case 7: //Destroy pool object |
| 986 | + if (isClient) |
| 987 | + { |
| 988 | + InternalMessageHandler.HandleDestroyPoolObject(clientId, incommingData, channelId); |
| 989 | + } |
| 990 | + break; |
| 991 | + case 8: //Change owner |
| 992 | + if (isClient) |
| 993 | + { |
| 994 | + InternalMessageHandler.HandleChangeOwner(clientId, incommingData, channelId); |
| 995 | + } |
| 996 | + break; |
| 997 | + case 9: //Syncvar |
| 998 | + if (isClient) |
| 999 | + { |
| 1000 | + InternalMessageHandler.HandleSyncVarUpdate(clientId, incommingData, channelId); |
| 1001 | + } |
| 1002 | + break; |
| 1003 | + case 10: |
| 1004 | + if (isClient) //MLAPI_ADD_OBJECTS (plural) |
| 1005 | + { |
| 1006 | + InternalMessageHandler.HandleAddObjects(clientId, incommingData, channelId); |
| 1007 | + } |
| 1008 | + break; |
| 1009 | + case 11: |
| 1010 | + if (isClient) |
| 1011 | + { |
| 1012 | + InternalMessageHandler.HandleTimeSync(clientId, incommingData, channelId); |
| 1013 | + } |
| 1014 | + break; |
1017 | 1015 | }
|
| 1016 | + #endregion |
1018 | 1017 | }
|
1019 | 1018 | }
|
1020 | 1019 |
|
|
0 commit comments