diff options
author | Joe Crayne <joe@jerkface.net> | 2018-09-08 06:25:55 -0400 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2018-09-08 08:36:05 -0400 |
commit | adf62395931f4b90491cc1338b9a5105da98fbbe (patch) | |
tree | 1177cc719fdd2e5449e03963fb32b24b7017a94a /src | |
parent | ad20be57786ad34f80192206c480d575392b4ebb (diff) |
Factored Data.Tox.Message out of Network.Tox.Crypto.Transport.
Diffstat (limited to 'src')
-rw-r--r-- | src/Data/Tox/Message.hs | 83 | ||||
-rw-r--r-- | src/Network/Tox/Crypto/Handlers.hs | 1 | ||||
-rw-r--r-- | src/Network/Tox/Crypto/Transport.hs | 271 |
3 files changed, 85 insertions, 270 deletions
diff --git a/src/Data/Tox/Message.hs b/src/Data/Tox/Message.hs new file mode 100644 index 00000000..b77307a2 --- /dev/null +++ b/src/Data/Tox/Message.hs | |||
@@ -0,0 +1,83 @@ | |||
1 | -- | This module assigns meaningful symbolic names to Tox message ids and | ||
2 | -- classifies messages as lossy or lossless. | ||
3 | {-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
4 | {-# LANGUAGE PatternSynonyms #-} | ||
5 | {-# LANGUAGE ViewPatterns #-} | ||
6 | module Data.Tox.Message where | ||
7 | |||
8 | import Data.Word | ||
9 | |||
10 | -- | The one-byte type code prefix that classifies a 'CryptoMessage'. | ||
11 | newtype MessageID = MessageID Word8 deriving (Eq,Enum,Ord,Bounded) | ||
12 | pattern Padding = MessageID 0 -- ^ 0 padding (skipped until we hit a non zero (data id) byte) | ||
13 | pattern PacketRequest = MessageID 1 -- ^ 1 packet request packet (lossy packet) | ||
14 | pattern KillPacket = MessageID 2 -- ^ 2 connection kill packet (lossy packet) | ||
15 | pattern UnspecifiedPacket003 = MessageID 3 -- ^ 3+ unspecified | ||
16 | pattern PING = MessageID 16 -- ^ 16+ reserved for Messenger usage (lossless packets) | ||
17 | -- TODO: rename to ALIVE 16 | ||
18 | -- SHARE_RELAYS 17 | ||
19 | -- FRIEND_REQUESTS 18 | ||
20 | pattern ONLINE = MessageID 24 -- 1 byte | ||
21 | pattern OFFLINE = MessageID 25 -- 1 byte | ||
22 | -- LOSSLESS_RANGE_SIZE 32 | ||
23 | pattern NICKNAME = MessageID 48 -- up to 129 bytes | ||
24 | pattern STATUSMESSAGE = MessageID 49 -- up to 1008 bytes | ||
25 | pattern USERSTATUS = MessageID 50 -- 2 bytes | ||
26 | pattern TYPING = MessageID 51 -- 2 bytes | ||
27 | -- LOSSY_RANGE_SIZE 63 | ||
28 | pattern MESSAGE = MessageID 64 -- up to 1373 bytes | ||
29 | pattern ACTION = MessageID 65 -- up to 1373 bytes | ||
30 | pattern MSI = MessageID 69 | ||
31 | pattern FILE_SENDREQUEST = MessageID 80 -- 1+1+4+8+32+max255 = up to 301 | ||
32 | pattern FILE_CONTROL = MessageID 81 -- 8 bytes if seek, otherwise 4 | ||
33 | pattern FILE_DATA = MessageID 82 -- up to 1373 | ||
34 | pattern INVITE_GROUPCHAT = MessageID 96 -- 0x60 | ||
35 | -- TODO: rename to INVITE_CONFERENCE 96 | ||
36 | pattern ONLINE_PACKET = MessageID 97 -- 0x61 | ||
37 | pattern DIRECT_GROUPCHAT = MessageID 98 -- 0x62 | ||
38 | -- TODO: rename to DIRECT_CONFERENCE 98 | ||
39 | pattern MESSAGE_GROUPCHAT = MessageID 99 -- 0x63 | ||
40 | -- TODO: rename to MESSAGE_CONFERENCE 99 | ||
41 | -- LOSSLESS_RANGE_START 160 | ||
42 | pattern MessengerLossy192 = MessageID 192 -- ^ 192+ reserved for Messenger usage (lossy packets) | ||
43 | pattern LOSSY_GROUPCHAT = MessageID 199 -- 0xC7 | ||
44 | pattern Messenger255 = MessageID 255 -- ^ 255 reserved for Messenger usage (lossless packet) | ||
45 | |||
46 | instance Show MessageID where | ||
47 | show Padding = "Padding" | ||
48 | show PacketRequest = "PacketRequest" | ||
49 | show KillPacket = "KillPacket" | ||
50 | show UnspecifiedPacket003 = "UnspecifiedPacket003" | ||
51 | show PING = "PING" | ||
52 | show ONLINE = "ONLINE" | ||
53 | show OFFLINE = "OFFLINE" | ||
54 | show NICKNAME = "NICKNAME" | ||
55 | show STATUSMESSAGE = "STATUSMESSAGE" | ||
56 | show USERSTATUS = "USERSTATUS" | ||
57 | show TYPING = "TYPING" | ||
58 | show MESSAGE = "MESSAGE" | ||
59 | show ACTION = "ACTION" | ||
60 | show MSI = "MSI" | ||
61 | show FILE_SENDREQUEST = "FILE_SENDREQUEST" | ||
62 | show FILE_CONTROL = "FILE_CONTROL" | ||
63 | show FILE_DATA = "FILE_DATA" | ||
64 | show INVITE_GROUPCHAT = "INVITE_GROUPCHAT" | ||
65 | show ONLINE_PACKET = "ONLINE_PACKET" | ||
66 | show DIRECT_GROUPCHAT = "DIRECT_GROUPCHAT" | ||
67 | show MESSAGE_GROUPCHAT = "MESSAGE_GROUPCHAT" | ||
68 | show MessengerLossy192 = "MessengerLossy192" | ||
69 | show LOSSY_GROUPCHAT = "LOSSY_GROUPCHAT" | ||
70 | show Messenger255 = "Messenger255" | ||
71 | show (MessageID n) = "MessageID " ++ show n | ||
72 | |||
73 | data LossyOrLossless = Lossless | Lossy | ||
74 | deriving (Eq,Ord,Enum,Show,Bounded) | ||
75 | |||
76 | -- | Classify a packet as lossy or lossless. | ||
77 | lossyness :: MessageID -> LossyOrLossless | ||
78 | lossyness (fromEnum -> x) | x < 3 = Lossy | ||
79 | lossyness (fromEnum -> x) | {-16 <= x,-} x < 192 = Lossless | ||
80 | lossyness (fromEnum -> x) | 192 <= x, x < 255 = Lossy | ||
81 | lossyness (fromEnum -> 255) = Lossless | ||
82 | |||
83 | |||
diff --git a/src/Network/Tox/Crypto/Handlers.hs b/src/Network/Tox/Crypto/Handlers.hs index 3269f5dd..f0332df0 100644 --- a/src/Network/Tox/Crypto/Handlers.hs +++ b/src/Network/Tox/Crypto/Handlers.hs | |||
@@ -476,7 +476,6 @@ ncToWire getState seqno bufend pktno msg = do | |||
476 | setMessageId (UpToN _ x) mid = UpToN (toEnum8 mid) x | 476 | setMessageId (UpToN _ x) mid = UpToN (toEnum8 mid) x |
477 | msg' = setMessageId msg outid | 477 | msg' = setMessageId msg outid |
478 | in case lsness msg of | 478 | in case lsness msg of |
479 | UnknownLossyness -> return Nothing | ||
480 | Lossy -> let cd = | 479 | Lossy -> let cd = |
481 | CryptoData | 480 | CryptoData |
482 | { bufferStart = seqno | 481 | { bufferStart = seqno |
diff --git a/src/Network/Tox/Crypto/Transport.hs b/src/Network/Tox/Crypto/Transport.hs index 3ade25f7..9220fd1e 100644 --- a/src/Network/Tox/Crypto/Transport.hs +++ b/src/Network/Tox/Crypto/Transport.hs | |||
@@ -53,16 +53,17 @@ module Network.Tox.Crypto.Transport | |||
53 | , sizedAtLeastN | 53 | , sizedAtLeastN |
54 | , isIndirectGrpChat | 54 | , isIndirectGrpChat |
55 | , LossyOrLossless(..) | 55 | , LossyOrLossless(..) |
56 | , lossyness | ||
57 | , fromEnum8 | 56 | , fromEnum8 |
58 | , fromEnum16 | 57 | , fromEnum16 |
59 | , toEnum8 | 58 | , toEnum8 |
60 | , msgSizeParam | 59 | , msgSizeParam |
61 | , NegotiationID(..) | 60 | , NegotiationID(..) |
62 | , NegotiationMsg(..) | 61 | , NegotiationMsg(..) |
62 | , module Data.Tox.Message | ||
63 | ) where | 63 | ) where |
64 | 64 | ||
65 | import Crypto.Tox | 65 | import Crypto.Tox |
66 | import Data.Tox.Message | ||
66 | import Network.Tox.DHT.Transport (Cookie) | 67 | import Network.Tox.DHT.Transport (Cookie) |
67 | import Network.Tox.NodeId | 68 | import Network.Tox.NodeId |
68 | 69 | ||
@@ -750,15 +751,6 @@ isIndirectGrpChat MESSAGE_GROUPCHAT = True | |||
750 | isIndirectGrpChat LOSSY_GROUPCHAT = True | 751 | isIndirectGrpChat LOSSY_GROUPCHAT = True |
751 | isIndirectGrpChat _ = False | 752 | isIndirectGrpChat _ = False |
752 | 753 | ||
753 | data LossyOrLossless = UnknownLossyness | Lossless | Lossy | ||
754 | deriving (Eq,Ord,Enum,Show,Bounded) | ||
755 | |||
756 | lossyness :: MessageID -> LossyOrLossless | ||
757 | lossyness (fromEnum -> x) | x < 3 = Lossy | ||
758 | lossyness (fromEnum -> x) | {-x >= 16,-} x < 192 = Lossless | ||
759 | lossyness (fromEnum -> x) | x >= 192, x < 255 = Lossy | ||
760 | lossyness (fromEnum -> 255) = Lossless | ||
761 | lossyness _ = UnknownLossyness | ||
762 | 754 | ||
763 | isKillPacket :: MessageType -> Bool | 755 | isKillPacket :: MessageType -> Bool |
764 | isKillPacket (Msg KillPacket) = True | 756 | isKillPacket (Msg KillPacket) = True |
@@ -768,265 +760,6 @@ isOFFLINE :: MessageType -> Bool | |||
768 | isOFFLINE (Msg OFFLINE) = True | 760 | isOFFLINE (Msg OFFLINE) = True |
769 | isOFFLINE _ = False | 761 | isOFFLINE _ = False |
770 | 762 | ||
771 | -- TODO: Flesh this out. | ||
772 | data MessageID -- First byte indicates data | ||
773 | = Padding -- ^ 0 padding (skipped until we hit a non zero (data id) byte) | ||
774 | | PacketRequest -- ^ 1 packet request packet (lossy packet) | ||
775 | | KillPacket -- ^ 2 connection kill packet (lossy packet) | ||
776 | | UnspecifiedPacket003 -- ^ 3+ unspecified | ||
777 | | UnspecifiedPacket004 | ||
778 | | UnspecifiedPacket005 | ||
779 | | UnspecifiedPacket006 | ||
780 | | UnspecifiedPacket007 | ||
781 | | UnspecifiedPacket008 | ||
782 | | UnspecifiedPacket009 | ||
783 | | UnspecifiedPacket010 | ||
784 | | UnspecifiedPacket011 | ||
785 | | UnspecifiedPacket012 | ||
786 | | UnspecifiedPacket013 | ||
787 | | UnspecifiedPacket014 | ||
788 | | UnspecifiedPacket015 | ||
789 | | PING -- ^ 16+ reserved for Messenger usage (lossless packets) | ||
790 | | MessengerLossless017 | ||
791 | | MessengerLossless018 | ||
792 | | MessengerLossless019 | ||
793 | | MessengerLossless020 | ||
794 | | MessengerLossless021 | ||
795 | | MessengerLossless022 | ||
796 | | MessengerLossless023 | ||
797 | | ONLINE -- 1 byte | ||
798 | | OFFLINE -- 1 byte | ||
799 | | MessengerLossless026 | ||
800 | | MessengerLossless027 | ||
801 | | MessengerLossless028 | ||
802 | | MessengerLossless029 | ||
803 | | MessengerLossless030 | ||
804 | | MessengerLossless031 | ||
805 | | MessengerLossless032 | ||
806 | | MessengerLossless033 | ||
807 | | MessengerLossless034 | ||
808 | | MessengerLossless035 | ||
809 | | MessengerLossless036 | ||
810 | | MessengerLossless037 | ||
811 | | MessengerLossless038 | ||
812 | | MessengerLossless039 | ||
813 | | MessengerLossless040 | ||
814 | | MessengerLossless041 | ||
815 | | MessengerLossless042 | ||
816 | | MessengerLossless043 | ||
817 | | MessengerLossless044 | ||
818 | | MessengerLossless045 | ||
819 | | MessengerLossless046 | ||
820 | | MessengerLossless047 | ||
821 | | NICKNAME -- up to 129 bytes | ||
822 | | STATUSMESSAGE -- up to 1008 bytes | ||
823 | | USERSTATUS -- 2 bytes | ||
824 | | TYPING -- 2 bytes | ||
825 | | MessengerLossless052 | ||
826 | | MessengerLossless053 | ||
827 | | MessengerLossless054 | ||
828 | | MessengerLossless055 | ||
829 | | MessengerLossless056 | ||
830 | | MessengerLossless057 | ||
831 | | MessengerLossless058 | ||
832 | | MessengerLossless059 | ||
833 | | MessengerLossless060 | ||
834 | | MessengerLossless061 | ||
835 | | MessengerLossless062 | ||
836 | | MessengerLossless063 | ||
837 | | MESSAGE -- up to 1373 bytes | ||
838 | | ACTION -- up to 1373 bytes | ||
839 | | MessengerLossless066 | ||
840 | | MessengerLossless067 | ||
841 | | MessengerLossless068 | ||
842 | | MSI | ||
843 | | MessengerLossless070 | ||
844 | | MessengerLossless071 | ||
845 | | MessengerLossless072 | ||
846 | | MessengerLossless073 | ||
847 | | MessengerLossless074 | ||
848 | | MessengerLossless075 | ||
849 | | MessengerLossless076 | ||
850 | | MessengerLossless077 | ||
851 | | MessengerLossless078 | ||
852 | | MessengerLossless079 | ||
853 | | FILE_SENDREQUEST -- 1+1+4+8+32+max255 = up to 301 | ||
854 | | FILE_CONTROL -- 8 bytes if seek, otherwise 4 | ||
855 | | FILE_DATA -- up to 1373 | ||
856 | | MessengerLossless083 | ||
857 | | MessengerLossless084 | ||
858 | | MessengerLossless085 | ||
859 | | MessengerLossless086 | ||
860 | | MessengerLossless087 | ||
861 | | MessengerLossless088 | ||
862 | | MessengerLossless089 | ||
863 | | MessengerLossless090 | ||
864 | | MessengerLossless091 | ||
865 | | MessengerLossless092 | ||
866 | | MessengerLossless093 | ||
867 | | MessengerLossless094 | ||
868 | | MessengerLossless095 | ||
869 | | INVITE_GROUPCHAT -- 0x60 | ||
870 | | ONLINE_PACKET -- 0x61 | ||
871 | | DIRECT_GROUPCHAT -- 0x62 | ||
872 | | MESSAGE_GROUPCHAT -- 0x63 | ||
873 | | MessengerLossless100 | ||
874 | | MessengerLossless101 | ||
875 | | MessengerLossless102 | ||
876 | | MessengerLossless103 | ||
877 | | MessengerLossless104 | ||
878 | | MessengerLossless105 | ||
879 | | MessengerLossless106 | ||
880 | | MessengerLossless107 | ||
881 | | MessengerLossless108 | ||
882 | | MessengerLossless109 | ||
883 | | MessengerLossless110 | ||
884 | | MessengerLossless111 | ||
885 | | MessengerLossless112 | ||
886 | | MessengerLossless113 | ||
887 | | MessengerLossless114 | ||
888 | | MessengerLossless115 | ||
889 | | MessengerLossless116 | ||
890 | | MessengerLossless117 | ||
891 | | MessengerLossless118 | ||
892 | | MessengerLossless119 | ||
893 | | MessengerLossless120 | ||
894 | | MessengerLossless121 | ||
895 | | MessengerLossless122 | ||
896 | | MessengerLossless123 | ||
897 | | MessengerLossless124 | ||
898 | | MessengerLossless125 | ||
899 | | MessengerLossless126 | ||
900 | | MessengerLossless127 | ||
901 | | MessengerLossless128 | ||
902 | | MessengerLossless129 | ||
903 | | MessengerLossless130 | ||
904 | | MessengerLossless131 | ||
905 | | MessengerLossless132 | ||
906 | | MessengerLossless133 | ||
907 | | MessengerLossless134 | ||
908 | | MessengerLossless135 | ||
909 | | MessengerLossless136 | ||
910 | | MessengerLossless137 | ||
911 | | MessengerLossless138 | ||
912 | | MessengerLossless139 | ||
913 | | MessengerLossless140 | ||
914 | | MessengerLossless141 | ||
915 | | MessengerLossless142 | ||
916 | | MessengerLossless143 | ||
917 | | MessengerLossless144 | ||
918 | | MessengerLossless145 | ||
919 | | MessengerLossless146 | ||
920 | | MessengerLossless147 | ||
921 | | MessengerLossless148 | ||
922 | | MessengerLossless149 | ||
923 | | MessengerLossless150 | ||
924 | | MessengerLossless151 | ||
925 | | MessengerLossless152 | ||
926 | | MessengerLossless153 | ||
927 | | MessengerLossless154 | ||
928 | | MessengerLossless155 | ||
929 | | MessengerLossless156 | ||
930 | | MessengerLossless157 | ||
931 | | MessengerLossless158 | ||
932 | | MessengerLossless159 | ||
933 | | MessengerLossless160 | ||
934 | | MessengerLossless161 | ||
935 | | MessengerLossless162 | ||
936 | | MessengerLossless163 | ||
937 | | MessengerLossless164 | ||
938 | | MessengerLossless165 | ||
939 | | MessengerLossless166 | ||
940 | | MessengerLossless167 | ||
941 | | MessengerLossless168 | ||
942 | | MessengerLossless169 | ||
943 | | MessengerLossless170 | ||
944 | | MessengerLossless171 | ||
945 | | MessengerLossless172 | ||
946 | | MessengerLossless173 | ||
947 | | MessengerLossless174 | ||
948 | | MessengerLossless175 | ||
949 | | MessengerLossless176 | ||
950 | | MessengerLossless177 | ||
951 | | MessengerLossless178 | ||
952 | | MessengerLossless179 | ||
953 | | MessengerLossless180 | ||
954 | | MessengerLossless181 | ||
955 | | MessengerLossless182 | ||
956 | | MessengerLossless183 | ||
957 | | MessengerLossless184 | ||
958 | | MessengerLossless185 | ||
959 | | MessengerLossless186 | ||
960 | | MessengerLossless187 | ||
961 | | MessengerLossless188 | ||
962 | | MessengerLossless189 | ||
963 | | MessengerLossless190 | ||
964 | | MessengerLossless191 | ||
965 | | MessengerLossy192 -- ^ 192+ reserved for Messenger usage (lossy packets) | ||
966 | | MessengerLossy193 | ||
967 | | MessengerLossy194 | ||
968 | | MessengerLossy195 | ||
969 | | MessengerLossy196 | ||
970 | | MessengerLossy197 | ||
971 | | MessengerLossy198 | ||
972 | | LOSSY_GROUPCHAT -- 0xC7 | ||
973 | | MessengerLossy200 | ||
974 | | MessengerLossy201 | ||
975 | | MessengerLossy202 | ||
976 | | MessengerLossy203 | ||
977 | | MessengerLossy204 | ||
978 | | MessengerLossy205 | ||
979 | | MessengerLossy206 | ||
980 | | MessengerLossy207 | ||
981 | | MessengerLossy208 | ||
982 | | MessengerLossy209 | ||
983 | | MessengerLossy210 | ||
984 | | MessengerLossy211 | ||
985 | | MessengerLossy212 | ||
986 | | MessengerLossy213 | ||
987 | | MessengerLossy214 | ||
988 | | MessengerLossy215 | ||
989 | | MessengerLossy216 | ||
990 | | MessengerLossy217 | ||
991 | | MessengerLossy218 | ||
992 | | MessengerLossy219 | ||
993 | | MessengerLossy220 | ||
994 | | MessengerLossy221 | ||
995 | | MessengerLossy222 | ||
996 | | MessengerLossy223 | ||
997 | | MessengerLossy224 | ||
998 | | MessengerLossy225 | ||
999 | | MessengerLossy226 | ||
1000 | | MessengerLossy227 | ||
1001 | | MessengerLossy228 | ||
1002 | | MessengerLossy229 | ||
1003 | | MessengerLossy230 | ||
1004 | | MessengerLossy231 | ||
1005 | | MessengerLossy232 | ||
1006 | | MessengerLossy233 | ||
1007 | | MessengerLossy234 | ||
1008 | | MessengerLossy235 | ||
1009 | | MessengerLossy236 | ||
1010 | | MessengerLossy237 | ||
1011 | | MessengerLossy238 | ||
1012 | | MessengerLossy239 | ||
1013 | | MessengerLossy240 | ||
1014 | | MessengerLossy241 | ||
1015 | | MessengerLossy242 | ||
1016 | | MessengerLossy243 | ||
1017 | | MessengerLossy244 | ||
1018 | | MessengerLossy245 | ||
1019 | | MessengerLossy246 | ||
1020 | | MessengerLossy247 | ||
1021 | | MessengerLossy248 | ||
1022 | | MessengerLossy249 | ||
1023 | | MessengerLossy250 | ||
1024 | | MessengerLossy251 | ||
1025 | | MessengerLossy252 | ||
1026 | | MessengerLossy253 | ||
1027 | | MessengerLossy254 | ||
1028 | | Messenger255 -- ^ 255 reserved for Messenger usage (lossless packet) | ||
1029 | deriving (Show,Eq,Enum,Ord,Bounded) | ||
1030 | 763 | ||
1031 | 764 | ||
1032 | 765 | ||