Exemplo n.º 1
0
        public void Read()
        {
            int  number;
            bool res = true;

            while (res)
            {
                try
                {
                    number = GetNumberFromConsole("Выберите направление сортировки: 1 - А-Я, 2 - Я-А ");
                    res    = false;
                    ReadEvent?.Invoke(number);
                }
                catch (MyException ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    if (res)
                    {
                        Console.WriteLine("Попробуйте еще раз!");
                    }
                }
            }
        }
Exemplo n.º 2
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            SocketState state = (SocketState)ar.AsyncState;

            try
            {
                Socket client    = state.workSocket;
                int    bytesRead = client.EndReceive(ar);

                if (bytesRead > 0)
                {
                    state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));

                    if (client.Available == 0)
                    {
                        string response = state.sb.ToString();

                        state.sb.Clear();

                        if (LoggingLevel == 1)
                        {
                            OnLogWriteEntry(EventLogEntryCodes.SocketClientDataReceived, new string[] { string.Empty });
                        }
                        else if (LoggingLevel == 2)
                        {
                            OnLogWriteEntry(EventLogEntryCodes.SocketClientDataReceived, new string[] { response });
                        }

                        ReadEvent.Invoke(new Payload
                                         (
                                             ReadEvent.Id,
                                             new PayloadSubject[] { new PayloadSubject(ReadEvent.Subjects[0], response) }
                                         ));
                    }

                    client.BeginReceive(state.buffer, 0, SocketState.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
                }
            }
            catch (ObjectDisposedException)
            {
                ConnectionInError = true;
                // Connection Closed
            }
            catch (SocketException theSocketException)
            {
                OnSocketException(theSocketException);
            }
            catch (Exception theException)
            {
                ConnectionInError = true;

                if (!state.workSocket.Connected)
                {
                    InitialiseAfterDelay();
                }

                OnLogWriteEntry(EventLogEntryCodes.SocketClientException, new string[] { theException.Message });
            }
        }
Exemplo n.º 3
0
        public int Read(byte[] buffer, int offset, int count)
        {
            int readCount = parent.Read(buffer, offset, count);

            var args = new WaveProviderReadEventArgs(buffer, offset, count, readCount);

            ReadEvent?.Invoke(this, args);

            return(readCount);
        }
Exemplo n.º 4
0
        private void RecieveSocket(IAsyncResult ar)
        {
            var socket  = (System.Net.Sockets.Socket)ar.AsyncState;
            var localEp = socket.LocalEndPoint;
            var count   = socket.EndReceive(ar);

            var readEventArgs = new ReadEventArgs(_buffer, count, localEp);

            ReadEvent?.Invoke(this, readEventArgs);
            BeginRead();
        }
Exemplo n.º 5
0
        /// <summary>
        /// Callback call by stream.BeginRead after reception of newLength data
        /// </summary>
        /// <param name="ar">Not sure of what is it, but needed for terminate reading</param>
        private static void ReadCallback()
        {
            char character;
            int  data;

            while (client.Connected)
            {
                try
                {
                    data = stream.ReadByte();
                }
                catch (ObjectDisposedException)
                {
                    Console.WriteLine("Connection to server dropped (object disposed)!");
                    return;
                }
                catch (System.IO.IOException)
                {
                    Console.WriteLine("Connection to server dropped (other error)");
                    return;
                }

                if (data >= 0) // a data was read
                {
                    character = (char)data;

                    if (character != '\n')
                    {
                        message.Append(character);

                        if (receiveBuffer == null)
                        {
                            receiveBuffer = new byte[1];
                        }
                        else
                        {
                            Array.Resize <byte>(ref receiveBuffer, receiveBuffer.Length + 1); // resize currrent buffer
                        }
                        receiveBuffer[receiveBuffer.Length - 1] = (byte)data;
                    }
                    else // end of string found
                    {
                        readEvent?.Invoke(message.ToString(), receiveBuffer);

                        message.Clear();
                        receiveBuffer = null;
                    }
                }
            }
        }
Exemplo n.º 6
0
 public RFIDReader()
 {
     s = new SdSerial((string log) =>
     {
         //Console.WriteLine("Log: " + log);
     },
                      (byte[] message) =>
     {
         Console.WriteLine("NFC read with ID: " + BitConverter.ToString(message, 0, message.Length));
         ReadEvent?.Invoke(this, new NfcReaderReadEventArgs(message));
     },
                      commands,
                      31, 115200);
     s.setTrace(true);
     s.connect("COM6", "");
 }
Exemplo n.º 7
0
        /// <summary>
        /// Callback call by stream.BeginRead after reception of newLength data
        /// </summary>
        /// <param name="ar">Not sure of what is it, but needed for terminate reading</param>
        private static void ReadCallback(IAsyncResult ar)
        {
            int newLength = 1;
            int index     = -1;

            if (client.Connected)
            {
                int bytesRead;

                try
                {
                    // Terminate read operation, and get number of byte stored in buffer
                    bytesRead = stream.EndRead(ar);
                }
                catch (ObjectDisposedException e)
                {
                    Console.WriteLine("Connection to server dropped: " + e.ToString());
                    return;
                }
                catch (System.IO.IOException e)
                {
                    Console.WriteLine("Connection to server dropped: " + e.ToString());
                    return;
                }

                newLength = 1;
                index     = -1;

                // if number of byte read is not 0, concatenate string and buffer
                if (bytesRead > 0)
                {
                    //Console.WriteLine("Receive " + bytesRead + " data");

                    // Append new data to current string (expecting data are ascii)
                    message.Append(Encoding.ASCII.GetString(buffer, 0, bytesRead));
                    index = SearchCarriageReturn(buffer, bytesRead);
                }

                // if it remains received data, prepare for a new reading (get another buffer to append to current one)
                if (index == -1)
                {
                    if (client.Available > 0)
                    {
                        newLength = client.Available;
                        if (newLength > BufferMaxSize)
                        {
                            newLength = BufferMaxSize;
                        }
                        else
                        {
                            newLength = client.Available;
                        }
                    }
                }
                else
                {
                    string s = message.ToString();

                    // no more data to read, buffer and string can be send to upper level
                    readEvent?.Invoke(s);

                    message.Clear();

                    if (index != bytesRead - 1)
                    {
                        byte[] trailing = new byte[BufferMaxSize];

                        Array.Copy(buffer, index + 1, trailing, 0, bytesRead - index - 1);
                        message.Append(Encoding.ASCII.GetString(trailing, 0, bytesRead - index - 1));
                    }
                }

                // Prepare for reading new data
                if (newLength > BufferMaxSize)
                {
                    newLength = BufferMaxSize;
                }
                if (newLength <= 0)
                {
                    newLength = 1;
                }

                try
                {
                    stream.BeginRead(buffer, 0, newLength, new AsyncCallback(ReadCallback), message);
                }
                catch (ObjectDisposedException e)
                {
                    Console.WriteLine("Connection to server dropped: " + e.ToString());
                    return;
                }
                catch (System.IO.IOException e)
                {
                    Console.WriteLine("Connection to server dropped: " + e.ToString());
                    return;
                }
            }
        }
Exemplo n.º 8
0
 protected virtual void OnRead(TelnetReadEventArgs e)
 {
     ReadEvent?.Invoke(this, e);
 }
Exemplo n.º 9
0
 protected void onRead(VncReadEventArgs a_args)
 {
     ReadEvent?.Invoke(this, a_args);
 }