예제 #1
0
        private void Init()
        {
            Scheme = ConnUri.Scheme.ToUpperInvariant();
            Console.WriteLine($"SCHEME: {Scheme} for {ConnUri.ToString()}");
            switch (Scheme)
            {
            case "RSERVE":
                Hostname = ConnUri.Host;
                break;

            case "FILE":
                Hostname  = "127.0.0.1";
                RTermPath = ConnUri.LocalPath;
                break;

            default:
                throw new Exception($"The uri scheme \"{ConnUri.Scheme}\" is unknown.");
            }

            IPAddress address = null;

            if (IPAddress.TryParse(Hostname, out address))
            {
                IpAddress    = address;
                UseIpAddress = true;
            }

            Credentials = null;
            if (!String.IsNullOrEmpty(User))
            {
                Credentials = new NetworkCredential(User, Password);
            }
        }
예제 #2
0
        public override void Init(ConnUri uri, bool persistent, string ID, int inactivityMS, int _sendGap = 0)
        {
            if ((uri == null || !uri.IsValid) && !tcpClientProvided)
            {
                return;
            }

            this.ID          = ID;
            messageQueu      = useCircular ? (ICommsQueue) new CircularByteBuffer4Comms(65536) : (ICommsQueue) new BlockingByteQueue();
            MINIMUM_SEND_GAP = _sendGap;
            RECEIVE_TIMEOUT  = inactivityMS;
            frameWrapper?.SetID(ID);
            State = STATE.STOP;

            CommsUri = uri ?? CommsUri;
            SetIPChunks(CommsUri.IP);

            if (!tcpClientProvided)
            {
                tcpEq    = new CommEquipmentObject <TcpClient>(ID, uri, null, persistent);
                tcpEq.ID = ID;
            }
            else
            {
                tcpEq.ID = ID;
            }
        }
예제 #3
0
        public override void Init(ConnUri uri, bool persistent, string ID, int inactivityMS, int _sendGap = 0)
        {
            if ((uri == null || !uri.IsValid) && !tcpClientProvided)
            {
                return;
            }

            messageCircularBuffer = new CircularByteBuffer4Comms(65536);

            MINIMUM_SEND_GAP = _sendGap;
            RECEIVE_TIMEOUT  = inactivityMS;
            frameWrapper?.SetID(ID);

            CommsUri = uri ?? CommsUri;
            SetIPChunks(CommsUri.IP);

            if (!tcpClientProvided)
            {
                tcpEq        = new CommEquipmentObject <TcpClient>(ID, uri, null, persistent);
                tcpEq.ID     = ID;
                receiverTask = new Task(Connect2EquipmentCallback, TaskCreationOptions.LongRunning);
            }
            else
            {
                tcpEq.ID     = ID;
                receiverTask = new Task(ReceiveCallback, TaskCreationOptions.LongRunning);
            }

            senderTask = new Task(DoSendStart, TaskCreationOptions.LongRunning);
        }
예제 #4
0
 public CommEquipmentObject(string ID, ConnUri uri, T _ClientImpl, bool _persistent = false)
 {
     this.ID                  = ID;
     this.ConnUri             = uri;
     this.ClientImpl          = _ClientImpl;
     this.timeLastIncoming    = TimeTools.GetCoarseMillisNow();
     this.persitentConnection = _persistent;
 }
예제 #5
0
        public TCPNETCommunicatorv2(TcpClient client, FrameWrapperBase <T> _frameWrapper = null) : base()
        {
            frameWrapper = _frameWrapper != null ? _frameWrapper : null;
            // Do stuff
            tcpClientProvided = true;
            var IP   = (client.Client.RemoteEndPoint as IPEndPoint).Address.ToString();
            var Port = (client.Client.RemoteEndPoint as IPEndPoint).Port;

            CommsUri = new ConnUri($"tcp://{IP}:{Port}");

            tcpEq = new CommEquipmentObject <TcpClient>("", CommsUri, client, false);
        }
예제 #6
0
 private void OnConnection(string ID, ConnUri uri, bool connected)
 {
     // Send addres msg
     if (connected)
     {
         var cmd = AddressCMD.RentFromPool();
         SendMessage(cmd);
     }
     else
     {
         CameraConnectedEvent?.Invoke(ID, false);
     }
 }
예제 #7
0
        public TCPNETCommunicator(Socket client, FrameWrapperBase <T> _frameWrapper = null) : base()
        {
            frameWrapper = _frameWrapper;

            // Do stuff
            tcpClientProvided = true;
            var IP   = (client.RemoteEndPoint as IPEndPoint).Address.ToString();
            var Port = (client.RemoteEndPoint as IPEndPoint).Port;

            CommsUri = new ConnUri($"tcp://{IP}:{Port}");

            // When clint provided, persistent is forced false
            persistent = false;
        }
예제 #8
0
        /// <summary>
        /// Creates an ICommunicator
        /// </summary>
        /// <typeparam name="T">Base data type used by the serialilzer/framewrapper. If not using serializer, put object</typeparam>
        /// <param name="uri">ConnUri defining the connection</param>
        /// <param name="frameWrapper">Serializer/Framewrapper that will be used. It can be null if not used</param>
        /// <param name="circular">Use internal CircularBuffer if you do not own the byte array passed to SendASync and do not have control over its lifespan. It is not used in SendSync</param>
        /// <returns>Created ICommunicator. It still needs to be initialized and started</returns>
        public static ICommunicator CreateCommunicator <T>(ConnUri uri, FrameWrapperBase <T> frameWrapper, bool circular = false)
        {
            ICommunicator c = null;

            switch (uri.UriType)
            {
            case ConnUri.TYPE.TCP:
                c = new TCPNETCommunicator <T>(frameWrapper, circular);
                break;

            case ConnUri.TYPE.UDP:
                c = new UDPNETCommunicator <T>(frameWrapper, circular);
                break;
            }

            return(c);
        }
예제 #9
0
파일: Program.cs 프로젝트: wjax/CommsPlus
        static async Task Main(string[] args)
        {
            Console.WriteLine("Hello Client!");

            // Crear un Framewrapper. En este casi mi librería ya te da uno hecho para Protobuff que usa Serializer.SerializeWithLengthPrefix(memoryStreamTX, data, PrefixStyle.Base128);
            FrameWrapperBase <MessageBase> frameWrapper1 = new ProtobufFrameWrapper <MessageBase>(true);
            // Crear un TCPCommunicator al que le pasas el framewrapper que ha de usar
            IPSocketCommunicator <MessageBase> client1 = new IPSocketCommunicator <MessageBase>(frameWrapper1);
            // Creas una Uri con el Peer al que quieres conectar
            ConnUri uri1 = new ConnUri("tcp://127.0.0.1:1111");

            // Inicializas el Communicator con la Uri, si quieres q sea persistente (reconecte), el ID, y alguna cosita más
            client1.Init(uri1, true, "CLIENT1", 10000);

            // Te suscribes a los eventos de conexión del communicator y de frame (objeto) disponible del framewrapper
            frameWrapper1.FrameAvailableEvent += OnMessage1;
            client1.ConnectionStateEvent      += OnConnection1;
            client1.DataReadySyncEvent        += OnRawSyncData;
            client1.DataReadyAsyncEvent       += OnRawAsyncData;

            // inicias todo
            client1.Start();

            while (true)
            {
                await Task.Delay(TimeSpan.FromSeconds(1));

                ChildMessage1 msg = new ChildMessage1()
                {
                    Name = DateTime.Now.ToShortTimeString()
                };

                //TEST
                Dictionary <string, string> payload = new Dictionary <string, string>();
                payload.Add("Jesus", "Roci");
                msg.Payload = new DictionaryPayload {
                    DataDictionary = payload
                };

                _ = client1.SendAsync(msg);

                //while (true)
                //    await Task.Delay(TimeSpan.FromSeconds(1));
            }
        }
예제 #10
0
        public override void Init(ConnUri uri, bool persistent, string id, int inactivityMS)
        {
            if (State != STATE.STOP ||
                ((uri == null || !uri.IsValid) && !clientProvided))
            {
                return;
            }

            ID = id;
            this.persistent = persistent;
            RECEIVE_TIMEOUT = inactivityMS;
            if (frameWrapper != null)
            {
                frameWrapper.ID = ID;
            }

            CommsUri = uri ?? CommsUri;
        }
예제 #11
0
        public IPSocketCommunicator(Socket client, FrameWrapperBase <T> _frameWrapper = null) : base()
        {
            frameWrapper   = _frameWrapper;
            socket         = client;
            clientProvided = true;

            // Do stuff
            var IP   = (client.RemoteEndPoint as IPEndPoint).Address.ToString();
            var Port = (client.RemoteEndPoint as IPEndPoint).Port;

            CommsUri = new ConnUri($"{(client.ProtocolType == ProtocolType.Tcp ? "tcp" : "udp")}://{IP}:{Port}");
            IPUInt   = HelperTools.IP2UInt(IP);

            // When client provided, persistent is forced false
            persistent = false;

            State = CommunicatorBase <T> .STATE.STOP;
        }
예제 #12
0
        public void Connect(ConnUri cameraUri)
        {
            if (_communicator != null)
            {
                _viscaFrameWrapper.FrameAvailableEvent -= OnMessage;
                _viscaFrameWrapper.Dispose();
                _viscaFrameWrapper = null;

                _communicator.ConnectionStateEvent -= OnConnection;
                _communicator.Dispose();
                _communicator = null;
            }

            _viscaFrameWrapper = new ViscaFrameWrapper();
            _viscaFrameWrapper.FrameAvailableEvent += OnMessage;
            _communicator = new TCPNETCommunicator <BaseMessage>(_viscaFrameWrapper);
            _communicator.ConnectionStateEvent += OnConnection;
            _communicator.Init(cameraUri, true, ID, 0);

            _communicator.Start();
        }
예제 #13
0
        /// <summary>
        /// Creates an ICommunicator
        /// </summary>
        /// <typeparam name="T">Base data type used by the serialilzer/framewrapper. If not using serializer, put object</typeparam>
        /// <param name="uri">string uri defining the connection</param>
        /// <param name="frameWrapper">Serializer/Framewrapper that will be used. It can be null if not used</param>
        /// <param name="circular">Use internal CircularBuffer if you do not own the byte array passed to SendASync and do not have control over its lifespan. It is not used in SendSync</param>
        /// <returns>Created ICommunicator. It still needs to be initialized and started</returns>
        public static ICommunicator CreateCommunicator <T>(string uriString, FrameWrapperBase <T> frameWrapper, bool circular = false)
        {
            ICommunicator c   = null;
            var           uri = new ConnUri(uriString);

            if (!uri.IsValid)
            {
                return(null);
            }

            switch (uri.UriType)
            {
            case ConnUri.TYPE.TCP:
                c = new TCPNETCommunicator <T>(frameWrapper, circular);
                break;

            case ConnUri.TYPE.UDP:
                c = new UDPNETCommunicator <T>(frameWrapper, circular);
                break;
            }

            return(c);
        }
예제 #14
0
 public abstract void Init(ConnUri uri, bool persistent, string ID, int inactivityMS, int sendGAP = 0);
예제 #15
0
 public virtual void FireConnectionEvent(string ID, ConnUri uri, bool connected)
 {
     ConnectionStateEvent?.Invoke(ID, uri, connected);
 }
예제 #16
0
파일: Program.cs 프로젝트: wjax/CommsPlus
 private static void OnConnection1(string ID, ConnUri uri, bool connected)
 {
     Console.WriteLine($"Client connected: {connected.ToString()}");
 }