Example #1
0
        public void Send(IPOCS.Protocol.Message msg)
        {
            var buffer = msg.serialize().ToArray();

            try
            {
                this.TcpClient.GetStream().Write(buffer, 0, buffer.Length);
            } catch { }
        }
        private bool?Instance_OnConnectionRequest(IPOCS.Client client, IPOCS.Protocol.Packets.ConnectionRequest request)
        {
            var concentrator = MainViewModel.YardEditorViewModel.Concentrators.FirstOrDefault((c) => c.Name == client.Name);

            if (concentrator == null)
            {
                Log.Insert(0, $"{DateTime.Now.ToUniversalTime()} : Unable to locate {client.Name} - forcing disconnect.");
                return(false);
            }
            List <byte> vector;

            try
            {
                vector = concentrator.Serialize();
            }
            catch (NullReferenceException)
            {
                Log.Insert(0, $"{DateTime.Now.ToUniversalTime()} : Serialization for {client.Name} site data failed.");
                return(false);
            }

            ushort providedChecksum = ushort.Parse(request.RXID_SITE_DATA_VERSION);
            ushort computedChecksum = IPOCS.CRC16.Calculate(vector.ToArray());

            Log.Insert(0, $"{DateTime.Now.ToUniversalTime()} : ({client.Name}) R.CRC: {request.RXID_SITE_DATA_VERSION}, C.CRC: {computedChecksum}");
            if (providedChecksum == 0 || computedChecksum != providedChecksum)
            {
                var responseMsg = new IPOCS.Protocol.Message
                {
                    RXID_OBJECT = client.Name
                };
                responseMsg.packets.Add(new IPOCS.Protocol.Packets.ApplicationData
                {
                    RNID_XUSER = 0x0001,
                    PAYLOAD    = vector.ToArray()
                });
                client.Send(responseMsg);
                return(false);
            }
            return(true);
        }