Beispiel #1
0
        public static Message RpcResponse(uint id, uint uid, byte[] results)
        {
            var msg = new Message(MsgType.RpcResponse)
            {
                Val       = Value.MakeRpc(results, results.Length),
                Id        = id,
                SeqNumUid = uid
            };

            return(msg);
        }
Beispiel #2
0
        public static Message ExecuteRpc(uint id, uint uid, byte[] param)
        {
            var msg = new Message(MsgType.ExecuteRpc)
            {
                Val       = Value.MakeRpc(param, param.Length),
                Id        = id,
                SeqNumUid = uid
            };

            return(msg);
        }
Beispiel #3
0
        public static Message RpcResponse(uint id, uint uid, IList <byte> results)
        {
            var msg = new Message(MsgType.RpcResponse)
            {
                Val       = Value.MakeRpc(results, results.Count),
                Id        = id,
                SeqNumUid = uid
            };

            return(msg);
        }
Beispiel #4
0
        public static Message ExecuteRpc(uint id, uint uid, IList <byte> param)
        {
            var msg = new Message(MsgType.ExecuteRpc)
            {
                Val       = Value.MakeRpc(param, param.Count),
                Id        = id,
                SeqNumUid = uid
            };

            return(msg);
        }
Beispiel #5
0
        public static Message Read(WireDecoder decoder, GetEntryTypeFunc getEntryType)
        {
            byte msgType = 0;

            if (!decoder.Read8(ref msgType))
            {
                return(null);
            }
            MsgType mtype = (MsgType)msgType;
            var     msg   = new Message(mtype);
            NtType  type  = 0;
            byte    tmpB  = 0;
            ushort  tmpUs = 0;

            switch (mtype)
            {
            case MsgType.KeepAlive:
                break;

            case MsgType.ClientHello:
                ushort protoRev = 0;
                if (!decoder.Read16(ref protoRev))
                {
                    return(null);
                }
                msg.Id = protoRev;
                if (protoRev >= 0x0300u)
                {
                    if (!decoder.ReadString(ref msg.m_str))
                    {
                        return(null);
                    }
                }
                break;

            case MsgType.ProtoUnsup:
                ushort rdproto = 0;
                if (!decoder.Read16(ref rdproto))
                {
                    return(null);
                }
                msg.Id = rdproto;
                break;

            case MsgType.ServerHelloDone:
                break;

            case MsgType.ServerHello:
                if (decoder.ProtoRev < 0x0300u)
                {
                    decoder.Error = "received SERVER_HELLO_DONE in protocol < 3.0";
                    return(null);
                }
                byte rdflgs = 0;
                if (!decoder.Read8(ref rdflgs))
                {
                    return(null);
                }
                msg.Flags = rdflgs;
                if (!decoder.ReadString(ref msg.m_str))
                {
                    return(null);
                }
                break;

            case MsgType.ClientHelloDone:
                if (decoder.ProtoRev < 0x0300)
                {
                    decoder.Error = "recieved SERVER_HELLO_DONE in protocol < 3.0";
                    return(null);
                }
                break;

            case MsgType.EntryAssign:
                if (!decoder.ReadString(ref msg.m_str))
                {
                    return(null);
                }
                if (!decoder.ReadType(ref type))
                {
                    return(null);
                }
                if (!decoder.Read16(ref tmpUs))
                {
                    return(null);
                }
                msg.Id = tmpUs;
                if (!decoder.Read16(ref tmpUs))
                {
                    return(null);
                }
                msg.SeqNumUid = tmpUs;
                if (decoder.ProtoRev >= 0x0300)
                {
                    if (!decoder.Read8(ref tmpB))
                    {
                        return(null);
                    }
                    msg.Flags = tmpB;
                }
                msg.Val = decoder.ReadValue(type);
                if (msg.Val == null)
                {
                    return(null);
                }
                break;

            case MsgType.EntryUpdate:
                if (!decoder.Read16(ref tmpUs))
                {
                    return(null);
                }
                msg.Id = tmpUs;
                if (!decoder.Read16(ref tmpUs))
                {
                    return(null);
                }
                msg.SeqNumUid = tmpUs;
                if (decoder.ProtoRev >= 0x0300)
                {
                    if (!decoder.ReadType(ref type))
                    {
                        return(null);
                    }
                }
                else
                {
                    type = getEntryType(msg.Id);
                }
                Debug4($"update message data tyepe: {type}");
                msg.Val = decoder.ReadValue(type);
                if (msg.Val == null)
                {
                    return(null);
                }
                break;

            case MsgType.FlagsUpdate:
                if (decoder.ProtoRev < 0x0300)
                {
                    decoder.Error = "received FLAGS_UPDATE in protocol < 3.0";
                    return(null);
                }
                if (!decoder.Read16(ref tmpUs))
                {
                    return(null);
                }
                msg.Id = tmpUs;
                if (!decoder.Read8(ref tmpB))
                {
                    return(null);
                }
                msg.Flags = tmpB;
                break;

            case MsgType.EntryDelete:
                if (decoder.ProtoRev < 0x0300)
                {
                    decoder.Error = "received ENTRY_DELETE in protocol < 3.0";
                    return(null);
                }
                if (!decoder.Read16(ref tmpUs))
                {
                    return(null);
                }
                msg.Id = tmpUs;
                break;

            case MsgType.ClearEntries:
                if (decoder.ProtoRev < 0x0300)
                {
                    decoder.Error = "received CLEAR_ENTRIES in protocol < 3.0";
                    return(null);
                }
                uint magic = 0;
                if (!decoder.Read32(ref magic))
                {
                    return(null);
                }
                if (magic != ClearAllMagic)
                {
                    decoder.Error = "received incorrect CLEAR_ENTRIES magic value, ignoring";
                    return(null);
                }
                break;

            case MsgType.ExecuteRpc:
                if (decoder.ProtoRev < 0x0300)
                {
                    decoder.Error = "received EXECUTE_RPC in protocol < 3.0";
                    return(null);
                }
                if (!decoder.Read16(ref tmpUs))
                {
                    return(null);
                }
                msg.Id = tmpUs;
                if (!decoder.Read16(ref tmpUs))
                {
                    return(null);
                }
                msg.SeqNumUid = tmpUs;
                ulong size;
                if (!decoder.ReadUleb128(out size))
                {
                    return(null);
                }
                byte[] results;
                if (!decoder.Read(out results, (int)size))
                {
                    return(null);
                }
                msg.Val = Value.MakeRpc(results, (int)size);
                break;

            case MsgType.RpcResponse:
                if (decoder.ProtoRev < 0x0300)
                {
                    decoder.Error = "received RPC_RESPONSE in protocol < 3.0";
                    return(null);
                }
                if (!decoder.Read16(ref tmpUs))
                {
                    return(null);
                }
                msg.Id = tmpUs;
                if (!decoder.Read16(ref tmpUs))
                {
                    return(null);
                }
                msg.SeqNumUid = tmpUs;
                ulong size2;
                if (!decoder.ReadUleb128(out size2))
                {
                    return(null);
                }
                byte[] results2;
                if (!decoder.Read(out results2, (int)size2))
                {
                    return(null);
                }
                msg.Val = Value.MakeRpc(results2, (int)size2);
                break;

            default:
                decoder.Error = "unrecognized message type";
                Info($"unrecognized message type: {msgType}");
                return(null);
            }
            return(msg);
        }
Beispiel #6
0
        public void CreatePolledRpc(string name, byte[] def)
        {
            if (string.IsNullOrEmpty(name) || def == null || def.Length == 0)
            {
                return;
            }
            IDisposable monitor = null;

            try
            {
                monitor = m_monitor.Enter();
                IDisposable monitorToUnlock;
                if (!m_server)
                {
                    return;
                }

                Entry entry;
                if (!m_entries.TryGetValue(name, out entry))
                {
                    entry = new Entry(name);
                    m_entries.Add(name, entry);
                }

                var oldValue = entry.Value;
                var value    = Value.MakeRpc(def);
                entry.Value       = value;
                entry.RpcCallback = null;

                if (oldValue != null && oldValue == value)
                {
                    return;
                }

                if (entry.Id == 0xffff)
                {
                    int id = m_idMap.Count;
                    entry.Id = (uint)id;
                    m_idMap.Add(entry);
                }

                if (m_queueOutgoing == null)
                {
                    return;
                }
                var queueOutgoing = m_queueOutgoing;
                if (oldValue == null || oldValue.Type != value.Type)
                {
                    ++entry.SeqNum;
                    var msg = Message.EntryAssign(name, entry.Id, entry.SeqNum.Value, value, entry.Flags);
                    monitorToUnlock = Interlocked.Exchange(ref monitor, null);
                    monitorToUnlock.Dispose();
                    queueOutgoing(msg, null, null);
                }
                else
                {
                    ++entry.SeqNum;
                    var msg = Message.EntryUpdate(entry.Id, entry.SeqNum.Value, value);
                    monitorToUnlock = Interlocked.Exchange(ref monitor, null);
                    monitorToUnlock.Dispose();
                    queueOutgoing(msg, null, null);
                }
            }
            finally
            {
                monitor?.Dispose();
            }
        }