Search in sources :

Example 1 with CmppConnectResponseMessage

use of com.zx.sms.codec.cmpp.msg.CmppConnectResponseMessage in project SMSGate by Lihuanghe.

the class SessionLoginManager method doLoginSuccess.

@Override
protected void doLoginSuccess(ChannelHandlerContext ctx, EndpointEntity entity, Object msg) {
    CMPPServerChildEndpointEntity childentity = (CMPPServerChildEndpointEntity) entity;
    CmppConnectRequestMessage message = (CmppConnectRequestMessage) msg;
    // channelHandler已绑定完成,给客户端发resp.
    CmppConnectResponseMessage resp = new CmppConnectResponseMessage(message.getHeader().getSequenceId());
    resp.setVersion(childentity.getVersion());
    resp.setStatus(0);
    resp.setAuthenticatorISMG(DigestUtils.md5(Bytes.concat(Ints.toByteArray((int) resp.getStatus()), message.getAuthenticatorSource(), childentity.getPassword().getBytes(childentity.getChartset()))));
    ctx.channel().writeAndFlush(resp);
}
Also used : CMPPServerChildEndpointEntity(com.zx.sms.connect.manager.cmpp.CMPPServerChildEndpointEntity) CmppConnectRequestMessage(com.zx.sms.codec.cmpp.msg.CmppConnectRequestMessage) CmppConnectResponseMessage(com.zx.sms.codec.cmpp.msg.CmppConnectResponseMessage)

Example 2 with CmppConnectResponseMessage

use of com.zx.sms.codec.cmpp.msg.CmppConnectResponseMessage in project SMSGate by Lihuanghe.

the class CmppConnectResponseMessageCodec method decode.

@Override
protected void decode(ChannelHandlerContext ctx, Message msg, List<Object> out) throws Exception {
    int commandId = msg.getHeader().getCommandId();
    if (commandId != packetType.getCommandId()) {
        // 不解析,交给下一个codec
        out.add(msg);
        return;
    }
    CmppConnectResponseMessage responseMessage = new CmppConnectResponseMessage(msg.getHeader());
    byte[] body = msg.getBodyBuffer();
    if (body.length == 21) {
        ByteBuf bodyBuffer = Unpooled.wrappedBuffer(msg.getBodyBuffer());
        responseMessage.setStatus(bodyBuffer.readUnsignedInt());
        responseMessage.setAuthenticatorISMG(toArray(bodyBuffer, CmppConnectResponse.AUTHENTICATORISMG.getLength()));
        responseMessage.setVersion(bodyBuffer.readUnsignedByte());
        ReferenceCountUtil.release(bodyBuffer);
        out.add(responseMessage);
    } else {
        if (body.length == 18) {
            ByteBuf bodyBuffer = Unpooled.wrappedBuffer(body);
            responseMessage.setStatus(bodyBuffer.readUnsignedByte());
            responseMessage.setAuthenticatorISMG(toArray(bodyBuffer, Cmpp20ConnectResponse.AUTHENTICATORISMG.getLength()));
            responseMessage.setVersion(bodyBuffer.readUnsignedByte());
            ReferenceCountUtil.release(bodyBuffer);
            out.add(responseMessage);
            logger.warn("error CmppConnectResponseMessage body length. dump:{}", ByteBufUtil.hexDump(body));
        } else {
            throw new NotSupportedException("error cmpp CmppConnectResponseMessage data .");
        }
    }
}
Also used : CmppConnectResponseMessage(com.zx.sms.codec.cmpp.msg.CmppConnectResponseMessage) ByteBuf(io.netty.buffer.ByteBuf) NotSupportedException(com.zx.sms.common.NotSupportedException)

Example 3 with CmppConnectResponseMessage

use of com.zx.sms.codec.cmpp.msg.CmppConnectResponseMessage in project SMSGate by Lihuanghe.

the class Cmpp20ConnectResponseMessageCodec method decode.

@Override
protected void decode(ChannelHandlerContext ctx, Message msg, List<Object> out) throws Exception {
    int commandId = msg.getHeader().getCommandId();
    if (commandId != packetType.getCommandId()) {
        // 不解析,交给下一个codec
        out.add(msg);
        return;
    }
    CmppConnectResponseMessage responseMessage = new CmppConnectResponseMessage(msg.getHeader());
    byte[] body = msg.getBodyBuffer();
    if (body.length == 18) {
        ByteBuf bodyBuffer = Unpooled.wrappedBuffer(body);
        responseMessage.setStatus(bodyBuffer.readUnsignedByte());
        responseMessage.setAuthenticatorISMG(toArray(bodyBuffer, Cmpp20ConnectResponse.AUTHENTICATORISMG.getLength()));
        responseMessage.setVersion(bodyBuffer.readUnsignedByte());
        ReferenceCountUtil.release(bodyBuffer);
        out.add(responseMessage);
    } else {
        if (body.length == 21) {
            ByteBuf bodyBuffer = Unpooled.wrappedBuffer(body);
            responseMessage.setStatus(bodyBuffer.readUnsignedInt());
            responseMessage.setAuthenticatorISMG(toArray(bodyBuffer, CmppConnectResponse.AUTHENTICATORISMG.getLength()));
            responseMessage.setVersion(bodyBuffer.readUnsignedByte());
            ReferenceCountUtil.release(bodyBuffer);
            out.add(responseMessage);
            logger.warn("error Cmpp20ConnectResponseMessageCodec body length. dump:{}", ByteBufUtil.hexDump(body));
        } else {
            throw new NotSupportedException("error cmpp CmppConnectResponseMessage data .");
        }
    }
}
Also used : CmppConnectResponseMessage(com.zx.sms.codec.cmpp.msg.CmppConnectResponseMessage) ByteBuf(io.netty.buffer.ByteBuf) NotSupportedException(com.zx.sms.common.NotSupportedException)

Example 4 with CmppConnectResponseMessage

use of com.zx.sms.codec.cmpp.msg.CmppConnectResponseMessage in project SMSGate by Lihuanghe.

the class SessionLoginManager method failedLogin.

@Override
protected /**
 * 状态 0:正确 1:消息结构错 2:非法源地址 3:认证错 4:版本太高 5~ :其他错误
 */
void failedLogin(ChannelHandlerContext ctx, Object msg, long status) {
    if (msg instanceof CmppConnectRequestMessage) {
        logger.error("Connected error status :{},msg : {}", status, msg);
        CmppConnectRequestMessage message = (CmppConnectRequestMessage) msg;
        // 认证失败
        CmppConnectResponseMessage resp = new CmppConnectResponseMessage(message.getHeader().getSequenceId());
        resp.setAuthenticatorISMG(new byte[16]);
        resp.setStatus(status);
        ChannelFuture promise = ctx.writeAndFlush(resp);
        final ChannelHandlerContext finalctx = ctx;
        promise.addListener(new GenericFutureListener() {

            public void operationComplete(Future future) throws Exception {
                finalctx.close();
            }
        });
    } else {
        logger.error("connect msg type error : {}", msg);
        ctx.close();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelFuture(io.netty.channel.ChannelFuture) Future(io.netty.util.concurrent.Future) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CmppConnectRequestMessage(com.zx.sms.codec.cmpp.msg.CmppConnectRequestMessage) CmppConnectResponseMessage(com.zx.sms.codec.cmpp.msg.CmppConnectResponseMessage) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener)

Example 5 with CmppConnectResponseMessage

use of com.zx.sms.codec.cmpp.msg.CmppConnectResponseMessage in project SMSGate by Lihuanghe.

the class TestCmppConnectResponseMessageCodec method testCode.

@Test
public void testCode() {
    CmppConnectResponseMessage msg = new CmppConnectResponseMessage(238);
    // 长度为16
    msg.setAuthenticatorISMG("passwordpassword".getBytes());
    ByteBuf buf = encode(msg);
    ByteBuf copybuf = buf.copy();
    int length = buf.readableBytes();
    int expectLength = (getVersion() == 0x30 ? CmppConnectResponse.AUTHENTICATORISMG.getBodyLength() : Cmpp20ConnectResponse.AUTHENTICATORISMG.getBodyLength()) + CmppHead.COMMANDID.getHeadLength();
    Assert.assertEquals(expectLength, length);
    Assert.assertEquals(expectLength, buf.readInt());
    Assert.assertEquals(msg.getPacketType().getCommandId(), buf.readInt());
    Assert.assertEquals(msg.getHeader().getSequenceId(), buf.readInt());
    CmppConnectResponseMessage result = decode(copybuf);
    Assert.assertEquals(msg.getHeader().getSequenceId(), result.getHeader().getSequenceId());
    Assert.assertArrayEquals(msg.getAuthenticatorISMG(), result.getAuthenticatorISMG());
}
Also used : CmppConnectResponseMessage(com.zx.sms.codec.cmpp.msg.CmppConnectResponseMessage) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Aggregations

CmppConnectResponseMessage (com.zx.sms.codec.cmpp.msg.CmppConnectResponseMessage)5 ByteBuf (io.netty.buffer.ByteBuf)3 CmppConnectRequestMessage (com.zx.sms.codec.cmpp.msg.CmppConnectRequestMessage)2 NotSupportedException (com.zx.sms.common.NotSupportedException)2 CMPPServerChildEndpointEntity (com.zx.sms.connect.manager.cmpp.CMPPServerChildEndpointEntity)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 Future (io.netty.util.concurrent.Future)1 GenericFutureListener (io.netty.util.concurrent.GenericFutureListener)1 Test (org.junit.Test)1