Beispiel #1
0
        private void ParsePacket(int start, int end)
        {
            if (buffer[start] != 0x55 && buffer[start + 1] != 0xFF)
            {
                RaiseError(new Exception("Illegal start for packet! Expected 0x55, 0xFF and got " + printByte(buffer[start]) + ", " + printByte(buffer[start + 1]) + "!"));
                return;
            }
            if (buffer[end - 2] != 0x55 && buffer[end - 1] != 0x00)
            {
                RaiseError(new Exception("Illegal end for packet! Expected 0x55, 0x00 and got " + printByte(buffer[start]) + ", " + printByte(buffer[start + 1]) + "!"));
                return;
            }
            if (end - start < 5)
            {
                RaiseError(new Exception("Illegal packet size. Expected at least one byte between start and end of packet!"));
                return;
            }
            byte             messageType = buffer[start + 2];
            OnMessageHandler handler     = null;

            if (messageCallbacks.TryGetValue(messageType, out handler))
            {
                byte[] data = new byte[end - start - 5];
                Buffer.BlockCopy(buffer, start + 3, data, 0, end - start - 5);
                data = UnescapeStream(data);
                handler?.Invoke(this, new MessageEventArgs(data));
            }
            else
            {
                RaiseError(new Exception("Unknown message type " + printByte(messageType) + " received on input stream!"));
            }
        }
Beispiel #2
0
 protected void RaiseMessage(Message message, OnMessageHandler callback)
 {
     if (callback != null)
     {
         callback.Invoke(this, message);
     }
     else
     {
         _messages.Enqueue(message);
     }
 }
Beispiel #3
0
        public void OnBookMessage(L2PackageDto l2Book, BookType bookType)
        {
            if (l2Book.Type != L2PackageType.SNAPSHOT_FULL_REFRESH)
            {
                return;
            }
            AlgoDictionary[bookType] = l2Book;
            OnMessageHandler bookHandler = bookType == BookType.QUOTE ? QuoteMessageHandler :
                                           bookType == BookType.SOURCE ? SourceMessageHandler :
                                           bookType == BookType.TARGET ? TargetMessageHandler : HedgeMessageHandler;

            bookHandler?.Invoke(this);
        }
Beispiel #4
0
        public void Launch()
        {
            if (HostAddress == null)
            {
                UpdateStatus(ClientStatus.Disconnected);
                return;
            }

            Task.Run(() =>
            {
                var ipAddress = IPAddress.Parse(HostAddress.Ip);
                var remoteEp  = new IPEndPoint(ipAddress, HostAddress.Port);
                socket        = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                socket.Connect(remoteEp);

                UpdateStatus(ClientStatus.Connected);

                SendMessage("hello");

                while (socket != null && socket.Connected)
                {
                    try
                    {
                        var bytes    = new byte[1024];
                        var bytesRec = socket.Receive(bytes);
                        if (bytesRec == 0)
                        {
                            break;
                        }
                        var message = Encoding.ASCII.GetString(bytes, 0, bytesRec);
                        while (socket.Available > 0)
                        {
                            bytesRec = socket.Receive(bytes);
                            message += Encoding.ASCII.GetString(bytes, 0, bytesRec);
                        }

                        OnMessageHandler?.Invoke(message);
                    }
                    catch (Exception ex)
                    {
                        break;
                    }
                }
                UpdateStatus(ClientStatus.Disconnected);
                socket?.Close();
            });
        }
 public void Publish(T message)
 {
     OnMessageHandler?.Invoke(message);
 }