| | 1 | | /* |
| | 2 | | * Copyright 2018 Stanislav Muhametsin. All rights Reserved. |
| | 3 | | * |
| | 4 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
| | 5 | | * you may not use this file except in compliance with the License. |
| | 6 | | * You may obtain a copy of the License at |
| | 7 | | * |
| | 8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
| | 9 | | * |
| | 10 | | * Unless required by applicable law or agreed to in writing, software |
| | 11 | | * distributed under the License is distributed on an "AS IS" BASIS, |
| | 12 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| | 13 | | * implied. |
| | 14 | | * |
| | 15 | | * See the License for the specific language governing permissions and |
| | 16 | | * limitations under the License. |
| | 17 | | */ |
| | 18 | | using System; |
| | 19 | | using System.Collections.Generic; |
| | 20 | | using System.Text; |
| | 21 | | using System.Threading; |
| | 22 | | using UtilPack; |
| | 23 | |
|
| | 24 | | namespace CBAM.NATS.Implementation |
| | 25 | | { |
| | 26 | | internal sealed class NATSMessageImpl : NATSMessage |
| | 27 | | { |
| | 28 | | private Byte[] _data; |
| | 29 | | private Int32 _dataLength; |
| | 30 | |
|
| 5 | 31 | | public NATSMessageImpl( |
| 5 | 32 | | String subject, |
| 5 | 33 | | Int64 subID, |
| 5 | 34 | | String replyTo, |
| 5 | 35 | | Byte[] data, |
| 5 | 36 | | Int32 dataLength |
| 5 | 37 | | ) |
| | 38 | | { |
| 5 | 39 | | this.Subject = ArgumentValidator.ValidateNotEmpty( nameof( subject ), subject ); |
| 5 | 40 | | this.SubscriptionID = subID; |
| 5 | 41 | | this.ReplyTo = replyTo; |
| 5 | 42 | | this._data = data ?? Empty<Byte>.Array; |
| 5 | 43 | | this._dataLength = dataLength; |
| 5 | 44 | | } |
| | 45 | |
|
| 2 | 46 | | public String Subject { get; } |
| | 47 | |
|
| 0 | 48 | | public Int64 SubscriptionID { get; } |
| | 49 | |
|
| 1 | 50 | | public String ReplyTo { get; } |
| | 51 | |
|
| 4 | 52 | | public Int32 DataLength => this._data.Length; |
| | 53 | |
|
| | 54 | | public Int32 CopyDataTo( Byte[] array, Int32 offsetInMessage, Int32 offsetInArray, Int32 count ) |
| | 55 | | { |
| 2 | 56 | | if ( offsetInMessage < 0 ) |
| | 57 | | { |
| 0 | 58 | | offsetInMessage = 0; |
| 0 | 59 | | } |
| 2 | 60 | | else if ( offsetInMessage >= this._dataLength ) |
| | 61 | | { |
| 0 | 62 | | offsetInMessage = this._dataLength - 1; |
| | 63 | | } |
| | 64 | |
|
| 2 | 65 | | if ( count < 0 || count > this._dataLength - offsetInMessage ) |
| | 66 | | { |
| 2 | 67 | | count = this._dataLength - offsetInMessage; |
| | 68 | | } |
| | 69 | |
|
| 2 | 70 | | this._data.CopyTo( array, ref offsetInMessage, offsetInArray, count ); |
| | 71 | |
|
| 2 | 72 | | return count; |
| | 73 | | } |
| | 74 | |
|
| | 75 | | public Byte GetSingleByteAt( Int32 offsetInMessage ) |
| | 76 | | { |
| 0 | 77 | | if ( offsetInMessage < 0 || offsetInMessage >= this._dataLength ) |
| | 78 | | { |
| 0 | 79 | | throw new ArgumentException( nameof( offsetInMessage ) ); |
| | 80 | | } |
| 0 | 81 | | return this._data[offsetInMessage]; |
| | 82 | | } |
| | 83 | |
|
| | 84 | | internal void SetData( Byte[] data, Int32 dataLength ) |
| | 85 | | { |
| 0 | 86 | | Interlocked.Exchange( ref this._data, data ?? Empty<Byte>.Array ); |
| 0 | 87 | | Interlocked.Exchange( ref this._dataLength, dataLength ); |
| 0 | 88 | | } |
| | 89 | | } |
| | 90 | |
|
| | 91 | | internal sealed class NATSPublishCompletedImpl : NATSPublishCompleted |
| | 92 | | { |
| | 93 | |
|
| | 94 | | public static NATSPublishCompleted Instance { get; } = new NATSPublishCompletedImpl(); |
| | 95 | |
|
| | 96 | | private NATSPublishCompletedImpl() |
| | 97 | | { |
| | 98 | |
|
| | 99 | | } |
| | 100 | | } |
| | 101 | | } |