7
7
import java .util .Random ;
8
8
9
9
import org .java_websocket .exceptions .InvalidDataException ;
10
+ import org .java_websocket .exceptions .InvalidFrameException ;
10
11
import org .java_websocket .exceptions .InvalidHandshakeException ;
12
+ import org .java_websocket .exceptions .LimitExedeedException ;
11
13
import org .java_websocket .exceptions .NotSendableException ;
12
14
import org .java_websocket .framing .CloseFrame ;
13
15
import org .java_websocket .framing .FrameBuilder ;
@@ -40,14 +42,13 @@ public class Draft_75 extends Draft {
40
42
*/
41
43
public static final byte END_OF_FRAME = (byte ) 0xFF ;
42
44
45
+ /** Is only used to detect protocol violations */
43
46
protected boolean readingState = false ;
44
- private boolean inframe = false ;
47
+
45
48
protected List <Framedata > readyframes = new LinkedList <Framedata >();
46
49
protected ByteBuffer currentFrame ;
47
-
48
-
50
+
49
51
private final Random reuseableRandom = new Random ();
50
-
51
52
52
53
@ Override
53
54
public HandshakeState acceptHandshakeAsClient ( ClientHandshake request , ServerHandshake response ) {
@@ -122,29 +123,29 @@ public HandshakeBuilder postProcessHandshakeResponseAsServer( ClientHandshake re
122
123
}
123
124
124
125
protected List <Framedata > translateRegularFrame ( ByteBuffer buffer ) throws InvalidDataException {
126
+
125
127
while ( buffer .hasRemaining () ) {
126
128
byte newestByte = buffer .get ();
127
129
if ( newestByte == START_OF_FRAME ) { // Beginning of Frame
128
130
if ( readingState )
129
- return null ;
131
+ throw new InvalidFrameException ( "unexpected START_OF_FRAME" ) ;
130
132
readingState = true ;
131
133
} else if ( newestByte == END_OF_FRAME ) { // End of Frame
132
134
if ( !readingState )
133
- return null ;
135
+ throw new InvalidFrameException ( "unexpected END_OF_FRAME" ) ;
134
136
// currentFrame will be null if END_OF_FRAME was send directly after
135
137
// START_OF_FRAME, thus we will send 'null' as the sent message.
136
138
if ( this .currentFrame != null ) {
137
139
currentFrame .flip ();
138
140
FramedataImpl1 curframe = new FramedataImpl1 ();
139
141
curframe .setPayload ( currentFrame );
140
142
curframe .setFin ( true );
141
- curframe .setOptcode ( inframe ? Opcode . CONTINUOUS : Opcode .TEXT );
143
+ curframe .setOptcode ( Opcode .TEXT );
142
144
readyframes .add ( curframe );
143
145
this .currentFrame = null ;
144
146
buffer .mark ();
145
147
}
146
148
readingState = false ;
147
- inframe = false ;
148
149
} else if ( readingState ) { // Regular frame data, add to current frame buffer //TODO This code is very expensive and slow
149
150
if ( currentFrame == null ) {
150
151
currentFrame = createBuffer ();
@@ -156,15 +157,11 @@ protected List<Framedata> translateRegularFrame( ByteBuffer buffer ) throws Inva
156
157
return null ;
157
158
}
158
159
}
159
- if ( readingState ) {
160
- FramedataImpl1 curframe = new FramedataImpl1 ();
161
- currentFrame .flip ();
162
- curframe .setPayload ( currentFrame );
163
- curframe .setFin ( false );
164
- curframe .setOptcode ( inframe ? Opcode .CONTINUOUS : Opcode .TEXT );
165
- inframe = true ;
166
- readyframes .add ( curframe );
167
- }
160
+
161
+ // if no error occurred this block will be reached
162
+ /*if( readingState ) {
163
+ checkAlloc(currentFrame.position()+1);
164
+ }*/
168
165
169
166
List <Framedata > frames = readyframes ;
170
167
readyframes = new LinkedList <Framedata >();
@@ -196,9 +193,9 @@ public ByteBuffer createBuffer() {
196
193
return ByteBuffer .allocate ( INITIAL_FAMESIZE );
197
194
}
198
195
199
- public ByteBuffer increaseBuffer ( ByteBuffer full ) {
196
+ public ByteBuffer increaseBuffer ( ByteBuffer full ) throws LimitExedeedException , InvalidDataException {
200
197
full .flip ();
201
- ByteBuffer newbuffer = ByteBuffer .allocate ( full .capacity () * 2 );
198
+ ByteBuffer newbuffer = ByteBuffer .allocate ( checkAlloc ( full .capacity () * 2 ) );
202
199
newbuffer .put ( full );
203
200
return newbuffer ;
204
201
}
0 commit comments