Skip to content

Commit

Permalink
Added SCTP Codec Handlers + minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jestan committed Sep 23, 2012
1 parent bf22173 commit b268f0b
Show file tree
Hide file tree
Showing 12 changed files with 243 additions and 149 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2012 The Netty Project
*
* The Netty Project licenses this file to you 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 io.netty.handler.codec.sctp;

import com.sun.nio.sctp.MessageInfo;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.socket.SctpMessage;
import io.netty.handler.codec.MessageToMessageDecoder;

public class SctpMessageDecoder extends MessageToMessageDecoder<SctpMessage, ByteBuf> {
private ByteBuf cumulation = Unpooled.EMPTY_BUFFER;

@Override
public ByteBuf decode(ChannelHandlerContext ctx, SctpMessage msg) throws Exception {
ByteBuf byteBuf = cumulation = Unpooled.wrappedBuffer(cumulation, msg.getPayloadBuffer());
if (msg.isComplete()) {
cumulation = Unpooled.EMPTY_BUFFER;
return byteBuf;
} else {
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2012 The Netty Project
*
* The Netty Project licenses this file to you 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 io.netty.handler.codec.sctp;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.MessageBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundMessageHandlerAdapter;
import io.netty.channel.socket.SctpMessage;
import io.netty.handler.codec.EncoderException;

public class SctpMessageEncoder extends ChannelOutboundMessageHandlerAdapter<ByteBuf> {
private final int streamIdentifier;
private final int protocolIdentifier;

public SctpMessageEncoder(int streamIdentifier, int protocolIdentifier) {
this.streamIdentifier = streamIdentifier;
this.protocolIdentifier = protocolIdentifier;
}

@Override
public void flush(ChannelHandlerContext ctx, ChannelFuture future) throws Exception {
ByteBuf in = ctx.outboundByteBuffer();

try {
MessageBuf<Object> out = ctx.nextOutboundMessageBuffer();
ByteBuf payload = Unpooled.buffer(in.readableBytes());
payload.writeBytes(in);
out.add(new SctpMessage(streamIdentifier, protocolIdentifier, payload));
in.discardReadBytes();
} catch (Throwable t) {
ctx.fireExceptionCaught(new EncoderException(t));
}

ctx.flush(future);
}
}
23 changes: 23 additions & 0 deletions codec/src/main/java/io/netty/handler/codec/sctp/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2012 The Netty Project
*
* The Netty Project licenses this file to you 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.
*/

/**
* Encoder and decoder which transform a {@link io.netty.channel.socket.SctpMessage} into a
* {@link io.netty.buffer.ByteBuf} and vice versa.
*
* @apiviz.exclude \.oneone\.
*/
package io.netty.handler.codec.sctp;
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,16 @@ public void initChannel(SctpChannel ch) throws Exception {

public static void main(String[] args) throws Exception {
// Print usage if no argument is specified.
if (args.length < 2 || args.length > 3) {
/* if (args.length < 2 || args.length > 3) {
System.err.println(
"Usage: " + OioSctpEchoClient.class.getSimpleName() +
" <host> <port> [<first message size>]");
return;
}
}*/

// Parse options.
final String host = "localhost";
final int port = Integer.parseInt(args[1]);
final int port = 2556;
final int firstMessageSize;
if (args.length == 3) {
firstMessageSize = Integer.parseInt(args[2]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundMessageHandlerAdapter;
import io.netty.channel.socket.SctpData;
import io.netty.channel.socket.SctpMessage;

import java.util.logging.Level;
Expand Down Expand Up @@ -53,16 +52,14 @@ public SctpEchoClientHandler(int firstMessageSize) {

@Override
public void channelActive(ChannelHandlerContext ctx) {
ctx.write(new SctpData(0, 0, firstMessage));
ctx.write(new SctpMessage(0, 0, firstMessage));
}

@Override
public void messageReceived(ChannelHandlerContext ctx, SctpMessage msg) throws Exception {
if (msg instanceof SctpData) {
MessageBuf<Object> out = ctx.nextOutboundMessageBuffer();
out.add(msg);
ctx.flush();
}
MessageBuf<Object> out = ctx.nextOutboundMessageBuffer();
out.add(msg);
ctx.flush();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundMessageHandlerAdapter;
import io.netty.channel.socket.SctpData;
import io.netty.channel.socket.SctpMessage;

import java.util.logging.Level;
Expand All @@ -43,10 +42,8 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {

@Override
public void messageReceived(ChannelHandlerContext ctx, SctpMessage msg) throws Exception {
if (msg instanceof SctpData) {
MessageBuf<Object> out = ctx.nextOutboundMessageBuffer();
out.add(msg);
ctx.flush();
}
MessageBuf<Object> out = ctx.nextOutboundMessageBuffer();
out.add(msg);
ctx.flush();
}
}
120 changes: 0 additions & 120 deletions transport/src/main/java/io/netty/channel/socket/SctpData.java

This file was deleted.

Loading

0 comments on commit b268f0b

Please sign in to comment.