View Javadoc

1   // ========================================================================
2   // Copyright (c) 2010 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
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  }