Exemple #1
0
        // Throws `FormatException` if s is not a valid Base58 string, or the checksum is invalid
        public IByteArray DecodeWithCheckSum(string s)
        {
            if (string.IsNullOrWhiteSpace(s))
            {
                throw new ArgumentNullException();
            }

            IByteArray dataWithCheckSum    = this.Decode(s);
            IByteArray dataWithoutCheckSum = this.VerifyAndRemoveCheckSum(dataWithCheckSum);

            dataWithCheckSum.Return();

            if (dataWithoutCheckSum == null)
            {
                throw new FormatException("Base checksum is invalid");
            }

            return(dataWithoutCheckSum);
        }
Exemple #2
0
        public bool Receive(Action <IByteArray> handler)
        {
            if (this.receiveQueue.Count > 0)
            {
                IByteArray bytes = null;
                lock (this.receiveQueue) {
                    bytes = this.receiveQueue.Dequeue();
                }
                if (null != bytes)
                {
                    if (null != handler)
                    {
                        handler(bytes);
                    }
                    bytes.Release();
                }
            }

            return(this.receiveQueue.Count > 0);
        }
Exemple #3
0
        write(IByteArray bytes, int offset, int length)
        {
            if (!need(length))
            {
                if (trace.enabled(1))
                {
                    trace.write(title + ": ByteArray length " + length + " too long");
                }
                close();
                throw SqlEx.get(ERR_GC4002_PROTOCOL_ERR);
            }

            if (trace.enabled(4))
            {
                trace.write(title + ": appending " + length);
            }
            length    = bytes.get(offset, length, buffer, data_ptr);
            data_ptr += length;
            return(length);
        }         // write
 public void Unpack(IByteArray reader, Queue <byte[]> receiveQueue)
 {
     if (!_isGetHead)
     {
         if (reader.Length >= 2)//2 is example, get msg's head length
         {
             var bodyLengthBytes = reader.Read(2);
             _bodyLength = BitConverter.ToUInt16(bodyLengthBytes, 0);
         }
         else
         {
             if (reader.Length >= _bodyLength)//get body
             {
                 var bytes = reader.Read(_bodyLength);
                 receiveQueue.Enqueue(bytes);
                 _isGetHead = false;
             }
         }
     }
 }
Exemple #5
0
            public static IByteArray SubArray(IByteArray arr, int start)
            {
                if (arr == null)
                {
                    throw new ArgumentNullException();
                }

                if ((start < 0) || (start > arr.Length))
                {
                    throw new InvalidOperationException();
                }

                IByteArray result = SubArray(arr, start, arr.Length - start);

                if (result.Length != (arr.Length - start))
                {
                    throw new ApplicationException();
                }

                return(result);
            }
Exemple #6
0
 {  /// <summary>
    /// Pack your message here(this is only an example)
    /// </summary>
    /// <param name="source"></param>
    /// <param name="unpackedHandler"></param>
     public void Unpack(IByteArray source, Action <byte[]> unpackedHandler)
     {
         // Unpack your message(use int, 4 byte as head)
         if (source.Length >= 4)
         {
             var head       = source.Read(4);
             int bodyLength = BitConverter.ToInt32(head, 0);// get body's length
             if (source.Length >= bodyLength)
             {
                 var unpacked = source.Read(bodyLength);// get body
                 unpackedHandler(unpacked);
             }
             else
             {
                 source.Insert(0, head);// rewrite in, used for next time
             }
         }
         else
         {
             source.Insert(0, source.Read(source.Length));// rewrite in, used for next time
         }
     }
        public override IElectionContext CreateElectionContext(IByteArray compressedContext)
        {
            GzipCompression compressor = new GzipCompression();

            IByteArray decompressedContext = compressor.Decompress(compressedContext);

            IDataRehydrator electionContextRehydrator = DataSerializationFactory.CreateRehydrator(decompressedContext);

            var version = electionContextRehydrator.RehydrateRewind <ComponentVersion <ElectionContextType> >();

            IElectionContext context = null;

            if (version.Type == ElectionContextTypes.Instance.Active)
            {
                if (version == (1, 0))
                {
                    context = new NeuraliumActiveElectionContext();
                }
            }

            if (version.Type == ElectionContextTypes.Instance.Passive)
            {
                if (version == (1, 0))
                {
                    context = new NeuraliumPassiveElectionContext();
                }
            }

            if (context == null)
            {
                throw new ApplicationException("Unrecognized election context version.");
            }

            context.Rehydrate(electionContextRehydrator, this);

            decompressedContext.Return();

            return(context);
        }
Exemple #8
0
            public static IByteArray SubArray(IByteArray arr, int start, int length)
            {
                if (arr == null)
                {
                    throw new ArgumentNullException();
                }

                if ((start < 0) || (length < 0) || ((start + length) <= arr.Length))
                {
                    throw new InvalidOperationException();
                }

                IByteArray result = MemoryAllocators.Instance.allocator.Take(length);

                Buffer.BlockCopy(arr.Bytes, arr.Offset + start, result.Bytes, result.Offset, length);

                if (result.Length != length)
                {
                    throw new ApplicationException();
                }

                return(result);
            }
    {  /// <summary>
       /// Pack your message here(this is only an example)
       /// </summary>
       /// <param name="source"></param>
       /// <param name="unpackedHandler"></param>
        public void Unpack(IByteArray source, Action <byte[]> unpackedHandler)
        {
            // Unpack your message(use int, 4 byte as head)
            var unpacked = source.Read(source.Length);// get body

            unpackedHandler(unpacked);

            /*
             * while (source.Length >= 4)
             * {
             *  var head = source.Read(4);
             *  int bodyLength = BitConverter.ToInt32(head, 0);// get body's length
             *  if (source.Length >= bodyLength)
             *  {
             *      var unpacked = source.Read(bodyLength);// get body
             *      unpackedHandler(unpacked);
             *  }
             *  else
             *  {
             *      source.Insert(0, head);// rewrite in, used for next time
             *  }
             * }
             */
        }
Exemple #10
0
 protected MsgBase(IByteArray byteArray)
 {
     ByteArray = byteArray;
 }
Exemple #11
0
 protected MsgBase()
 {
     ByteArray = new ByteArray();
 }
Exemple #12
0
 // readBytes
 /*
 ** Name: readBytes
 **
 ** Description:
 **	Read a ByteArray from the input stream.  ByteArray limits
 **	are ignored.  Any requested data which is available which
 **	exceeds the array limit is read and discarded.  It is the
 **	callers responsibility to ensure adaquate array limits
 **	and capacity.  Actual number of bytes read is limited to
 **	contents of current input buffer.
 **
 ** Input:
 **	length	    Number of bytes to place in array.
 **
 ** Output:
 **	bytes	    BytesArray filled from input stream
 **
 ** Returns:
 **	int	    Number of bytes actually read from input stream.
 **
 ** History:
 **	22-Sep-03 (gordy)
 **	    Created.
 */
 public int readBytes( IByteArray bytes, int length )
 {
     need( length, false );	// Make sure some data is available.
     length = Math.Min( length, avail() );
     bytes.put( buffer, data_ptr, length );  // Array limit overruns ignored!
     data_ptr += length;
     return( length );
 }
Exemple #13
0
 public void WriteByteArray(IByteArray data)
 {
     WriteBytes(data.Buffer, data.Offset + data.Position, data.Size - data.Position);
 }
 public DataRehydrator(IByteArray data, bool metadata = true) : this(data, data.Length, metadata)
 {
 }
Exemple #15
0
 public void Pack(IByteArray bytes)
 {
     bytes.Insert(0, new byte[] { 1 });
 }
 void OnMsg_Protobuf(IByteArray byteArray)
 {
     var        msg       = new MsgProtobuf(byteArray);
     GameObject testClass = msg.Read <GameObject>();//your class's type
     var        testName  = testClass.name;
 }
Exemple #17
0
 /// <summary>
 /// Unpack your message here(this is only an example)
 /// </summary>
 /// <param name="source"></param>
 /// <param name="packedHandler"></param>
 public void Pack(IByteArray source, Action <byte[]> packedHandler)
 {
     packedHandler(source.Read(source.Length));
 }
Exemple #18
0
 public MsgBytes(IByteArray byteArray) : base(byteArray)
 {
 }
Exemple #19
0
        /*
        ** Name: readBytes
        **
        ** Description:
        **	Reads bytes from the current input message into a
        **	IByteArray.  The array limit is ignored.  Any bytes
        **	which exceed the limit are read and discarded.
        **	It is the callers responsible to ensure adaquate
        **	limit and capacity.
        **
        ** Input:
        **	offset	    Offset in byte array.
        **	length	    Number of bytes.
        **
        ** Output:
        **	bytes	    Filled with bytes from message.
        **
        ** Returns:
        **	int	    Number of bytes actually read.
        **
        ** History:
        **	22-Sep-03 (gordy)
        **	    Created.
        */
        public int readBytes( IByteArray bytes, int length )
        {
            int requested = length;

            while( length > 0 )
            {
                need( length, false );
                int len = Math.Min( (int)in_msg_len, length );
                len = inBuff.readBytes( bytes, len );
                length -= len;
                in_msg_len -= (short)len;
            }

            return( requested - length );
        }
Exemple #20
0
 public virtual IInsertDebugConfirmWorkflow CreateDebugConfirmChainWorkflow(TransactionId guid, IByteArray hash)
 {
     return(new InsertDebugConfirmWorkflow(guid, hash, this.centralCoordinator));
 }
 public DataRehydrator(IByteArray data, int length, int maximumReadSize, bool metadata = true) : this(data, 0, length, maximumReadSize, metadata)
 {
 }
 public DataRehydrator(IByteArray data, int length, bool metadata = true) : this(data, length, length, metadata)
 {
 }
Exemple #23
0
        /*
        ** Name: write
        **
        ** Description:
        **	Append a ByteArray to current output message.  If buffer
        **	overflow would occur, the message (and array) is split.
        **
        ** Input:
        **	value	    The ByteArray to be appended.
        **
        ** Output:
        **	None.
        **
        ** Returns:
        **	void
        **
        ** History:
        **	 1-Dec-03 (gordy)
        **	    Created.
        */
        public void write(IByteArray bytes)
        {
            int end = bytes.valuelength();

            for( int offset = 0; offset < end;  )
            {
            if ( outBuff.avail() <= 0 )  split();
            int len = Math.Min( outBuff.avail(), end - offset );
            len = outBuff.write(bytes, offset, len);
            offset += len;
            }

            return;
        }
 public static IDataRehydrator CreateRehydrator(IByteArray data)
 {
     return(CreateRehydrator(data, data?.Length ?? 0));
 }
 public static IDataRehydrator CreateRehydrator(IByteArray data, int length)
 {
     return(CreateRehydrator(data, length, length));
 }
Exemple #26
0
        public void Unpack(IByteArray bytes)
        {
            MsgProtobuf msg = new MsgProtobuf(bytes);

            msg.Read <int>();
        }
Exemple #27
0
            /// <summary>
            /// 在此处理将要发送的数据添加长度消息id等
            /// </summary>
            /// <param name="bytes"></param>
            public void Pack(IByteArray source, Action <byte[]> packedHandler)
            {
                var @byte = source.Read(4);

                packedHandler(@byte);
            }
 public static IDataRehydrator CreateRehydrator(IByteArray data, int length, int maximumReadSize)
 {
     return(CreateRehydrator(data, 0, length, maximumReadSize));
 }
Exemple #29
0
 public MsgProtobuf(IByteArray byteArray) : base(byteArray)
 {
 }
Exemple #30
0
    public void Pack(IByteArray _source, Action <byte[]> _packHandler)
    {
        var _length = _source.Length;

        _packHandler(_source.Read(_source.Length));
    }
 void OnMsg_Bytes(IByteArray byteArray)
 {
     var msg    = new MsgBytes(byteArray);
     int getInt = msg.Read <int>();
 }
Exemple #32
0
 public void Unpack(IByteArray _source, Action <byte[]> _unpackHandler)
 {
     _unpackHandler(_source.Read(_source.Length));
 }
Exemple #33
0
        public override ITargettedMessageSet <IBlockchainEventsRehydrationFactory> RehydrateMessage(IByteArray data, TargettedHeader header, IBlockchainEventsRehydrationFactory rehydrationFactory)
        {
            IDataRehydrator dr = DataSerializationFactory.CreateRehydrator(data);

            IByteArray messageBytes = NetworkMessageSet.ExtractMessageBytes(dr);

            NetworkMessageSet.ResetAfterHeader(dr);
            IDataRehydrator messageRehydrator = DataSerializationFactory.CreateRehydrator(messageBytes);

            IBlockchainTargettedMessageSet messageSet = null;

            try {
                if (data?.Length == 0)
                {
                    throw new ApplicationException("null message");
                }

                short workflowType = 0;
                ComponentVersion <SimpleUShort> version = null;

                messageRehydrator.Peek(rehydrator => {
                    workflowType = rehydrator.ReadShort();

                    if (workflowType != NeuraliumWorkflowIDs.TEST)
                    {
                        throw new ApplicationException("Invalid workflow type");
                    }

                    version = rehydrator.Rehydrate <ComponentVersion <SimpleUShort> >();
                });

                switch (version.Type.Value)
                {
                case MESS1:

                    if (version == (1, 0))
                    {
                        messageSet = this.CreateTestWorkflowTriggerSet(header);
                    }

                    break;

                case MESS2:

                    if (version == (1, 0))
                    {
                        messageSet = this.CreateServerAnswerSet(header);
                    }

                    break;

                default:

                    throw new ApplicationException("invalid message type");
                }

                if (messageSet?.BaseMessage == null)
                {
                    throw new ApplicationException("Invalid message type or version");
                }

                messageSet.BaseHeader = header;                 // set the header explicitely
                messageSet.RehydrateRest(dr, rehydrationFactory);
            } catch (Exception ex) {
                Log.Error(ex, "Invalid data sent");
            }

            return(messageSet);
        }
Exemple #34
0
        /*
        ** Name: write
        **
        ** Description:
        **	Append a portion of a ByteArray to the output buffer.
        **	Number of bytes copied from ByteArray may be limited
        **	by current length of the array and requested position.
        **	This routine does not split arrays across buffers, the
        **	current buffer is flushed and the array must fit in
        **	the new buffer, or an exception is thrown.
        **
        ** Input:
        **	bytes	ByteArray.
        **	offset	Position of data in array.
        **	length	Length of data in array.
        **
        ** Output:
        **	None.
        **
        ** Returns:
        **	int	Number of bytes appended.
        **
        ** History:
        **	 1-Dec-03 (gordy)
        **	    Created.
        */
        public int write(IByteArray bytes, int offset, int length)
        {
            if (!need(length))
            {
                if (trace.enabled(1))
                    trace.write(title + ": ByteArray length " + length + " too long");
                close();
                throw SqlEx.get(ERR_GC4002_PROTOCOL_ERR);
            }

            if (trace.enabled(4)) trace.write(title + ": appending " + length);
            length = bytes.get(offset, length, buffer, data_ptr);
            data_ptr += length;
            return (length);
        }