Skip to content

Commit

Permalink
Introduced method sendFragmentedFrame() to send fragmented frames more
Browse files Browse the repository at this point in the history
convenient.
  • Loading branch information
Davidiusdadi committed Aug 8, 2013
1 parent 4e27fa8 commit d4d99ed
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/main/java/org/java_websocket/WebSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import org.java_websocket.drafts.Draft;
import org.java_websocket.framing.Framedata;
import org.java_websocket.framing.Framedata.Opcode;

public interface WebSocket {
public enum Role {
Expand Down Expand Up @@ -62,6 +63,21 @@ public enum READYSTATE {

public abstract void sendFrame( Framedata framedata );

/**
* Allows to send continuous/fragmented frames conveniently. <br>
* For more into on this frame type see http://tools.ietf.org/html/rfc6455#section-5.4<br>
*
* If the first frame you send is also the last then it is not a fragmented frame and will received via onMessage instead of onFragmented even though it was send by this method.
*
* @param op
* This is only important for the first frame in the sequence. Opcode.TEXT, Opcode.BINARY are allowed.
* @param buffer
* The buffer which contains the payload. It may have no bytes remaining.
* @param fin
* true means the current frame is the last in the sequence.
**/
public abstract void sendFragmentedFrame( Opcode op, ByteBuffer buffer, boolean fin );

public abstract boolean hasBufferedData();

/**
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/java_websocket/WebSocketImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,11 @@ private void send( Collection<Framedata> frames ) {
}
}

@Override
public void sendFragmentedFrame( Opcode op, ByteBuffer buffer, boolean fin ) {
send( draft.continuousFrame( op, buffer, fin ) );
}

@Override
public void sendFrame( Framedata framedata ) {
if( DEBUG )
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/org/java_websocket/drafts/Draft.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
import org.java_websocket.exceptions.InvalidHandshakeException;
import org.java_websocket.exceptions.LimitExedeedException;
import org.java_websocket.framing.CloseFrame;
import org.java_websocket.framing.FrameBuilder;
import org.java_websocket.framing.Framedata;
import org.java_websocket.framing.Framedata.Opcode;
import org.java_websocket.framing.FramedataImpl1;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.handshake.ClientHandshakeBuilder;
import org.java_websocket.handshake.HandshakeBuilder;
Expand Down Expand Up @@ -46,6 +49,8 @@ public enum CloseHandshakeType {
/** In some cases the handshake will be parsed different depending on whether */
protected Role role = null;

protected Opcode continuousFrameType = null;

public static ByteBuffer readLine( ByteBuffer buf ) {
ByteBuffer sbuf = ByteBuffer.allocate( buf.remaining() );
byte prev = '0';
Expand Down Expand Up @@ -123,6 +128,34 @@ protected boolean basicAccept( Handshakedata handshakedata ) {

public abstract List<Framedata> createFrames( String text, boolean mask );

public List<Framedata> continuousFrame( Opcode op, ByteBuffer buffer, boolean fin ) {
if( op != Opcode.BINARY && op != Opcode.TEXT && op != Opcode.TEXT ) {
throw new IllegalArgumentException( "Only Opcode.BINARY or Opcode.TEXT are allowed" );
}

if( continuousFrameType != null ) {
continuousFrameType = Opcode.CONTINUOUS;
} else if( fin ) {
throw new IllegalArgumentException( "There is no continious frame to continue" );
} else {
continuousFrameType = op;
}

FrameBuilder bui = new FramedataImpl1( continuousFrameType );
try {
bui.setPayload( buffer );
} catch ( InvalidDataException e ) {
throw new RuntimeException( e ); // can only happen when one builds close frames(Opcode.Close)
}
bui.setFin( fin );
if( fin ) {
continuousFrameType = null;
} else {
continuousFrameType = op;
}
return Collections.singletonList( (Framedata) bui );
}

public abstract void reset();

public List<ByteBuffer> createHandshake( Handshakedata handshakedata, Role ownrole ) {
Expand Down

0 comments on commit d4d99ed

Please sign in to comment.