swim.ws.WsDecoder Maven / Gradle / Ivy
// Copyright 2015-2019 SWIM.AI inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package swim.ws;
import swim.codec.Binary;
import swim.codec.Decoder;
import swim.codec.DecoderException;
import swim.codec.InputBuffer;
import swim.structure.Data;
public abstract class WsDecoder {
public WsFrame fragment(WsOpcode opcode, Decoder content) {
return new WsFragment(opcode, content);
}
public WsFrame message(T value) {
return new WsValue(value);
}
public WsFrame control(WsOpcode opcode, P payload) {
switch (opcode) {
case CLOSE: return close(payload);
case PING: return ping(payload);
case PONG: return pong(payload);
default: throw new IllegalArgumentException(opcode.toString());
}
}
@SuppressWarnings("unchecked")
public WsFrame close(P payload) {
return (WsFrame) WsClose.from(payload);
}
@SuppressWarnings("unchecked")
public WsFrame ping(P payload) {
return (WsFrame) WsPing.from(payload);
}
@SuppressWarnings("unchecked")
public WsFrame pong(P payload) {
return (WsFrame) WsPong.from(payload);
}
public Decoder continuationDecoder(Decoder content) {
return content;
}
public Decoder textDecoder(Decoder content) {
return content.fork(WsOpcode.TEXT);
}
public Decoder binaryDecoder(Decoder content) {
return content.fork(WsOpcode.BINARY);
}
public Decoder> closeDecoder(Decoder content) {
return WsStatus.decoder();
}
public Decoder> pingDecoder(Decoder content) {
return Binary.outputParser(Data.output());
}
public Decoder> pongDecoder(Decoder content) {
return Binary.outputParser(Data.output());
}
public Decoder> frameDecoder(Decoder content) {
return new WsOpcodeDecoder(this, content);
}
public Decoder> decodeFrame(Decoder content, InputBuffer input) {
return WsOpcodeDecoder.decode(input, this, content);
}
public Decoder> decodeFrame(int finRsvOp, Decoder content, InputBuffer input) {
final int opcode = finRsvOp & 0xf;
switch (opcode) {
case 0x0: return decodeContinuationFrame(finRsvOp, continuationDecoder(content), input);
case 0x1: return decodeTextFrame(finRsvOp, textDecoder(content), input);
case 0x2: return decodeBinaryFrame(finRsvOp, binaryDecoder(content), input);
case 0x8: return decodeCloseFrame(finRsvOp, closeDecoder(content), input);
case 0x9: return decodePingFrame(finRsvOp, pingDecoder(content), input);
case 0xa: return decodePongFrame(finRsvOp, pongDecoder(content), input);
default: return Decoder.error(new DecoderException("reserved opcode: " + WsOpcode.from(opcode)));
}
}
public Decoder> decodeContinuationFrame(int finRsvOp, Decoder content, InputBuffer input) {
return WsFrameDecoder.decode(input, this, content);
}
public Decoder> decodeTextFrame(int finRsvOp, Decoder content, InputBuffer input) {
return WsFrameDecoder.decode(input, this, content);
}
public Decoder> decodeBinaryFrame(int finRsvOp, Decoder content, InputBuffer input) {
return WsFrameDecoder.decode(input, this, content);
}
@SuppressWarnings("unchecked")
public Decoder> decodeCloseFrame(int finRsvOp, Decoder content, InputBuffer input) {
return (Decoder>) (Decoder>) WsFrameDecoder.decode(input, this, content);
}
@SuppressWarnings("unchecked")
public Decoder> decodePingFrame(int finRsvOp, Decoder content, InputBuffer input) {
return (Decoder>) (Decoder>) WsFrameDecoder.decode(input, this, content);
}
@SuppressWarnings("unchecked")
public Decoder> decodePongFrame(int finRsvOp, Decoder content, InputBuffer input) {
return (Decoder>) (Decoder>) WsFrameDecoder.decode(input, this, content);
}
}