Пример #1
0
 private static unsafe void AddSessionToPacket(UnmanagedMemory packet,byte[] sessionBytes,int index)
 {
     if (sessionBytes != null)
     {
         Marshal.Copy(sessionBytes, 0, (IntPtr)(packet.Handle + index), sessionBytes.Length);
     }
 }
Пример #2
0
 private static unsafe void AddHeaderToPacket(UnmanagedMemory packet, byte isPrice, byte sessionLength, byte[] contentLengthBytes)
 {
     packet.Handle[PacketConstants.SettingIndex] = isPrice;
     packet.Handle[PacketConstants.SessionLengthIndex] = sessionLength;
     const int startIndex = PacketConstants.ContentLengthIndex;
     Marshal.Copy(contentLengthBytes, 0, (IntPtr)(packet.Handle + startIndex), contentLengthBytes.Length);
 }
 public TraderMemoryStream(int capacity)
 {
     if (capacity < 0)
     {
         throw new ArgumentOutOfRangeException("capacity", "ArgumentOutOfRange_NegativeCapacity");
     }
     this._buffer = new UnmanagedMemory(capacity);
     this._capacity = capacity;
     this._expandable = true;
     this._writable = true;
     this._exposable = true;
     this._origin = 0;
     this._isOpen = true;
 }
Пример #4
0
 private static unsafe UnmanagedMemory BuildForCommandCommon(byte[] data, bool isPrice)
 {
     int packetLength = PacketConstants.HeadLength + data.Length;
     UnmanagedMemory packet =new UnmanagedMemory(packetLength);
     byte[] contentLengthBytes = CustomerIntCache.Get(data.Length);
     packet.Handle[PacketConstants.SettingIndex] = 0;
     if (isPrice)
     {
         byte priceByte = PacketFirstHeadByteValue.IsPrice;
         packet.Handle[PacketConstants.SettingIndex] = priceByte;
     }
     packet.Handle[PacketConstants.SessionLengthIndex] = 0;
     int offset = PacketConstants.ContentLengthIndex;
     Marshal.Copy(contentLengthBytes, 0, (IntPtr)(packet.Handle + offset), contentLengthBytes.Length);
     offset = PacketConstants.HeadLength;
     Marshal.Copy(data, 0, (IntPtr)(packet.Handle + offset), data.Length);
     return packet;
 }
Пример #5
0
 public static UnmanagedMemory GetKickoutPacket()
 {
     if (_KickoutPacket != null)
     {
         return _KickoutPacket;
     }
     lock (_Lock)
     {
         if (_KickoutPacket != null)
         {
             return _KickoutPacket;
         }
         byte[] contentBytes = PacketConstants.ContentEncoding.GetBytes(KickoutContent);
         byte[] contentLengthBytes = contentBytes.Length.ToCustomerBytes();
         byte[] packet = new byte[PacketConstants.HeadLength + contentBytes.Length];
         Buffer.BlockCopy(contentLengthBytes, 0, packet, PacketConstants.ContentLengthIndex, contentLengthBytes.Length);
         Buffer.BlockCopy(contentBytes, 0, packet, PacketConstants.HeadLength, contentBytes.Length);
         _KickoutPacket = new UnmanagedMemory(packet);
         return _KickoutPacket;
     }
 }
Пример #6
0
 public CommandForClient(UnmanagedMemory data = null, QuotationCommand quotationCommand = null, Command command = null)
 {
     if (command != null)
     {
         this._Command = command;
         this._Quotation = null;
         this._Data = null;
         this._CommandType = DataType.Command;
     }
     else if (quotationCommand != null)
     {
         this._Quotation = new QuotationTranslator(quotationCommand);
         this._Command = null;
         this._Data = null;
         this._CommandType = DataType.Quotation;
     }
     else
     {
         this._Data = data;
         this._Command = null;
         this._Quotation = null;
         this._CommandType = DataType.Response;
     }
 }
Пример #7
0
 public PacketContent(UnmanagedMemory mem)
 {
     this.UnmanageMem = mem;
     this.ContentType = ContentType.UnmanageMemory;
 }
Пример #8
0
 private static unsafe void AddContentToPacket(UnmanagedMemory packet, byte[] contentBytes,int index)
 {
     Marshal.Copy(contentBytes, 0, (IntPtr)(packet.Handle + index), contentBytes.Length);
 }
Пример #9
0
 private static unsafe UnmanagedMemory BuildForPointer(UnmanagedMemory source, string invokeID)
 {
     UnmanagedMemory content = ZlibHelper.ZibCompress(source.ToArray());
     source.Dispose();
     int contentLength = PacketConstants.InvokeIdLength + content.Length;
     int packetLength=PacketConstants.HeadLength + contentLength;
     UnmanagedMemory packet = new UnmanagedMemory(packetLength);
     byte[] contentLengthBytes = contentLength.ToCustomerBytes();
     packet.Handle[PacketConstants.SettingIndex] = PacketFirstHeadByteValue.IsPlainString;
     packet.Handle[PacketConstants.SessionLengthIndex] = 0;
     int offset = PacketConstants.ContentLengthIndex;
     Marshal.Copy(contentLengthBytes, 0, (IntPtr)(packet.Handle + offset), contentLengthBytes.Length);
     offset = PacketConstants.HeadLength;
     byte[] invokeIDBytes = PacketConstants.ClientInvokeIDEncoding.GetBytes(invokeID);
     Marshal.Copy(invokeIDBytes, 0, (IntPtr)(packet.Handle + offset), invokeIDBytes.Length);
     offset += invokeIDBytes.Length;
     CopyMemory((IntPtr)(packet.Handle + offset),(IntPtr)content.Handle, (uint)content.Length);
     content.Dispose();
     return packet;
 }
Пример #10
0
 private static UnmanagedMemory BuildForKeepAlive(SerializedInfo response)
 {
     Debug.Assert(response.Content.ContentType == ContentType.KeepAlivePacket, "content type should be keepalive");
     KeepAlive keepAlive = response.Content.KeepAlive;
     keepAlive.Packet[PacketConstants.SettingIndex] = keepAlive.IsSuccess ? PacketFirstHeadByteValue.IsKeepAliveAndSuccessValue : PacketFirstHeadByteValue.IsKeepAliveAndFailedValue;
     UnmanagedMemory packet = new UnmanagedMemory(keepAlive.Packet);
     return packet;
 }
Пример #11
0
 private static UnmanagedMemory BuildForGeneralFormat(SerializedInfo response)
 {
     if (!string.IsNullOrEmpty(response.ClientInfo.ClientInvokeId))
     {
         AppendClientInvokeIdToContentNode(response.Content, response.ClientInfo.ClientInvokeId);
     }
     byte[] contentBytes = GetContentBytes(response.Content);
     byte[] sessionBytes = GetSessionBytes(response.ClientInfo.Session.ToString());
     byte sessionLengthByte = (byte)sessionBytes.Length;
     byte[] contentLengthBytes = contentBytes.Length.ToCustomerBytes();
     int packetLength = PacketConstants.HeadLength + sessionLengthByte + contentBytes.Length;
     var packet = new UnmanagedMemory(packetLength);
     const byte priceByte = 0;
     AddHeaderToPacket(packet, priceByte, sessionLengthByte, contentLengthBytes);
     AddSessionToPacket(packet, sessionBytes, PacketConstants.HeadLength);
     AddContentToPacket(packet, contentBytes, PacketConstants.HeadLength + sessionLengthByte);
     return packet;
 }
Пример #12
0
 private unsafe void BeginWrite(UnmanagedMemory mem, int offset, int len)
 {
     try
     {
         if (mem.Data != null) // keep alive data or kickout packet
         {
             this._Stream.BeginWrite(mem.Data, 0, mem.Data.Length, this.EndWrite, null);
             return;
         }
         if (len <= MAX_WRITE_LENGTH)
         {
             Marshal.Copy((IntPtr)(mem.Handle + offset), this._Buffer, this._WriteBufferIndex, len);
             this._Stream.BeginWrite(this._Buffer, this._WriteBufferIndex, len, this.EndWrite, null);
         }
         else
         {
             this._LastWriteBuffer = mem;
             Marshal.Copy((IntPtr)(mem.Handle + offset), this._Buffer, this._WriteBufferIndex, MAX_WRITE_LENGTH);
             this._Stream.BeginWrite(this._Buffer, this._WriteBufferIndex, MAX_WRITE_LENGTH, this.EndWrite, null);
         }
     }
     catch(Exception ex)
     {
         _Logger.Error("Begin Write", ex);
         this.Close();
     }
 }
Пример #13
0
 private void WriteForQuotation()
 {
     Token token;
     TraderState state = SessionManager.Default.GetTokenAndState(this._Id, out token);
     if (state == null || token == null)
     {
         Write();
         return;
     }
     this._QuotationQueue.Clear();
     this._QuotationQueue.Add(this._CurrentCommand.Quotation.QuotationCommand);
     while (this._Queue.Count > 0)
     {
         CommandForClient item = this._Queue.Peek();
         if (item.CommandType != DataType.Quotation)
         {
             break;
         }
         item = this._Queue.Dequeue();
         this._QuotationQueue.Add(item.Quotation.QuotationCommand);
     }
     byte[] content;
     if (this._QuotationQueue.Count == 1)
     {
         content = this._CurrentCommand.Quotation.GetPriceInBytes(token, state);
     }
     else
     {
         QuotationCommand command = new QuotationCommand();
         command.Merge(this._QuotationQueue);
         content = QuotationTranslator.GetPriceInBytes(token, state, command, this._QuotationQueue[0].Sequence, this._QuotationQueue[this._QuotationQueue.Count - 1].Sequence);
     }
     if (content == null)
     {
         Write();
         return;
     }
     this._CurrentPacket = SerializeManager.Default.SerializePrice(content);
     this.BeginWrite(this._CurrentPacket, 0, this._CurrentPacket.Length);
 }
Пример #14
0
 private void WriteForCommand()
 {
     Token token;
     TraderState state = SessionManager.Default.GetTokenAndState(this._Id, out token);
     if (state == null || token == null)
     {
         Write();
         return;
     }
     byte[] data = CommandTranslator.GetDataForCommand(token, state, this._CurrentCommand.Command);
     if (data == null)
     {
         Write();
         return;
     }
     this._CurrentPacket = SerializeManager.Default.SerializeCommand(data);
     this.BeginWrite(this._CurrentPacket, 0, this._CurrentPacket.Length);
 }
Пример #15
0
        private void Write()
        {
            try
            {
                lock (this._QueueLock)
                {
                    if (this._Queue.Count == 0)
                    {
                        this._IsSendingData = false;
                        return;
                    }
                    this._CurrentCommand = this._Queue.Dequeue();
                    if (this._CurrentCommand.CommandType == DataType.Command)
                    {
                        WriteForCommand();
                    }
                    else if (this._CurrentCommand.CommandType == DataType.Response)
                    {
                        this._CurrentPacket = this._CurrentCommand.Data;
                        this.BeginWrite(this._CurrentPacket, 0, this._CurrentPacket.Length);
                    }
                    else if (this._CurrentCommand.CommandType == DataType.Quotation)
                    {
                        WriteForQuotation();
                    }
                    else
                    {
                        throw new ArgumentException(string.Format("Unrecognize DataType:{0}", this._CurrentCommand.CommandType));
                    }

                }
            }
            catch (Exception ex)
            {
                _Logger.Error("Write",ex);
                this.Close();
            }
        }
Пример #16
0
 private void EndWrite(IAsyncResult ar)
 {
     Action action = new Action(() =>
     {
         try
         {
             this._Stream.EndWrite(ar);
             if (this._LastWriteBuffer == null)
             {
                 this._CurrentPacket.Dispose();
                 Write();
                 return;
             }
             this._LastWriteIndex += MAX_WRITE_LENGTH;
             int len = this._LastWriteBuffer.Length - this._LastWriteIndex;
             if (len <= 0)
             {
                 this._LastWriteBuffer = null;
                 this._LastWriteIndex = 0;
                 this._CurrentPacket.Dispose();
                 Write();
                 return;
             }
             int lenToBeWrite = len >= MAX_WRITE_LENGTH ? MAX_WRITE_LENGTH : len;
             this.BeginWrite(this._LastWriteBuffer, this._LastWriteIndex, lenToBeWrite);
         }
         catch(Exception ex)
         {
             _Logger.Error("End write", ex);
             this.Close();
         }
     });
     Task task = new Task(action);
     TaskQueue.Default.Enqueue(task);
 }