1 /*
2  * Collie - An asynchronous event-driven network framework using Dlang development
3  *
4  * Copyright (C) 2015-2017  Shanghai Putao Technology Co., Ltd 
5  *
6  * Developer: putao's Dlang team
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 module collie.codec.http.codec.wsframe;
12 
13 enum WebSocketGuid = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
14 
15 enum OpCode
16 {
17 	OpCodeContinue = 0x0,
18 	OpCodeText = 0x1,
19 	OpCodeBinary = 0x2,
20 	OpCodeReserved3 = 0x3,
21 	OpCodeReserved4 = 0x4,
22 	OpCodeReserved5 = 0x5,
23 	OpCodeReserved6 = 0x6,
24 	OpCodeReserved7 = 0x7,
25 	OpCodeClose = 0x8,
26 	OpCodePing = 0x9,
27 	OpCodePong = 0xA,
28 	OpCodeReservedB = 0xB,
29 	OpCodeReservedC = 0xC,
30 	OpCodeReservedD = 0xD,
31 	OpCodeReservedE = 0xE,
32 	OpCodeReservedF = 0xF
33 }
34 
35 enum CloseCode
36 {
37 	CloseCodeNormal = 1000,
38 	CloseCodeGoingAway = 1001,
39 	CloseCodeProtocolError = 1002,
40 	CloseCodeDatatypeNotSupported = 1003,
41 	CloseCodeReserved1004 = 1004,
42 	CloseCodeMissingStatusCode = 1005,
43 	CloseCodeAbnormalDisconnection = 1006,
44 	CloseCodeWrongDatatype = 1007,
45 	CloseCodePolicyViolated = 1008,
46 	CloseCodeTooMuchData = 1009,
47 	CloseCodeMissingExtension = 1010,
48 	CloseCodeBadOperation = 1011,
49 	CloseCodeTlsHandshakeFailed = 1015
50 }
51 /**
52  * websocket frame
53 */
54 struct WSFrame
55 {
56 	
57 	bool isControlFrame() const nothrow
58 	{
59 		return (_opCode & 0x08) == 0x08;
60 	}
61 	
62 	bool isDataFrame() const nothrow
63 	{
64 		return !isControlFrame();
65 	}
66 	
67 	bool isContinuationFrame() const nothrow
68 	{
69 		return isDataFrame() && (opCode == OpCode.OpCodeContinue);
70 	}
71 	
72 	@property opCode() const nothrow
73 	{ 
74 		return _opCode;
75 	}
76 
77 	@property parentCode() nothrow
78 	{
79 		return _lastCode;
80 	}
81 	
82 	@property isFinalFrame() const nothrow
83 	{
84 		return _isFinalFrame;
85 	}
86 	
87 	@property closeCode() const nothrow
88 	{
89 		return _closeCode;
90 	}
91 	
92 	@property closeReason() nothrow
93 	{
94 		return _closeReason;
95 	}
96 	
97 	@property rsv1() const nothrow
98 	{
99 		return _rsv1;
100 	}
101 	
102 	@property rsv2() const nothrow
103 	{
104 		return _rsv2;
105 	}
106 	
107 	@property rsv3() const nothrow
108 	{ 
109 		return _rsv3;
110 	}
111 	
112 	@property isValid() const nothrow
113 	{
114 		return _isValid;
115 	}
116 	
117 	ubyte[] data;
118 
119 package (collie.codec.http.codec):  
120 	
121 	bool _isFinalFrame;
122 	bool _rsv1;
123 	bool _rsv2;
124 	bool _rsv3;
125 	bool _isValid = false;
126 	OpCode _opCode;
127 	OpCode _lastCode;
128 	string _closeReason;
129 	CloseCode _closeCode;
130 }