1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.eclipse.jetty.websocket;
15
16 import org.eclipse.jetty.io.Buffer;
17 import org.eclipse.jetty.util.Utf8StringBuilder;
18 import org.eclipse.jetty.util.log.Log;
19
20 final class FrameHandlerD1 implements WebSocketParser.FrameHandler
21 {
22 public final static byte PING=1;
23 public final static byte PONG=1;
24
25 final WebSocketConnectionD00 _connection;
26 final WebSocket _websocket;
27 final Utf8StringBuilder _utf8 = new Utf8StringBuilder();
28 boolean _fragmented=false;
29
30 FrameHandlerD1(WebSocketConnectionD00 connection, WebSocket websocket)
31 {
32 _connection=connection;
33 _websocket=websocket;
34 }
35
36 public void onFrame(boolean more, byte flags, byte opcode, Buffer buffer)
37 {
38 try
39 {
40 byte[] array=buffer.array();
41
42 if (opcode==0)
43 {
44 if (more)
45 {
46 _utf8.append(buffer.array(),buffer.getIndex(),buffer.length());
47 _fragmented=true;
48 }
49 else if (_fragmented)
50 {
51 _utf8.append(buffer.array(),buffer.getIndex(),buffer.length());
52 _websocket.onMessage(opcode,_utf8.toString());
53 _utf8.reset();
54 _fragmented=false;
55 }
56 else
57 {
58 _websocket.onMessage(opcode,buffer.toString("utf-8"));
59 }
60 }
61 else if (opcode==PING)
62 {
63 _connection.sendMessage(PONG,buffer.array(),buffer.getIndex(),buffer.length());
64 }
65 else if (opcode==PONG)
66 {
67
68 }
69 else
70 {
71 if (more)
72 {
73 _websocket.onFragment(true,opcode,array,buffer.getIndex(),buffer.length());
74 }
75 else if (_fragmented)
76 {
77 _websocket.onFragment(false,opcode,array,buffer.getIndex(),buffer.length());
78 }
79 else
80 {
81 _websocket.onMessage(opcode,array,buffer.getIndex(),buffer.length());
82 }
83 }
84 }
85 catch(ThreadDeath th)
86 {
87 throw th;
88 }
89 catch(Throwable th)
90 {
91 Log.warn(th);
92 }
93 }
94 }