Exemplo n.º 1
0
        protected void SubscribeCommunicationEvents(AbstractTcpCom tcpComm)
        {
            tcpComm.ConnectionEstablished += new EventHandler <ConnectionStateChangedEventArgs>(OnTcpComm_ConnectionEstablished);
            tcpComm.ConnectionClosed      += new EventHandler <ConnectionStateChangedEventArgs>(OnTcpComm_ConnectionClosed);

            tcpComm.RawMessageReceivedDelegate = OnRawMessageReceived;
        }
Exemplo n.º 2
0
        private void InitServiceInternal(int servicePort, TcpServiceCom service)
        {
            this.connectionType         = ConnectionType.Service;
            service.Logger              = this.logger;
            service.ReceiveBufferSize   = this.ReceiveBufferSize;
            service.SendBufferSize      = this.SendBufferSize;
            service.AdjustSocketHandler = this.AdjustSocketHandler;


            SubscribeCommunicationEvents(service);

            service.Init(servicePort);
            this.communication = service;

            if (LogDataStream)
            {
                dataStreamLogger.Init(this, "Service-" + servicePort, Config != null ? Config.Root : null);
            }

            string errMsg;

            if (this.communication.Connect(out errMsg))
            {
                logger.Info(string.Format("Tcp service established on port {0}", servicePort));
            }
            else
            {
                throw new InvalidOperationException(errMsg);
            }
        }
Exemplo n.º 3
0
        private void InitClientInternal(string host, int port, TcpClientCom client)
        {
            this.connectionType        = ConnectionType.Client;
            client.Logger              = this.logger;
            client.ReceiveBufferSize   = this.ReceiveBufferSize;
            client.SendBufferSize      = this.SendBufferSize;
            client.AdjustSocketHandler = this.AdjustSocketHandler;
            SubscribeCommunicationEvents(client);

            originalTarget = new TcpTarget()
            {
                Host = host, Port = port
            };

            client.Init(host, port);

            this.communication = client;

            if (LogDataStream)
            {
                dataStreamLogger.Init(this, host.Replace('.', '_') + "-" + port, Config != null ? Config.Root : null);
            }

            // async client connect
            Task.Run(StartAutoReconnectAsync);
        }
Exemplo n.º 4
0
        // ----------------------------------------------------------------------------------------
        #endregion

        #region Client constructors
        // ----------------------------------------------------------------------------------------
        // Client constructors
        // ----------------------------------------------------------------------------------------

        /// <summary>
        /// Initializes a new instance of the <see cref="Client"/> class.
        /// </summary>
        /// <param name="socket">The socket.</param>
        /// <param name="stream">The stream.</param>
        /// <param name="queueReceivedPackets">The queue received packets.</param>
        /// <param name="localEndPoint">The local end point.</param>
        /// <param name="remoteEndPoint">The remote end point.</param>
        /// <param name="logger">The logger.</param>
        public Client(Socket socket, Stream stream, ConcurrentQueue <IGenericMessage> queueReceivedPackets, EndPoint localEndPoint, EndPoint remoteEndPoint, ILogger logger)
        {
            this.logger               = logger;
            this.socket               = socket;
            this.stream               = stream;
            this.localEndPoint        = localEndPoint;
            this.remoteEndPoint       = remoteEndPoint;
            this.queueReceivedPackets = queueReceivedPackets;
            this.connectTimeUtc       = DateTime.UtcNow;
            this.connectionSessionId  = AbstractTcpCom.GetNewConnectionSessionId();
        }
Exemplo n.º 5
0
        /// <summary>
        /// Sends the generic message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="receiverSessionId">The receiver session id.</param>
        /// <param name="context">The context.</param>
        public void SendMessage(IGenericMessage message, int receiverSessionId, object context)
        {
            byte[] msgBytes = serializer.SerializeToBytes(message, context);
            byte[] encapsulatedMessageBytes = AbstractTcpCom.CreateMessage(serializer.MessageFormat, msgBytes);

            if (logDataStream)
            {
                dataStreamLogger.LogStreamMessage(receiverSessionId, false, msgBytes, serializer.MessageFormat != RawMessageFormat.JSON);
            }

            communication.Send(encapsulatedMessageBytes, receiverSessionId);
        }
Exemplo n.º 6
0
        public async ValueTask SendMessageAsync(IGenericMessage message, int receiverSessionId, object context)
        {
            byte[] msgBytes = serializer.SerializeToBytes(message, context);
            byte[] encapsulatedMessageBytes = AbstractTcpCom.CreateMessage(serializer.MessageFormat, msgBytes);

            if (logDataStream)
            {
                // todo: create async version
                dataStreamLogger.LogStreamMessage(receiverSessionId, false, msgBytes, serializer.MessageFormat != RawMessageFormat.JSON);
            }

            await communication.SendAsync(encapsulatedMessageBytes, receiverSessionId);
        }