private IFormatter <T> ApplyOptionsAndGetValueTypeFormatterObject <T>(FormatFunc <T> formatFunc, FormatSpecificFunc <T> formatSpecificFunc, MissingFormatSpecificHandling missingFormatSpecific)
        {
            FormatFunc <T>         valueTypeFormatFunc         = formatFunc;
            FormatSpecificFunc <T> valueTypeFormatSpecificFunc = formatSpecificFunc;

            // formatFunc will never be null because ToString() is always available.

            if (formatSpecificFunc is null)
            {
                if (_missingFormatSpecific == MissingFormatSpecificHandling.ThrowNotSupportedException)
                {
                    valueTypeFormatSpecificFunc = FormatSpecificThrowsNotSupportedException;
                }
                else if (_missingFormatSpecific == MissingFormatSpecificHandling.ReturnEmptyString)
                {
                    valueTypeFormatSpecificFunc = FormatSpecificReturnsEmptyString;
                }
                else if (_missingFormatSpecific == MissingFormatSpecificHandling.ReturnNull)
                {
                    valueTypeFormatSpecificFunc = FormatSpecificReturnsNull;
                }
                else
                {
                    valueTypeFormatSpecificFunc = (value, format) => valueTypeFormatFunc(value);
                }
            }

            return(new ValueTypeFunctorFormatterObject <T>(valueTypeFormatFunc, valueTypeFormatSpecificFunc));
        }
        /// <summary>
        /// Extract the meaningful protocol data
        /// </summary>
        /// <param name="count"></param>
        public void Extract(int count)
        {
            readCount += count;
            // If current data is shorter than a header, next data
            if (readCount < HEADER_SIZE)
            {
                return;
            }
            // Copy the first several bytes to header bytes
            Array.Copy(readBytes, _headerBytes, HEADER_SIZE);
            // Calculate the length value of current protocol
            _protoLen = FormatFunc.BytesToInt(_headerBytes);
            // Calculate the remain length supposing that current protocol is detached
            int remain = readCount - _protoLen - HEADER_SIZE;

            // The remain which is less than 0 means that more data need be received, next data
            if (remain < 0)
            {
                return;
            }
            // Copy current protocol data to a new array
            byte[] data = new byte[_protoLen];
            Array.Copy(readBytes, HEADER_SIZE, data, 0, _protoLen);
            // Trigger the event on protocol data extracted
            OnBytesExtracted(data);
            // Detach the extracted data and initialize
            Array.Copy(readBytes, _protoLen + HEADER_SIZE, readBytes, 0, remain);
            Reset();
            // Loop above process until no more protocol could be extracted
            if (remain > 0)
            {
                Extract(remain);
            }
        }
        private IFormatter <T> ApplyOptionsAndGetReferenceTypeFormatterObject <T>(FormatFunc <T> formatFunc, FormatSpecificFunc <T> formatSpecificFunc, MissingFormatSpecificHandling missingFormatSpecific)
        {
            FormatFunc <T>         referenceTypeFormatFunc         = formatFunc;
            FormatSpecificFunc <T> referenceTypeFormatSpecificFunc = formatSpecificFunc;

            // formatFunc will never be null because ToString() is always available.

            if (_referenceTypesFormatNullToNull)
            {
                referenceTypeFormatFunc = value => value != null?formatFunc(value) : null;
            }

            if (formatSpecificFunc is null)
            {
                if (missingFormatSpecific == MissingFormatSpecificHandling.UseToString)
                {
                    referenceTypeFormatSpecificFunc = (value, format) => referenceTypeFormatFunc(value);
                }
                else if (missingFormatSpecific == MissingFormatSpecificHandling.ReturnNull)
                {
                    referenceTypeFormatSpecificFunc = FormatSpecificReturnsNull;
                }
                else if (missingFormatSpecific == MissingFormatSpecificHandling.ReturnEmptyString)
                {
                    referenceTypeFormatSpecificFunc = FormatSpecificReturnsEmptyString;
                }
                else
                {
                    referenceTypeFormatSpecificFunc = FormatSpecificThrowsNotSupportedException;
                }
            }

            return(new FunctorFormatterObject <T>(referenceTypeFormatFunc, referenceTypeFormatSpecificFunc));
        }
        private IFormatter <T> GetValueTypeFormatterObject <T>()
            where T : struct
        {
            FormatFunc <T>         formatFunc         = GetDefaultValueTypeFormatFunc <T>();
            FormatSpecificFunc <T> formatSpecificFunc = GetDefaultValueTypeFormatSpecificFunc <T>();

            return(ApplyOptionsAndGetValueTypeFormatterObject(formatFunc, formatSpecificFunc, _missingFormatSpecific));
        }
        private IFormatter <T> AltCreateFormatterObject <T>(FormatFunc <T> formatFunc, FormatSpecificFunc <T> formatSpecificFunc, MissingFormatSpecificHandling missingFormatSpecific)
        {
            Type type = typeof(T);

            string     methodName    = type.GetTypeInfo().IsValueType ? nameof(ApplyOptionsAndGetValueTypeFormatterObject) : nameof(ApplyOptionsAndGetReferenceTypeFormatterObject);
            MethodInfo factoryMethod = ReflectionHelper.GetPrivateGenericMethod(typeof(FormatterContainer), methodName, type);

            Func <FormatFunc <T>, FormatSpecificFunc <T>, MissingFormatSpecificHandling, IFormatter <T> > factory = factoryMethod.CreateDelegate <Func <FormatFunc <T>, FormatSpecificFunc <T>, MissingFormatSpecificHandling, IFormatter <T> > >(this);

            return(factory(formatFunc, formatSpecificFunc, missingFormatSpecific));
        }
        private IFormatter <T> GetReferenceTypeFormatterObject <T>()
            where T : class
        {
            if (typeof(T) == typeof(string))
            {
                return((IFormatter <T>) new StringFormatter());
            }

            FormatFunc <T>         formatFunc         = GetDefaultReferenceTypeFormatFunc <T>();
            FormatSpecificFunc <T> formatSpecificFunc = GetDefaultReferenceTypeFormatSpecificFunc <T>();

            return(ApplyOptionsAndGetReferenceTypeFormatterObject(formatFunc, formatSpecificFunc, _missingFormatSpecific));
        }
示例#7
0
        /// <summary>
        /// Send data synchronously
        /// </summary>
        /// <param name="protoData"></param>
        public void Send(byte[] protoData)
        {
            int len = protoData.Length;

            if (len <= 0)
            {
                return;
            }
            byte[] header = FormatFunc.IntToBytes(len);
            byte[] data   = FormatFunc.BytesConcat(header, protoData);
            try { _socket.Send(data); }
            catch (SocketException e) { ProcessNetworkAnomaly(e.ErrorCode, e.Message); }
        }
示例#8
0
    // Use this for initialization
    void Start()
    {
        Property p = new Property(50f);

        p.AddTrace("weapon1", 10f);
        p.AddTrace("weapon1", 0.3f, DeltaMode.Percentage);
        Debug.Log(FormatFunc.ObjectToJson(p));

        Property p1 = FormatFunc.JsonToObject <Property>(FormatFunc.ObjectToJson(p));

        Debug.Log(FormatFunc.ObjectToJson(p1));
        Debug.Log(p1.value);
    }
示例#9
0
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.S))
        {
            //线程调用测试
            HoxisProtocol proto = new HoxisProtocol
            {
                type     = ProtocolType.Synchronization,
                receiver = new HoxisProtocolReceiver
                {
                    type = ReceiverType.Cluster,
                    uid  = 0,
                },
                sender = new HoxisProtocolSender
                {
                    aid      = new HoxisAgentID("soldier", 10),
                    loopback = true,
                },
                action = new HoxisProtocolAction
                {
                    method = "shoot",
                    args   = new HoxisProtocolArgs
                    {
                        values = new Dictionary <string, string> {
                            { "val", "15" },
                            { "src", "weapon" },
                        }
                    },
                },
                desc = "thread test",
            };
            string json = FormatFunc.ObjectToJson(proto);
            byte[] data = FormatFunc.StringToBytes(json);

            Thread t = new Thread(() =>
            {
                //Thread.Sleep(1000);
                HoxisDirector.Ins.ProtocolEntry(data);
            });
            t.Start();
        }
    }
示例#10
0
 public ValueTypeFunctorFormatterObject(FormatFunc <T> formatFunc, FormatSpecificFunc <T> formatSpecificFunc)
     : base(formatFunc, formatSpecificFunc)
 {
 }
示例#11
0
 /// <summary>
 /// Configures formatting of <typeparamref name="T"/> to use the provided functions.
 /// </summary>
 /// <typeparam name="T">The type for which to configure formatting.</typeparam>
 /// <param name="formatFunc">A function that implements formatting functionality for <typeparamref name="T"/>.</param>
 /// <param name="formatSpecificFunc">A function that implements formatting functionality with a format specifier for <typeparamref name="T"/>.</param>
 public abstract void UseFunc <T>(FormatFunc <T> formatFunc, FormatSpecificFunc <T> formatSpecificFunc);
示例#12
0
 /// <summary>
 /// Configures formatting of <typeparamref name="T"/> to use the provided function.
 /// </summary>
 /// <typeparam name="T">The type for which to configure formatting.</typeparam>
 /// <param name="formatFunc">A function that implements formatting functionality for <typeparamref name="T"/>.</param>
 /// <param name="missingFormatSpecific">A value indicating how to configure format functionality if the type does not have a ToString overload that accepts a format specification.</param>
 public abstract void UseFunc <T>(FormatFunc <T> formatFunc, MissingFormatSpecificHandling missingFormatSpecific);
示例#13
0
 /// <summary>
 /// Configures formatting of <typeparamref name="T"/> to use the provided function.
 /// </summary>
 /// <typeparam name="T">The type for which to configure formatting.</typeparam>
 /// <param name="formatFunc">A function that implements formatting functionality for <typeparamref name="T"/>.</param>
 public void UseFunc <T>(FormatFunc <T> formatFunc) => UseFunc <T>(formatFunc, MissingFormatSpecificHandling.ThrowNotSupportedException);
示例#14
0
 public FormatterProxy(FormatFunc <T> func)
 {
     formatFunc = func;
 }
 public FunctorFormatterObject(FormatFunc <T> formatFunc, FormatSpecificFunc <T> formatSpecificFunc)
 {
     _formatFunc         = formatFunc;
     _formatSpecificFunc = formatSpecificFunc;
 }
示例#16
0
 /// <summary>Initializes a new instance.</summary>
 /// <param name="exception">The Exception to serialize.</param>
 /// <param name="omitStackTrace">
 /// Whether or not to serialize the Exception.StackTrace member
 /// if it's not null.
 /// </param>
 public ExceptionXElement(Exception exception, bool omitStackTrace) : base(FormatFunc(exception, omitStackTrace)())
 {
 }
示例#17
0
 private LeaderBoardFormat(int persistedKey, FormatFunc func)
 {
     this.PersistedKey = persistedKey;
     this.formatFunc   = func;
 }