Пример #1
0
        // Server Ctor
        protected TcpChannel(Socket socket, TcpChannelEventHandler <TcpChannelNotifyEventArgs> onNotify,
                             int socketReceiveTimeoutInSec, int socketSendTimeoutInSec,
                             int socketReceiveBufferSize, int socketSendBufferSize)
            : this(socketReceiveTimeoutInSec, socketSendTimeoutInSec, socketReceiveBufferSize, socketSendBufferSize)
        {
            this.socket = socket;

            SetSocketParams();

            RemoteEP = this.socket.RemoteEndPoint as IPEndPoint;
            Id       = RemoteEP.ToString();
        }
Пример #2
0
        // Client Ctor
        protected TcpChannel(TcpChannelEventHandler <TcpChannelReceivedEventArgs> onReceived,
                             string id, int socketReceiveTimeoutInSec, int socketSendTimeoutInSec,
                             int socketReceiveBufferSize, int socketSendBufferSize)
            : this(socketReceiveTimeoutInSec, socketSendTimeoutInSec, socketReceiveBufferSize, socketSendBufferSize)
        {
            Id = id;

            if (onReceived == null)
            {
                throw new Exception("onReceived is null.");
            }

            this.onReceived += onReceived;
        }
        public EasyTcpClient(TcpChannelEventHandler <TcpChannelReceivedEventArgs> onReceived,
                             string id = null,
                             int receiveTimeoutInSec  = DEFAULT_RECEIVE_TIMEOUT_IN_SEC,
                             int reconnectionAttempts = DEFAULT_RECONNECTION_ATTEMPTS,
                             int delayBetweenReconnectionAttemptsInSec = DEFAULT_RECONNECTION_DELAY_IN_SEC,
                             int socketReceiveTimeoutInSec             = DEFAULT_RECEIVE_TIMEOUT_IN_SEC,
                             int socketSendTimeoutInSec  = DEFAULT_SEND_TIMEOUT_IN_SEC,
                             int socketReceiveBufferSize = DEFAULT_RECEIVE_BUFFER_SIZE,
                             int socketSendBufferSize    = DEFAULT_SEND_BUFFER_SIZE) :
            base(onReceived, id, socketReceiveTimeoutInSec, socketSendTimeoutInSec, socketReceiveBufferSize, socketSendBufferSize)
        {
            dlgtReconnect = new ReconnectDelegate(Connect);

            this.m_ReconnectionAttempts = reconnectionAttempts;
            if (this.m_ReconnectionAttempts <= 0)
            {
                this.m_ReconnectionAttempts = 1;
            }

            this.m_DelayBetweenReconnectionAttempts = new TimeSpan(0, 0, delayBetweenReconnectionAttemptsInSec);
        }
Пример #4
0
        public static void StartAcceptSubscribersOnPort(int acceptPort,
                                                        TcpChannelEventHandler <TcpChannelReceivedEventArgs> onReceived,
                                                        TcpChannelEventHandler <EventArgs> onInitConnectionToServer         = null,
                                                        TcpChannelEventHandler <TcpChannelNotifyEventArgs> onServerNotifies = null,
                                                        int socketReceiveTimeoutInSec = DEFAULT_RECEIVE_TIMEOUT_IN_SEC,
                                                        int socketSendTimeoutInSec    = DEFAULT_SEND_TIMEOUT_IN_SEC,
                                                        int socketReceiveBufferSize   = DEFAULT_RECEIVE_BUFFER_SIZE,
                                                        int socketSendBufferSize      = DEFAULT_SEND_BUFFER_SIZE)
        {
            if (onReceived == null)
            {
                throw new Exception("onReceived is null.");
            }

            EasyTcpServer.onServerNotifies += onServerNotifies;
            string processName = Process.GetCurrentProcess().ProcessName;

            AutoResetEvent evAccept    = new AutoResetEvent(false);
            TcpListener    tcpListener = null;

            try
            {
                tcpListener = StartListenOnPort(acceptPort);
            }
            catch (Exception e)
            {
                if (EasyTcpServer.onServerNotifies != null)
                {
                    EasyTcpServer.onServerNotifies(null
                                                   , new TcpChannelNotifyEventArgs(EnumNotificationLevel.Error, "StartAcceptSubscribersOnPort", null, e));
                }
            }

            if (tcpListener != null)
            {
                WorkerThread wtAccept = new WorkerThread(e =>
                {
                    if (EasyTcpServer.onServerNotifies != null)
                    {
                        EasyTcpServer.onServerNotifies(null
                                                       , new TcpChannelNotifyEventArgs(EnumNotificationLevel.Error, "StartAcceptSubscribersOnPort, Accept Worker Thread", null, e));
                    }
                });

                cdctThread[tcpListener] = wtAccept;

                wtAccept.Start(() =>
                {
                    AcceptBegin(tcpListener, evAccept,
                                tcpServer =>
                    {
                        if (tcpServer.IsSocketConnected)
                        {
                            tcpServer.onReceived += onReceived;
                            tcpServer.Receive();

                            if (onInitConnectionToServer != null)
                            {
                                onInitConnectionToServer(tcpServer, null);
                            }
                        }
                    },
                                ex =>
                    {
                        if (EasyTcpServer.onServerNotifies != null)
                        {
                            EasyTcpServer.onServerNotifies(null
                                                           , new TcpChannelNotifyEventArgs(EnumNotificationLevel.Error, "StartAcceptSubscribersOnPort.AcceptBegin, AcceptEnd", null, ex));
                        }
                    },
                                socketReceiveTimeoutInSec, socketSendTimeoutInSec, socketReceiveBufferSize, socketSendBufferSize);

                    evAccept.WaitOne();

                    wtAccept.SetEvent();
                });

                wtAccept.SetEvent();
            }
        }
Пример #5
0
 internal EasyTcpServer(Socket socket, TcpChannelEventHandler <TcpChannelNotifyEventArgs> onNotify,
                        int socketReceiveTimeoutInSec, int socketSendTimeoutInSec, int socketReceiveBufferSize, int socketSendBufferSize) :
     base(socket, onNotify, socketReceiveTimeoutInSec, socketSendTimeoutInSec, socketReceiveBufferSize, socketSendBufferSize)
 {
 }