Skip to content

Commit

Permalink
adding opcode enum
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Frowen committed Sep 23, 2024
1 parent ac069f9 commit 09e037d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
31 changes: 17 additions & 14 deletions source/Runtime/Common/MessageProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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)]
Expand All @@ -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);
Expand Down Expand Up @@ -136,34 +144,29 @@ static void ThrowIfMaskNotExpected(bool hasMask, bool expectMask)
}

/// <exception cref="InvalidDataException"></exception>
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}");
Expand Down
6 changes: 3 additions & 3 deletions source/Runtime/Common/ReceiveLoop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct Header
{
public int payloadLength;
public int offset;
public int opcode;
public OpCode opcode;
public bool finished;
}

Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 09e037d

Please sign in to comment.