All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gateway.connector.tcp.TcpConnection Maven / Gradle / Ivy

 
package com.gateway.connector.tcp;

import com.gateway.connector.api.ExchangeConnection;
import com.gateway.utils.NetUtils;
import com.gateway.exception.LostConnectException;
import com.gateway.exception.PushException;

import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;

import java.net.InetSocketAddress;
import java.net.SocketAddress;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TcpConnection extends ExchangeConnection {

    private final static Logger logger = LoggerFactory.getLogger(TcpConnection.class);

    private ChannelHandlerContext cxt;

    public TcpConnection(ChannelHandlerContext cxt) {
        this.cxt = cxt;
    }

    public void connect() {
    }

    public void close() {
        this.close = true;

        cxt.close();
        logger.debug("the connection have been destroyed! ctx -> " + cxt.toString());
    }

    public void send(T message) {
        if (message == null)
            return;

        sendMessage(message);
    }
    public boolean isWritable()   {
       return cxt.channel().isWritable();
    }
    private void sendMessage(T message) {
        if (isClosed()) {
            PushException e = new PushException("Use a closed pushSocked!");
            this.fireError(e);
            return;
        }
        pushMessage0(message);
    }
    private String getRemoteAddress(ChannelHandlerContext ctx) {
        SocketAddress remote1 = ctx.channel().remoteAddress();
        InetSocketAddress remote = (InetSocketAddress) remote1;
        return NetUtils.toAddressString(remote);
    }
    private void pushMessage0(T message) {
        try {
        		 cxt.writeAndFlush(message);
        		 session.access();
        } catch (LostConnectException e) {
            logger.error("TcpConnection pushMessage occur LostConnectException.", e);
            this.fireError(new PushException(e));
        } catch (Exception e) {
            logger.error("TcpConnection pushMessage occur Exception.", e);
            this.fireError(new PushException("ChannelFuture " + connectionId + " ", e));
        } catch (Throwable e) {
            logger.error("TcpConnection pushMessage occur Throwable.", e);
            this.fireError(new PushException("Failed to send message, cause: " + e.getMessage(), e));
        }
    }

    private void pushMessage(T message) {
        boolean success = true;
        boolean sent = true;
        int timeout = 60;
        try {
            ChannelFuture cf = cxt.write(message);
            cxt.flush();
            if (sent) {
                success = cf.await(timeout);
            }
            if (cf.isSuccess()) {
                logger.debug("send success.");
            }
            Throwable cause = cf.cause();
            if (cause != null) {
                this.fireError(new PushException(cause));
            }
        } catch (LostConnectException e) {
            logger.error("TcpConnection pushMessage occur LostConnectException.", e);
            this.fireError(new PushException(e));
        } catch (Exception e) {
            logger.error("TcpConnection pushMessage occur Exception.", e);
            this.fireError(new PushException("ChannelFuture " + connectionId + " ", e));
        } catch (Throwable e) {
            logger.error("TcpConnection pushMessage occur Throwable.", e);
            this.fireError(new PushException("Failed to send message, cause: " + e.getMessage(), e));
        }
        if (!success) {
            this.fireError(new PushException("Failed to send message, in timeout(" + timeout + "ms) limit"));
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy