示例#1
0
        internal void SendReadNotify <TType>(uint nbElements)
        {
            if (Client.Configuration.DebugTiming)
            {
                lock (ElapsedTimings)
                {
                    if (!ElapsedTimings.ContainsKey("SendReadNotify"))
                    {
                        ElapsedTimings.Add("SendReadNotify", Stopwatch.Elapsed);
                    }
                }
            }

            DataPacket packet = DataPacket.Create(16);

            packet.Command = (ushort)CommandID.CA_PROTO_READ_NOTIFY;
            Type t = typeof(TType);

            if (typeof(TType).IsArray)
            {
                t = (typeof(TType)).GetElementType();
            }
            else if (t.IsGenericType)
            {
                if (t.GetGenericArguments().First() == typeof(object))
                {
                    t = t.GetGenericTypeDefinition().MakeGenericType(new Type[] { channelDefinedType });
                }
            }
            if (t == typeof(object))
            {
                t = channelDefinedType;
            }
            packet.DataType   = (ushort)TypeHandling.Lookup[t];
            packet.DataCount  = nbElements;
            packet.Parameter1 = SID;
            uint ioid = (NextIoId++);

            packet.Parameter2 = ioid;

            lock (ioc.PendingIo)
            {
                ioc.PendingIo.Add(ioid, this);
            }

            ioc.Send(packet);
        }
示例#2
0
        internal virtual void Disconnect()
        {
            if (Disposed)
            {
                return;
            }
            ioc?.RemoveChannel(this);
            lock (ConnectionLock)
            {
                if (Status != ChannelStatus.CONNECTED && Status != ChannelStatus.CONNECTING)
                {
                    return;
                }

                Status          = ChannelStatus.DISCONNECTED;
                StartSearchTime = DateTime.Now;
                ioc             = null;
                SID             = 0;

                if (PrivMonitorChanged != null)
                {
                    AfterConnect(action =>
                    {
                        if (MonitoredType == null)
                        {
                            return;
                        }
                        DataPacket p = DataPacket.Create(16 + 16);
                        p.Command    = (ushort)CommandID.CA_PROTO_EVENT_ADD;
                        p.DataType   = (ushort)TypeHandling.Lookup[MonitoredType];
                        p.DataCount  = MonitoredElements;
                        p.Parameter1 = SID;
                        p.Parameter2 = CID;

                        p.SetUInt16(12 + 16, (ushort)MonitorMask);

                        if (ioc != null)
                        {
                            try
                            {
                                ioc.Send(p);
                            }
                            catch
                            {
                                Disconnect();
                            }
                        }
                        else
                        {
                            Disconnect();
                        }
                    });
                }
            }
        }
示例#3
0
        public void Dispose()
        {
            if (Disposed)
            {
                return;
            }
            Disposed = true;
            lock (Client.Channels)
                Client.Channels.Remove(this.CID);
            if (ioc != null)
            {
                DataPacket p = DataPacket.Create(16);
                p.Command    = (ushort)CommandID.CA_PROTO_CLEAR_CHANNEL;
                p.DataType   = 0;
                p.DataCount  = 0;
                p.Parameter1 = SID;
                p.Parameter2 = CID;
                ioc?.Send(p);

                ioc?.RemoveChannel(this);
            }
            Client.Searcher.Remove(this);
            Status = ChannelStatus.DISPOSED;
        }
示例#4
0
        internal void SetIoc(DataPipe pipe)
        {
            ClientTcpReceiver tcpReceiver = (ClientTcpReceiver)pipe[0];

            tcpReceiver.AddChannel(this);
            lock (ConnectionLock)
            {
                if (!Client.Searcher.Contains(this))
                {
                    return;
                }
                Client.Searcher.Remove(this);
                SID = 0;

                ioc = tcpReceiver;
                lock (ioc.ChannelSID)
                {
                    //Console.WriteLine(ioc.ChannelSID.Count);
                    // Channel already known
                    if (ioc.ChannelSID.ContainsKey(ChannelName))
                    {
                        SID = ioc.ChannelSID[ChannelName];
                        //Console.WriteLine("Here");
                        Channel chan = ioc.ConnectedChannels.FirstOrDefault(row => row.ChannelName == ChannelName && row.ChannelDataCount != 0);
                        if (chan != null)
                        {
                            this.ChannelDataCount   = chan.ChannelDataCount;
                            this.channelDefinedType = chan.ChannelDefinedType;
                            Status = ChannelStatus.CONNECTED;
                            ConnectionEvent.Set();
                        }
                    }
                }
            }
            if (SID != 0)
            {
                //Console.WriteLine("SID " + SID + " STATUS CHANGED");
                if (StatusChanged != null)
                {
                    StatusChanged(this, Status);
                }
                return;
            }
            if (Client.Configuration.DebugTiming)
            {
                lock (ElapsedTimings)
                {
                    if (!ElapsedTimings.ContainsKey("IocConnection"))
                    {
                        ElapsedTimings.Add("IocConnection", Stopwatch.Elapsed);
                    }
                }
            }

            // We need to create the channel
            int padding;

            if (ChannelName.Length % 8 == 0)
            {
                padding = 8;
            }
            else
            {
                padding = (8 - (ChannelName.Length % 8));
            }

            DataPacket packet = DataPacket.Create(16 + ChannelName.Length + padding);

            packet.Command    = (ushort)CommandID.CA_PROTO_CREATE_CHAN;
            packet.DataType   = 0;
            packet.DataCount  = 0;
            packet.Parameter1 = cid;
            packet.Parameter2 = (uint)CAConstants.CA_MINOR_PROTOCOL_REVISION;
            packet.SetDataAsString(ChannelName);
            //Status = ChannelStatus.CONNECTING;

            if (ioc != null)
            {
                ioc.Send(packet);
            }
            else
            {
                Disconnect();
                return;
            }
            lock (ElapsedTimings)
            {
                if (!ElapsedTimings.ContainsKey("SendCreateChannel"))
                {
                    ElapsedTimings.Add("SendCreateChannel", Stopwatch.Elapsed);
                }
            }
        }