Пример #1
0
 private void ClientOnDataReceived(ClientRabbit client, IParserStruct structure)
 {
     if (DataReceived != null)
     {
         DataReceived(client, structure);
     }
 }
Пример #2
0
        public void StartSending(IParserStruct structure, string messageId)
        {
            try
            {
                switch (structure.Id)
                {
                case 7:
                case 8:
                case 12:
                case 13:
                case 14:
                    _BUFFER_SIZE = 40 * 1024;
                    break;

                default:
                    _BUFFER_SIZE = 1024;
                    break;
                }
                byte[] body = new byte[_BUFFER_SIZE];
                structure.WriteBytes(body, 0);
                _channel.BasicPublish("", messageId, null, body);
            }
            catch (Exception ex)
            {
                //InvokeLog("EXCEPTION: StartSending : " + ex.Message);
            }

            return;
        }
Пример #3
0
 public void SendStruct(IParserStruct @struct, string messageId, Action onCompleted)
 {
     switch (@struct.Id)
     {
     default:
         StartSending(@struct, messageId);
         break;
     }
     onCompleted();
 }
Пример #4
0
        public void BeginSend(IParserStruct structure, AsyncCallback callback, SocketsType socketType)
        {
            lock (_sendSync)
            {
                if (Disposed || _closed)
                {
                    //Debug.WriteLine(string.Format("[SocketClient] Can't BeginSend cause Disposed='{0}' or Close='{1}'", Disposed,_closed));
                    InvokeOnException(new Exception(string.Format("[SocketClient] Can't BeginSend cause Disposed='{0}' or Close='{1}'", Disposed,
                                                                  _closed)), true);

                    return;
                }

                structure.WriteBytes(_bufferForSend, _offsetForSend);
                //we add 8 bytes to strcuture length, cause every structure has a header created
                //from 2 Int32 fields
                //Structue.Length - reports the length of structure itself, without header
                try
                {
                    SocketError error;
                    if (socketType == SocketsType.History)
                    {
                        _socketHistory.BeginSend(_bufferForSend, _offsetForSend, structure.Length + 8, SocketFlags.None, out error, callback, this);
                    }
                    else if (socketType == SocketsType.RealTime)
                    {
                        _socketRT.BeginSend(_bufferForSend, _offsetForSend, structure.Length + 8, SocketFlags.None, out error, callback, this);
                    }
                    else
                    {
                        _socket.BeginSend(_bufferForSend, _offsetForSend, structure.Length + 8, SocketFlags.None, out error, callback, this);
                    }
                    if (error != SocketError.Success)
                    {
                        InvokeLog("[SocketClient] ERROR: " + error.ToString());
                        //Debug.WriteLine(string.Format("[SocketClient] Can't begin cause of error '{0}'", error));
                        if (error == SocketError.ConnectionReset || error == SocketError.ConnectionAborted || error == SocketError.NotConnected || error == SocketError.ConnectionRefused)
                        {
                            InvokeOnException(new Exception("Connection Closed - " + error), true);
                        }
                    }
                }
                catch (Exception exception)
                {
                    //Debug.WriteLine(string.Format("[SocketClient] {0}", exception.Message));
                    if (!StandaloneUsage)
                    {
                        throw;
                    }
                    InvokeOnException(exception, true);
                }
            }
        }
Пример #5
0
        public void Broadcast(IParserStruct @struct)
        {
            List <Client> _clientsToDelete = new List <Client>();

            foreach (Client client in _clients)
            {
                Client cl = client;
                try
                {
                    cl.BeginSend(@struct,
                                 result =>
                    {
                        try
                        {
                            int res = cl.EndSend(result);
                            if (res == -1)
                            {
                                _clientsToDelete.Add(cl);
                            }
                        }
                        catch (Exception ex)
                        {
                            if (!CheckException(ex, _endReceiveExceptions))
                            {
                                throw;
                            }
                            _clientsToDelete.Add(cl);
                        }
                    }, SocketsType.Main);
                }
                catch (Exception ex)
                {
                    if (!CheckException(ex, _endReceiveExceptions))
                    {
                        throw;
                    }
                    _clientsToDelete.Add(cl);
                }
            }

            lock (_clients)
            {
                foreach (var client in _clientsToDelete)
                {
                    _clients.Remove(client);
                    CloseClientSocket(client);
                }
            }
        }
Пример #6
0
        public void SendStruct(IParserStruct @struct, Action onCompleted)
        {
            switch (@struct.Id)
            {
            case 1:     //Authentication
            case 2:     //AuthenticationAnswer
            case 3:     //Symbol
            case 4:     //SymbolRequest
            case 5:     //Subscribe
            case 7:     //HistoryRequest
            case 9:     //Ping
            case 10:    //Snapshot
                BeginSend(@struct, result =>
                {
                    EndSend(result);
                    onCompleted();
                }, SocketsType.Main);
                break;

            case 8:     //BarData
                BeginSend(@struct, result =>
                {
                    EndSendHistory(result);
                    onCompleted();
                }, SocketsType.History);
                break;

            case 6:     //TickData
                BeginSend(@struct, result =>
                {
                    EndSendRT(result);
                    onCompleted();
                }, SocketsType.RealTime);
                break;
            }
        }
Пример #7
0
        public void Parse()
        {
            lock (_stream)
            {
                int step         = 0;
                int structLength = -1;



                IParserStruct currentStruct = null;



                //reset stream Position, After writing it points to the end of stream
                _stream.Position = 0;



                while (_stream.Position + 8 <= _stream.Length) //8 cause we need at least 8 bytes to start
                {
                    if (step == 0)
                    {
                        //read Id - it must be always at first place
                        _stream.Read(_buffer, 0, 8); //8 bytes for StructId and its Length
                        int structId = BitConverter.ToInt32(_buffer, 0);


                        Type currentStructType;
                        if (!Structs.TryGetValue(structId, out currentStructType))
                        {
                            Debug.WriteLine(
                                string.Format(
                                    "ERROR Getting value for structID {2} - Stream Length: {0}. Standalone: {1}",
                                    _stream.Length, StandaloneUsage, structId));
                            _stream.SetLength(0);
                            _stream.Seek(0, SeekOrigin.Begin);
                            return;
                            //throw new BufferParserException(string.Format("Structure with ID = {0} is not supported.", structId));
                        }



                        currentStruct = (IParserStruct)Activator.CreateInstance(currentStructType);



                        //read struct Length
                        structLength = BitConverter.ToInt32(_buffer, 4);



                        step = 1;
                        continue;
                    }
                    if (step == 1)
                    {
                        if (currentStruct == null || structLength == -1)
                        {
                            throw new BufferParserException("Got to read the structure, but not created.");
                        }
                        if ((structLength + _stream.Position <= _stream.Length) && (structLength + _stream.Position <= _buffer.Length))
                        {
                            _stream.Read(_buffer, 0, structLength);
                            currentStruct.ReadBytes(_buffer, 0);
                            //make an additional check to ensure struct was read OK
                            //if (!currentStruct.CrcOk())
                            //  throw new BufferParserException("Structure was read, but its CRC is wrong.");


                            //if is ok, raise the event that structure was read succesfully
                            if (!IsHistorical)
                            {
                                StructRead(this, new List <IParserStruct>()
                                {
                                    currentStruct
                                });
                            }
                            else
                            {
                                lock (StructsRead) StructsRead.Add(currentStruct);
                            }



                            //go to read another structure
                            step = 0;
                        }
                        else
                        {
                            break; //nothing to read
                        }
                    }
                }
                if (step == 1)
                {
                    _stream.Position -= 8;
                }



                int shiftBufferLength = (int)(_stream.Length - _stream.Position);
                if (shiftBufferLength > 0)
                {
                    _stream.Read(_buffer, 0, shiftBufferLength);
                    _stream.SetLength(0);
                    _stream.Seek(0, SeekOrigin.Begin);
                    _stream.Write(_buffer, 0, shiftBufferLength);
                }
                else
                {
                    _stream.SetLength(0);
                    _stream.Seek(0, SeekOrigin.Begin);
                }
            }
        }
Пример #8
0
        public List <IParserStruct> Parse(byte[] message)
        {
            List <IParserStruct> result = new List <IParserStruct>();

            try
            {
                MemoryStream _stream = new MemoryStream(message.Length);
                _stream.Write(message, 0, message.Length);

                byte[] _buffer      = new byte[40 * 1024];
                int    step         = 0;
                int    structLength = -1;

                IParserStruct currentStruct = null;

                //reset stream Position, After writing it points to the end of stream
                _stream.Position = 0;

                while (_stream.Position + 8 <= _stream.Length) //8 cause we need at least 8 bytes to start
                {
                    if (step == 0)
                    {
                        //read Id - it must be always at first place
                        _stream.Read(_buffer, 0, 8); //8 bytes for StructId and its Length
                        int structId = BitConverter.ToInt32(_buffer, 0);

                        Type currentStructType;
                        if (!_structures.TryGetValue(structId, out currentStructType))
                        {
                            Log(string.Format("ERROR Getting value for structID {2} - Stream Length: {0}.", structId, _stream.Length));
                            _stream.SetLength(0);
                            _stream.Seek(0, SeekOrigin.Begin);
                            return(result);
                            //throw new BufferParserException(string.Format("Structure with ID = {0} is not supported.", structId));
                        }

                        currentStruct = (IParserStruct)Activator.CreateInstance(currentStructType);

                        //read struct Length
                        structLength = BitConverter.ToInt32(_buffer, 4);

                        step = 1;
                        continue;
                    }
                    if (step == 1)
                    {
                        if (currentStruct == null || structLength == -1)
                        {
                            Log("Got to read the structure, but not created.");
                        }
                        if (structLength + _stream.Position <= _stream.Length)
                        {
                            _stream.Read(_buffer, 0, structLength);
                            currentStruct.ReadBytes(_buffer, 0);
                            //make an additional check to ensure struct was read OK
                            //if (!currentStruct.CrcOk())
                            //  throw new BufferParserException("Structure was read, but its CRC is wrong.");

                            //if is ok, raise the event that structure was read succesfully
                            result.Add(currentStruct);

                            //go to read another structure
                            step = 0;
                        }
                        else
                        {
                            break; //nothing to read
                        }
                    }
                }
                if (step == 1)
                {
                    _stream.Position -= 8;
                }

                int shiftBufferLength = (int)(_stream.Length - _stream.Position);
                if (shiftBufferLength > 0)
                {
                    _stream.Read(_buffer, 0, shiftBufferLength);
                    _stream.SetLength(0);
                    _stream.Seek(0, SeekOrigin.Begin);
                    _stream.Write(_buffer, 0, shiftBufferLength);
                }
                else
                {
                    _stream.SetLength(0);
                    _stream.Seek(0, SeekOrigin.Begin);
                }
            }
            catch (Exception ex) { /*Log("EXCEPTION Parse() "+ex.Message);*/ }
            return(result);
        }