7
7
import java .nio .charset .Charset ;
8
8
import java .security .NoSuchAlgorithmException ;
9
9
import java .util .ArrayList ;
10
- import java .util .HashMap ;
11
10
import java .util .List ;
11
+
12
+ import net .tootallnate .websocket .Framedata .Opcode ;
12
13
import net .tootallnate .websocket .drafts .*;
13
14
import net .tootallnate .websocket .exeptions .InvalidFrameException ;
14
15
import net .tootallnate .websocket .exeptions .InvalidHandshakeException ;
@@ -59,7 +60,7 @@ public enum Role{
59
60
*/
60
61
public static final byte END_OF_FRAME = (byte )0xFF ;
61
62
62
- public static final boolean DEBUG = true ;
63
+ public static final boolean DEBUG = false ;
63
64
64
65
65
66
// INSTANCE PROPERTIES /////////////////////////////////////////////////////
@@ -102,6 +103,8 @@ public enum Role{
102
103
private Handshakedata handshakerequest = null ;
103
104
104
105
public List <Draft > known_drafts ;
106
+
107
+ private final int maxpayloadsize ;
105
108
106
109
107
110
// CONSTRUCTOR /////////////////////////////////////////////////////////////
@@ -115,24 +118,26 @@ public enum Role{
115
118
* @param listener The {@link WebSocketListener} to notify of events when
116
119
* they occur.
117
120
*/
118
- WebSocket (SocketChannel socketChannel , BlockingQueue <ByteBuffer > bufferQueue , WebSocketListener listener , Draft draft ) {
121
+ WebSocket (SocketChannel socketChannel , BlockingQueue <ByteBuffer > bufferQueue , WebSocketListener listener , Draft draft , int maxpayloadsize ) {
119
122
this .socketChannel = socketChannel ;
120
123
this .bufferQueue = bufferQueue ;
121
124
this .handshakeComplete = false ;
122
125
this .socketBuffer = ByteBuffer .allocate (8192 );
123
126
this .wsl = listener ;
124
127
this .role = Role .CLIENT ;
125
128
this .draft = draft ;
129
+ this .maxpayloadsize = maxpayloadsize ;
126
130
}
127
131
128
- WebSocket (SocketChannel socketChannel , BlockingQueue <ByteBuffer > bufferQueue , WebSocketListener listener , List <Draft > drafts ) {
132
+ WebSocket (SocketChannel socketChannel , BlockingQueue <ByteBuffer > bufferQueue , WebSocketListener listener , List <Draft > drafts , int maxpayloadsize ) {
129
133
this .socketChannel = socketChannel ;
130
134
this .bufferQueue = bufferQueue ;
131
135
this .handshakeComplete = false ;
132
136
this .socketBuffer = ByteBuffer .allocate (8192 );
133
137
this .wsl = listener ;
134
138
this .role = Role .SERVER ;
135
139
this .draft = null ;
140
+ this .maxpayloadsize = maxpayloadsize ;
136
141
if ( known_drafts == null || known_drafts .isEmpty () ){
137
142
known_drafts = new ArrayList <Draft > ( 1 );
138
143
known_drafts .add ( new Draft_10 () );
@@ -158,9 +163,14 @@ void handleRead() throws IOException, NoSuchAlgorithmException {
158
163
bytesRead = this .socketChannel .read (this .socketBuffer );
159
164
160
165
161
- if (bytesRead == -1 ) {
166
+ if ( bytesRead == -1 ) {
162
167
close ();
163
- } else if (bytesRead > 0 ) {
168
+ }
169
+ else if ( bytesRead > maxpayloadsize ){
170
+ wsl .onError ( new RuntimeException ("recived packet to big" ) );
171
+ abort ( "recived packet to big" );
172
+ }
173
+ else if ( bytesRead > 0 ) {
164
174
if (DEBUG ) System .out .println ( "got: {" + new String ( socketBuffer .array () , 0 , bytesRead ) + "}" );
165
175
if ( !handshakeComplete ){
166
176
try {
@@ -199,17 +209,38 @@ else if( role == Role.CLIENT){
199
209
}
200
210
}
201
211
else {
202
- List <Framedata > frames = draft .translateFrame ( socketBuffer . array () , bytesRead );
212
+ List <Framedata > frames = draft .translateFrame ( socketBuffer , bytesRead );
203
213
for ( Framedata f : frames ){
214
+ Opcode curop = f .getOpcode ();
215
+ if ( curop == null )// Ignore undefined opcodes
216
+ continue ;
217
+ else if ( curop == Opcode .CLOSING ){
218
+ sendFrame ( new FramedataImpl1 ( Opcode .CLOSING ) );
219
+ close ();
220
+ }
221
+ else if ( curop == Opcode .PING ){
222
+ sendFrame ( new FramedataImpl1 ( Opcode .PONG ) );
223
+ }
224
+ else if ( curop == Opcode .PONG ){
225
+ wsl .onPong ();
226
+ }
204
227
if ( currentframe == null ){
205
228
if ( f .isFin () ){
206
- wsl .onMessage ( this , new String ( f .getPayloadData () , UTF8_CHARSET ) );
229
+ if ( f .getOpcode () == Opcode .TEXT ){
230
+ wsl .onMessage ( this , new String ( f .getPayloadData () , UTF8_CHARSET ) );
231
+ }
232
+ else if ( f .getOpcode () == Opcode .BINARY ){
233
+ wsl .onMessage ( this , f .getPayloadData () );
234
+ }
235
+ else {
236
+ if (DEBUG ) System .out .println ( "Ignoring frame:" + f .toString () );
237
+ }
207
238
}
208
239
else {
209
240
currentframe = f ;
210
241
}
211
242
}
212
- else {
243
+ else if ( f . getOpcode () == Opcode . CONTINIOUS ) {
213
244
try {
214
245
currentframe .append ( f );
215
246
} catch ( InvalidFrameException e ) {
0 commit comments