diff --git a/source/Runtime/Common/MessageProcessor.cs b/source/Runtime/Common/MessageProcessor.cs index 0d3d766..d5fb48f 100644 --- a/source/Runtime/Common/MessageProcessor.cs +++ b/source/Runtime/Common/MessageProcessor.cs @@ -4,6 +4,14 @@ namespace JamesFrowen.SimpleWeb { + public enum OpCode : byte + { + continuation = 0, + text = 1, + binary = 2, + close = 8, + } + public static class MessageProcessor { [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -25,9 +33,9 @@ public static bool NeedToReadLongLength(byte[] buffer) } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static int GetOpcode(byte[] buffer) + public static OpCode GetOpcode(byte[] buffer) { - return buffer[0] & 0b0000_1111; + return (OpCode)(buffer[0] & 0b0000_1111); } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -53,7 +61,7 @@ public static void ValidateHeader(byte[] buffer, int maxLength, bool expectMask, bool finished = Finished(buffer); bool hasMask = (buffer[1] & 0b1000_0000) != 0; // true from clients, false from server, "All messages from the client to the server have this bit set" - int opcode = buffer[0] & 0b0000_1111; // expecting 1 - text message + OpCode opcode = GetOpcode(buffer); byte lenByte = FirstLengthByte(buffer); ThrowIfMaskNotExpected(hasMask, expectMask); @@ -136,34 +144,29 @@ static void ThrowIfMaskNotExpected(bool hasMask, bool expectMask) } /// - static void ThrowIfBadOpCode(int opcode, bool finished, bool opCodeContinuation) + static void ThrowIfBadOpCode(OpCode opcode, bool finished, bool opCodeContinuation) { - // 0 = continuation - // 2 = binary - // 8 = close - // do we expect Continuation? if (opCodeContinuation) { // good it was Continuation - if (opcode == 0) + if (opcode == OpCode.continuation) return; - // bad, wasn't Continuation throw new InvalidDataException("Expected opcode to be Continuation"); } else if (!finished) { - // fragmented message, should be binary - if (opcode == 2) + // Fragmented message, should be binary + if (opcode == OpCode.binary) return; throw new InvalidDataException("Expected opcode to be binary"); } else { - // normal message, should be binary or close - if (opcode == 2 || opcode == 8) + // Normal message, should be binary, text, or close + if (opcode == OpCode.binary || opcode == OpCode.close) return; throw new InvalidDataException($"Unexpected opcode {opcode}"); diff --git a/source/Runtime/Common/ReceiveLoop.cs b/source/Runtime/Common/ReceiveLoop.cs index 8768dd2..97dd8c2 100644 --- a/source/Runtime/Common/ReceiveLoop.cs +++ b/source/Runtime/Common/ReceiveLoop.cs @@ -42,7 +42,7 @@ struct Header { public int payloadLength; public int offset; - public int opcode; + public OpCode opcode; public bool finished; } @@ -124,10 +124,10 @@ static void ReadOneMessage(Config config, byte[] buffer) { switch (header.opcode) { - case 2: + case OpCode.binary: HandleArrayMessage(config, buffer, msgOffset, header.payloadLength); break; - case 8: + case OpCode.close: HandleCloseMessage(config, buffer, msgOffset, header.payloadLength); break; }