Esempio n. 1
0
        public void Write(byte[] buffer, int offset, int count, SocketsType type)
        {
            switch (type)

            {
            case SocketsType.Main:
                lock (_parser)
                {
                    _parser.Write(buffer, offset, count);
                }
                break;

            case SocketsType.History:
                lock (_parserHistory)
                {
                    _parserHistory.Write(buffer, offset, count);
                }
                break;

            case SocketsType.RealTime:
                lock (_parserRT)
                {
                    _parserRT.Write(buffer, offset, count);
                }
                break;
            }
        }
Esempio n. 2
0
        private void ClientReceive_CB(IAsyncResult result, SocketsType socketType)
        {
            Client client = (Client)result.AsyncState;

            try
            {
                //Debug.WriteLine("Server. Client::EndReceive");
                if (socketType == SocketsType.History)
                {
                    if (!client.EndReceiveHistory(result))
                    {
                        CloseClientSocket(client);
                        return;
                    }
                }
                else if (socketType == SocketsType.RealTime)
                {
                    if (!client.EndReceiveRT(result))
                    {
                        CloseClientSocket(client);
                        return;
                    }
                }
                else
                {
                    if (!client.EndReceive(result))
                    {
                        CloseClientSocket(client);
                        return;
                    }
                }
                if (!_stoped)
                {
                    DoReceiveAsync(client, socketType);
                }
                else
                {
                    DoLog("[SocketServer] Can't Start DoReceiveAsync on client cause stoped.");
                }
            }
            catch (BufferParser.BufferParserException ex)
            {
                Debug.WriteLine("BufferParser had an exception: " + ex.Message);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("EXCEPTION: ClientReceive_CB : " + ex.Message);

                if (CheckException(ex, _endReceiveExceptions))
                {
                    CloseClientSocket(client);
                    return;
                }
                throw;
            }
        }
Esempio n. 3
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);
                }
            }
        }
Esempio n. 4
0
        public void StartReceiving(SocketsType socketType)
        {
            if (socketType == SocketsType.RealTime)
            {
                BeginReceive(StandaloneBufferSize, Receive_CB_RT, socketType);
            }
            else if (socketType == SocketsType.History)
            {
                BeginReceive(StandaloneBufferSize, Receive_CB_History, socketType);
            }

            else
            {
                BeginReceive(StandaloneBufferSize, Receive_CB, socketType);
            }
        }
Esempio n. 5
0
        public void BeginReceive(int bufferSize, AsyncCallback callback, SocketsType socketType)
        {
            if (Disposed || _closed)
            {
                //Debug.WriteLine(string.Format("[SocketClient] Can't BeginReceive struct cause Disposed='{0}' or Close='{1}'", Disposed, _closed));
                return;
            }
            ////Debug.WriteLine("Client: BeginReceive");
            try
            {
                SocketError error;
                if (socketType == SocketsType.History)
                {
                    //InvokeLog("[SocketClient] BeginReceive History");
                    _socketHistory.BeginReceive(_bufferForReceive, _offsetForReceive, bufferSize, SocketFlags.None, out error, callback, this);
                }
                else if (socketType == SocketsType.RealTime)
                {
                    //InvokeLog("[SocketClient] BeginReceive RT");
                    _socketRT.BeginReceive(_bufferForReceive, _offsetForReceive, bufferSize, SocketFlags.None, out error, callback, this);
                }
                else
                {
                    //InvokeLog("[SocketClient] BeginReceive Main");
                    _socket.BeginReceive(_bufferForReceive, _offsetForReceive, bufferSize, SocketFlags.None, out error, callback, this);
                }

                if (error != SocketError.Success)
                {
                    InvokeLog("[SocketClient] ERROR: " + error.ToString());
                    //Debug.WriteLine(string.Format("[SocketClient] BeginReceive finished with 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);
            }
        }
Esempio n. 6
0
        private void DoReceiveAsync(Client client, SocketsType socketType)
        {
            try
            {
                //Debug.WriteLine("Server. Client::BeginReceive");
                client.BeginReceive(BufferSizeForAClient, result => { ClientReceive_CB(result, socketType); }, socketType);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("EXCEPTION: DoReceiveAsync : " + ex.Message);

                if (CheckException(ex, _beginReceiveExceptions))
                {
                    CloseClientSocket(client);
                    return;
                }
                throw;
            }
        }
Esempio n. 7
0
        private void AddClient(Socket socket, SocketsType socketType)
        {
            lock (_syncHandle)
            {
                Client client;
                if (socketType != SocketsType.Main)
                {
                    int index = -1;
                    index = _clients.IndexOf(_clients.Find(c => ((IPEndPoint)socket.RemoteEndPoint).Address.ToString() == c.IPadress.ToString() && (((IPEndPoint)socket.LocalEndPoint).Port == c.Port + 1 || ((IPEndPoint)socket.LocalEndPoint).Port == c.Port + 2)));
                    //index = _clients.Count - 1;
                    if (index == null || index == -1)
                    {
                        DoLog("ERROR: Socket dont have main client or that's just a MUX Connector!");
                        Debug.WriteLine("[AddClient] ERROR: Socket dont have main client or that's just a MUX Connector!");
                        Debug.WriteLine("\tSizeof(_clients) = {0}\n\t_clients.IP = {1}\n\tsocket.IP = {2}\n\tsocket.RemotPort={3}\n\tsocket.LocalPort={4}\n\t_client.Port={5}", _clients.Count, _clients[0].IPadress, ((IPEndPoint)socket.RemoteEndPoint).Address, ((IPEndPoint)socket.RemoteEndPoint).Port, ((IPEndPoint)socket.LocalEndPoint).Port, _clients[0].Port);
                        return;
                    }


                    if (socketType == SocketsType.History)
                    {
                        if (!_clients[index].ConnectedHistory)
                        {
                            _clients[index].AttachHistory(socket);
                            Debug.WriteLine("[AddClient] socketHistory attached to client {0}", _clients[index].Id);
                        }
                        else
                        {
                            Debug.WriteLine("[AddClient] socketHistory already exists, not attached to client {0}", _clients[index].Id);
                        }
                    }
                    else
                    {
                        if (!_clients[index].ConnectedRT)
                        {
                            _clients[index].AttachRT(socket);
                            Debug.WriteLine("[AddClient] socketRT attached to client {0}", _clients[index].Id);
                        }
                        else
                        {
                            Debug.WriteLine("[AddClient] socketRT already exists, not attached to client {0}", _clients[index].Id);
                        }
                    }


                    //start receiving on this new socket
                    DoReceiveAsync(_clients[index], socketType);
                }
                else
                {
                    client = _subscribersPool.Pop();



                    client.Attach(socket);
                    Debug.WriteLine("[AddClient] socket attached as client {0}", client.Id);
                    SetDesiredKeepAlive(socket);

                    if (!client.HasSubscriptionToData)
                    {
                        client.DataReceived         += ClientOnDataReceived;
                        client.HasSubscriptionToData = true;
                    }

                    _clients.Add(client);

                    IncommingConnection(this, (IPEndPoint)socket.RemoteEndPoint);



                    //start receiving on this new socket
                    DoReceiveAsync(client, socketType);
                }
            }
        }