///// <summary> ///// ///// </summary> ///// <param name="buffer"></param> ///// <param name="addr"></param> ///// <param name="size"></param> //private unsafe void CopyRealValue(IByteBuffer buffer,void* addr,int size,int id) //{ // buffer.WriteInt(id); // int tagsize = buffer.WriterIndex + size; // if (tagsize > buffer.Capacity) // buffer.AdjustCapacity((int)(tagsize*1.1)); // Buffer.MemoryCopy(addr, (void*)(buffer.AddressOfPinnedMemory() + buffer.WriterIndex), size, size); // buffer.SetWriterIndex(tagsize); //} ///// <summary> ///// ///// </summary> ///// <param name="buffer"></param> ///// <param name="id"></param> //private unsafe void ProcessTagPushToClient(IByteBuffer buffer,int id) //{ // var tag = mTagManager.GetTagById(id); // var addr = mTagConsumer.GetDataRawAddr(id); // CopyRealValue(buffer, addr, tag.ValueSize, id); //} /// <summary> /// /// </summary> /// <param name="buffer"></param> /// <param name="id"></param> private void ProcessTagPush(ByteBuffer re, int id) { byte type, qu; DateTime mtime; object value = mTagConsumer.GetTagValue(id, out qu, out mtime, out type); re.Write(id); re.Write(type); switch (type) { case (byte)TagType.Bool: re.Write(Convert.ToByte(value)); break; case (byte)TagType.Byte: re.Write(Convert.ToByte(value)); break; case (byte)TagType.Short: re.Write(Convert.ToUInt16(value)); break; case (byte)TagType.UShort: re.Write(Convert.ToUInt16(value)); break; case (byte)TagType.Int: re.Write(Convert.ToInt32(value)); break; case (byte)TagType.UInt: re.Write(Convert.ToUInt32(value)); break; case (byte)TagType.Long: case (byte)TagType.ULong: re.Write(Convert.ToInt64(value)); break; case (byte)TagType.Float: re.Write(Convert.ToSingle(value)); break; case (byte)TagType.Double: re.Write(Convert.ToDouble(value)); break; case (byte)TagType.String: string sval = value.ToString(); re.Write(sval); //re.Write(sval.Length); //re.Write(sval, Encoding.Unicode); break; case (byte)TagType.DateTime: re.Write(((DateTime)value).Ticks); break; case (byte)TagType.IntPoint: re.Write(((IntPointData)value).X); re.Write(((IntPointData)value).Y); break; case (byte)TagType.UIntPoint: re.Write((int)((UIntPointData)value).X); re.Write((int)((UIntPointData)value).Y); break; case (byte)TagType.IntPoint3: re.Write(((IntPoint3Data)value).X); re.Write(((IntPoint3Data)value).Y); re.Write(((IntPoint3Data)value).Z); break; case (byte)TagType.UIntPoint3: re.Write((int)((UIntPoint3Data)value).X); re.Write((int)((UIntPoint3Data)value).Y); re.Write((int)((UIntPoint3Data)value).Z); break; case (byte)TagType.LongPoint: re.Write(((LongPointData)value).X); re.Write(((LongPointData)value).Y); break; case (byte)TagType.ULongPoint: re.Write((long)((ULongPointData)value).X); re.Write((long)((ULongPointData)value).Y); break; case (byte)TagType.LongPoint3: re.Write(((LongPoint3Data)value).X); re.Write(((LongPoint3Data)value).Y); re.Write(((LongPoint3Data)value).Z); break; case (byte)TagType.ULongPoint3: re.Write((long)((ULongPoint3Data)value).X); re.Write((long)((ULongPoint3Data)value).Y); re.Write((long)((ULongPoint3Data)value).Z); break; } re.Write(mtime.Ticks); re.WriteByte(qu); //Debug.Print(buffer.WriterIndex.ToString()); }
/// <summary> /// /// </summary> private void SendThreadPro() { while (!mIsClosed) { try { resetEvent.WaitOne(); if (mIsClosed) { return; } resetEvent.Reset(); Stopwatch sw = new Stopwatch(); sw.Start(); lock (mtmp) { if (mtmp.Count > 0) { foreach (var vv in mtmp) { if (!mMonitors.ContainsKey(vv.Key)) { mMonitors.Add(vv.Key, vv.Value); } } } mtmp.Clear(); } foreach (var vv in mMonitors.Where(e => e.Value.RefCount <= 0).ToArray()) { mMonitors.Remove(vv.Key); } var chgarry = ArrayPool <int> .Shared.Rent(mMonitors.Count()); Array.Clear(chgarry, 0, chgarry.Length); int i = 0; DateTime te; byte qu; byte vtype; foreach (var vv in mMonitors) { var oval = mTagConsumer.GetTagValue(vv.Key, out qu, out te, out vtype); vv.Value.CheckValueChaned(oval, qu); if (vv.Value.IsValueChanged) { chgarry[i++] = vv.Key; vv.Value.IsValueChanged = false; } } if (i > 0) { var clients = mCallBackRegistorIds.ToArray(); foreach (var cb in clients) { var buffer = Parent.Allocate(Api.ApiFunConst.RealDataPushFun, i * 64 + 4); buffer.Write(0); buffers.Add(cb.Key, buffer); lock (mDataCounts) { if (mDataCounts.ContainsKey(cb.Key)) { mDataCounts[cb.Key] = 0; } } } for (int j = 0; j < i; j++) { var vid = chgarry[j]; MonitorTag tag = mMonitors[vid]; foreach (var vvc in clients) { if (vvc.Value.Contains(vid)) { ProcessTagPush(buffers[vvc.Key], vid, (byte)tag.Type, tag.Value, tag.Quality); lock (mDataCounts) { if (mDataCounts.ContainsKey(vvc.Key)) { mDataCounts[vvc.Key]++; } } } } } foreach (var cb in buffers) { // cb.Value.MarkWriterIndex(); var vindex = cb.Value.WriteIndex; cb.Value.WriteIndex = 1; lock (mDataCounts) { if (mDataCounts.ContainsKey(cb.Key)) { cb.Value.Write(mDataCounts[cb.Key]); cb.Value.WriteIndex = vindex; // cb.Value.ResetWriterIndex(); Parent.PushRealDatatoClient(cb.Key, cb.Value); } } } buffers.Clear(); } sw.Stop(); LoggerService.Service.Info("RealDataServerProcess", "推送数据耗时" + sw.ElapsedMilliseconds + " 个数大小:" + i); } catch (Exception ex) { LoggerService.Service.Erro("RealDataServerProcess", ex.StackTrace); } } }