namespace lib; public enum RequestType { Register = 1, ConfirmRegister = 2, Login = 3, GetMessages = 4, GetUserKey = 5, SendMessage = 6, } public static class Request { public static byte[] CreateRequest(RequestType Type, ref byte counter, byte[] data) { if (data.Length > 13) { throw new Exception("extra data is too long: " + data.Length.ToString()); } byte[] msg = new byte[16]; msg[0] = 0; // version msg[1] = (byte)Type; msg[2] = counter; if (counter == byte.MaxValue) { counter = 0; } else { counter += 1; } // insert data 3..16 Array.Copy(data, 0, msg, 3, data.Length); return msg; } public static string RequestToString(byte[] Request) { if (Request.Length != 16) { throw new Exception("Request size is not 128!"); } string res = $"V: {Request[0]}, "; res += $"Request type: {(RequestType)Request[1]}, "; res += $"Counter: {Request[2]}\n"; res += $"Extra: {string.Join(' ', Request[3..].Select(b => b.ToString("{b8}")))}"; // also display extra data based on the request itself switch ((RequestType)Request[1]) { case RequestType.Register: string phone = Utils.BytesToNumber(Request[3..11]); int rsaLen = BitConverter.ToInt32(Request, 11); res += $"Phone: {phone}, RSA Key length: {rsaLen}"; break; case RequestType.ConfirmRegister: string code = string.Join(' ', Request[3..9]); int sigLen = BitConverter.ToInt32(Request, 9); res += $"Code: {code}, signature length: {sigLen}"; break; case RequestType.Login: phone = Utils.BytesToNumber(Request[3..11]); sigLen = BitConverter.ToInt16(Request, 11); res += $"Phone: {phone}, signature length: {sigLen}"; break; case RequestType.GetMessages: break; case RequestType.GetUserKey: break; case RequestType.SendMessage: break; default: res += "INVALID REQUEST TYPE"; break; } return res; } }