Esempio n. 1
0
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="consistentKey"></param>
        /// <param name="cmdName"></param>
        /// <param name="payload"></param>
        /// <param name="funcResultFactory"></param>
        /// <param name="asyncState"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">cmdName is null or empty.</exception>
        /// <exception cref="ArgumentNullException">funcResultFactory is null.</exception>
        public Task <TResult> Send <TResult>(byte[] consistentKey, string cmdName, byte[] payload,
                                             Func <AsyncBinaryResponse, TResult> funcResultFactory, object asyncState = null)
        {
            if (string.IsNullOrEmpty(cmdName))
            {
                throw new ArgumentNullException("cmdName");
            }
            if (funcResultFactory == null)
            {
                throw new ArgumentNullException("funcResultFactory");
            }

            var seqID         = base.NextRequestSeqID();
            var cmdLength     = cmdName.Length;
            var messageLength = (payload == null ? 0 : payload.Length) + cmdLength + 6;
            var sendBuffer    = new byte[messageLength + 4];

            //write message length
            Buffer.BlockCopy(NetworkBitConverter.GetBytes(messageLength), 0, sendBuffer, 0, 4);
            //write seqID.
            Buffer.BlockCopy(NetworkBitConverter.GetBytes(seqID), 0, sendBuffer, 4, 4);
            //write response flag length.
            Buffer.BlockCopy(NetworkBitConverter.GetBytes((short)cmdLength), 0, sendBuffer, 8, 2);
            //write response flag
            Buffer.BlockCopy(Encoding.ASCII.GetBytes(cmdName), 0, sendBuffer, 10, cmdLength);
            //write body buffer
            if (payload != null && payload.Length > 0)
            {
                Buffer.BlockCopy(payload, 0, sendBuffer, 10 + cmdLength, payload.Length);
            }

            var source = new TaskCompletionSource <TResult>(asyncState);

            base.Send(new Request <Response.AsyncBinaryResponse>(consistentKey, seqID, cmdName, sendBuffer,
                                                                 ex => source.TrySetException(ex),
                                                                 response =>
            {
                TResult result;
                try { result = funcResultFactory(response); }
                catch (Exception ex) { source.TrySetException(ex); return; }

                source.TrySetResult(result);
            }));
            return(source.Task);
        }
Esempio n. 2
0
        public byte[] encode(IConnection connection, object obj)
        {
            UserInfo msg = (UserInfo)obj;

            byte[] name = Encoding.Default.GetBytes(msg.username);

            byte[] age = NetworkBitConverter.GetBytes(msg.age);

            byte[] data = new byte[4 + name.Length + 4];

            Buffer.BlockCopy(NetworkBitConverter.GetBytes(name.Length), 0, data, 0, 4);

            Buffer.BlockCopy(name, 0, data, 4, name.Length);

            Buffer.BlockCopy(age, 0, data, 4 + name.Length, 4);

            return(data);
        }
Esempio n. 3
0
        /// <summary>
        /// Serialize
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="reqCode"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">request is null</exception>
        private byte[] Serialize <T>(byte reqCode, T request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            byte[] bytes = null;
            using (var ms = new MemoryStream())
            {
                ms.Position = 5;
                ProtoBuf.Serializer.Serialize(ms, request);
                bytes = ms.ToArray();
            }
            bytes[4] = reqCode;
            Buffer.BlockCopy(NetworkBitConverter.GetBytes(bytes.Length - 4), 0, bytes, 0, 4);

            return(bytes);
        }