1 //
2 // ========================================================================
3 // Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
8 //
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
11 //
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
14 //
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
17 //
18
19 package org.eclipse.jetty.websocket.common;
20
21 public final class OpCode
22 {
23 /**
24 * OpCode for a Continuation Frame
25 *
26 * @see <a href="https://tools.ietf.org/html/rfc6455#section-11.8">RFC 6455, Section 11.8 (WebSocket Opcode Registry</a>
27 */
28 public static final byte CONTINUATION = (byte)0x00;
29
30 /**
31 * OpCode for a Text Frame
32 *
33 * @see <a href="https://tools.ietf.org/html/rfc6455#section-11.8">RFC 6455, Section 11.8 (WebSocket Opcode Registry</a>
34 */
35 public static final byte TEXT = (byte)0x01;
36
37 /**
38 * OpCode for a Binary Frame
39 *
40 * @see <a href="https://tools.ietf.org/html/rfc6455#section-11.8">RFC 6455, Section 11.8 (WebSocket Opcode Registry</a>
41 */
42 public static final byte BINARY = (byte)0x02;
43
44 /**
45 * OpCode for a Close Frame
46 *
47 * @see <a href="https://tools.ietf.org/html/rfc6455#section-11.8">RFC 6455, Section 11.8 (WebSocket Opcode Registry</a>
48 */
49 public static final byte CLOSE = (byte)0x08;
50
51 /**
52 * OpCode for a Ping Frame
53 *
54 * @see <a href="https://tools.ietf.org/html/rfc6455#section-11.8">RFC 6455, Section 11.8 (WebSocket Opcode Registry</a>
55 */
56 public static final byte PING = (byte)0x09;
57
58 /**
59 * OpCode for a Pong Frame
60 *
61 * @see <a href="https://tools.ietf.org/html/rfc6455#section-11.8">RFC 6455, Section 11.8 (WebSocket Opcode Registry</a>
62 */
63 public static final byte PONG = (byte)0x0A;
64
65 /**
66 * An undefined OpCode
67 */
68 public static final byte UNDEFINED = (byte)-1;
69
70 public static boolean isControlFrame(byte opcode)
71 {
72 return (opcode >= CLOSE);
73 }
74
75 public static boolean isDataFrame(byte opcode)
76 {
77 return (opcode == TEXT) || (opcode == BINARY);
78 }
79
80 /**
81 * Test for known opcodes (per the RFC spec)
82 *
83 * @param opcode
84 * the opcode to test
85 * @return true if known. false if unknown, undefined, or reserved
86 */
87 public static boolean isKnown(byte opcode)
88 {
89 return (opcode == CONTINUATION) || (opcode == TEXT) || (opcode == BINARY) || (opcode == CLOSE) || (opcode == PING) || (opcode == PONG);
90 }
91
92 public static String name(byte opcode)
93 {
94 switch (opcode)
95 {
96 case -1:
97 return "NO-OP";
98 case CONTINUATION:
99 return "CONTINUATION";
100 case TEXT:
101 return "TEXT";
102 case BINARY: return "BINARY";
103 case CLOSE:
104 return "CLOSE";
105 case PING:
106 return "PING";
107 case PONG:
108 return "PONG";
109 default:
110 return "NON-SPEC[" + opcode + "]";
111 }
112 }
113 }