Exemple #1
0
        /// <summary>
        /// Refer to <see cref="OdbcCommand.ExecuteScalar"/>
        /// </summary>
        /// <param name="sql">SQL request to run</param>
        /// <returns>The scalar value if successfull, -1 in an error has occurred</returns>
        public int SelectScalar(string sql)
        {
            int scalar = -1;

            if (IsOpen && !string.IsNullOrEmpty(sql))
            {
                sql = TryToFixTrueFalseTesting(sql);
                CLog.DEBUG($"{MethodBase.GetCurrentMethod().Module.Name}.{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}", $"SQL: {sql}");

                OdbcDataAdapter da = new OdbcDataAdapter();
                da.SelectCommand = new OdbcCommand();
                try
                {
                    da.SelectCommand.Connection  = Database;
                    da.SelectCommand.CommandText = sql;
                    scalar = (int)da.SelectCommand.ExecuteScalar();
                }
                catch (Exception ex)
                {
                    CLog.AddException($"{MethodBase.GetCurrentMethod().Module.Name}.{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}", ex);
                }
                finally
                {
                    da.SelectCommand.Dispose();
                }
            }
            return(scalar);
        }
Exemple #2
0
        /// <summary>
        /// Extract a PI from the given position in a buffer and returns the position of next PI (eventually) inside this buffer.
        /// Throws exceptions if the PI to extract is badly formated (size doesn't match PI,...)
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="start"></param>
        /// <param name="offset"></param>
        /// <returns>A PI object if found, null otherwise</returns>
        public static CPI ExtractPI(byte[] buffer, int start, out int offset)
        {
            // offset is at least the starting position
            offset = start;
            CPI cpi = null;

            // do we have anough space for a PGI+LGI
            if (CPI.PI_HEADER_SIZE <= buffer.Length - offset)
            {
                // we do... try to create a PI from the buffer
                try
                {
                    cpi     = new CPI(buffer[offset], buffer[offset + CPI.PI_PI_SIZE]);
                    offset += CPI.PI_HEADER_SIZE;
                    cpi.SetDataBytes(buffer, offset);
                    // arrived here the PI has been created
                    offset += cpi.PILen;
                }
                catch (Exception ex)
                {
                    CLog.AddException(MethodBase.GetCurrentMethod().Name, ex);
                    cpi = null;
                }
            }
            return(cpi);
        }
Exemple #3
0
        /// <summary>
        /// Selects a set of objects from the database and returns them inside a <see cref="DataSet"/> object to be extracted by the caller
        /// </summary>
        /// <param name="command">A complete <see cref="OdbcCommand"/> detailing a select request to launch</param>
        /// <param name="dataSet">An <see cref="DataSet"/> object which can be used to fetch data from the result set</param>
        /// <returns>True if successfull, false otherwise</returns>
        public bool SelectRequest(OdbcCommand command, ref DataSet dataSet)
        {
            bool f = false;

            if (IsOpen && null != command)
            {
                OdbcDataAdapter da = null;
                try
                {
                    command.Connection = Database;
                    da = new OdbcDataAdapter();
                    da.SelectCommand            = command;
                    da.SelectCommand.Connection = Database;
                    dataSet = new DataSet();
                    da.Fill(dataSet);
                    f = true;
                }
                catch (Exception ex)
                {
                    CLog.AddException($"{MethodBase.GetCurrentMethod().Module.Name}.{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}", ex);
                }
                finally
                {
                    if (null != da)
                    {
                        da.Dispose();
                    }
                }
            }
            return(f);
        }
Exemple #4
0
        /// <summary>
        /// Search a specific TLC in a buffer
        /// </summary>
        /// <param name="tag">The TLV tag to find</param>
        /// <param name="buffer">The buffer to loolk the TLV inside</param>
        /// <returns>A CTLV object</returns>
        public static CTLV SearchTLV(string tag, byte[] buffer)
        {
            CTLV tlv = null;
            int  offset = 0;
            bool fFound = false, fOK = true;

            do
            {
                int start = offset;
                try
                {
                    // extract the current PI
                    tlv = ExtractTLV(buffer, start, out offset);
                }
                catch (Exception ex)
                {
                    CLog.AddException(MethodBase.GetCurrentMethod().Name, ex);
                    fOK = false;
                    tlv = null;
                }
                if (null != tlv)
                {
                    // a PI was found, is it the one we're looking for ?
                    fFound = tlv.T == tag;
                    if (!fFound)
                    {
                        tlv = null;
                    }
                }
            }while (!fFound && buffer.Length > offset && fOK);
            return(tlv);
        }
Exemple #5
0
 /// <summary>
 /// Open the adequate stream to the server.
 /// </summary>
 /// <param name="client">TCP client to use to open the stream</param>
 /// <param name="settings">Settings to use when manipulating the stream</param>
 public CStreamServerIO(TcpClient client, CStreamServerSettings settings) : base(client, settings.LengthBufferSize)
 {
     Port     = settings.Port;
     Settings = settings;
     if (settings.UseSsl)
     {
         // SSL stream
         sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
         int tmo = client.ReceiveTimeout * 1000;
         client.ReceiveTimeout = 5000;
         try
         {
             // authenticate the server but don't require the client to authenticate.
             sslStream.AuthenticateAsServer(settings.ServerCertificate);
         }
         catch (Exception ex)
         {
             CLog.AddException($"{MethodBase.GetCurrentMethod().Module.Name}.{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}", ex);
             sslStream = null;
             throw;
         }
         finally
         {
             client.ReceiveTimeout = tmo;
         }
     }
     else
     {
         // standard Stream
         networkStream = client.GetStream();
     }
 }
Exemple #6
0
        /// <summary>
        /// Open the adequate stream to the server.
        /// </summary>
        /// <param name="client">TCP client to use to open the stream</param>
        /// <param name="settings">Settings to use when manipulating the stream</param>
        public CStreamClientIO(TcpClient client, CStreamClientSettings settings) : base(client, settings.LengthBufferSize)
        {
            if (settings.IsValid)
            {
                Settings = settings;
                // determine the kind of link to use
                if (Settings.UseSsl)
                {
                    sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
                    int tmo = client.ReceiveTimeout * 1000;
                    client.ReceiveTimeout = 5000;
                    try
                    {
                        // authenticate aginst the server

#if NET35
                        // check if TLS is supported
                        bool useTLS = false;
                        try
                        {
                            RegistryKey key   = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5");
                            object      value = (null != key ? key.GetValue("SP") : null);
                            useTLS = (null != value && 1 <= (int)value);
                        }
                        catch (Exception ex) { }
                        CLog.Add($".NET 3.5 {(useTLS ? "using TLS 1.2" : "not using TLS")}", TLog.INFOR);
                        if (useTLS)
                        {
                            sslStream.AuthenticateAsClient(Settings.ServerName, null, (System.Security.Authentication.SslProtocols) 3072, false);
                        }
                        else
                        {
                            sslStream.AuthenticateAsClient(Settings.ServerName);
                        }
#else
                        sslStream.AuthenticateAsClient(Settings.ServerName);
#endif
                    }
                    catch (Exception ex)
                    {
                        CLog.AddException($"{MethodBase.GetCurrentMethod().Module.Name}.{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}", ex);
                        sslStream = null;
                        throw;
                    }
                    finally
                    {
                        client.ReceiveTimeout = tmo;
                    }
                }
                else
                {
                    networkStream = client.GetStream();
                }
            }
            else
            {
                throw new Exception("Invalid client stream settings.");
            }
        }
Exemple #7
0
 /// <summary>
 /// Get the requested field
 /// </summary>
 /// <param name="field">Field id to get</param>
 /// <returns>The field if found, <see langword="null"/>otherwise</returns>
 public CField GetField(int field)
 {
     try
     {
         return(Fields[field]);
     }
     catch (Exception ex)
     {
         CLog.AddException(MethodBase.GetCurrentMethod().Name, ex, "CHAMP: " + field);
         return(null);
     }
 }
Exemple #8
0
 /// <summary>
 /// Get a TLV from the list of TLV
 /// </summary>
 /// <param name="id"></param>
 /// <returns></returns>
 public CTLV GetTLV(string id)
 {
     try
     {
         return(_tlv[id]);
     }
     catch (Exception ex)
     {
         CLog.AddException(MethodBase.GetCurrentMethod().Name, ex, "T: " + id);
         return(null);
     }
 }
Exemple #9
0
 /// <summary>
 /// Remove a TLV record from the field
 /// </summary>
 /// <param name="tlv">The fully formatted TLV record</param>
 /// <returns></returns>
 public bool RemoveTLV(CTLV tlv)
 {
     try
     {
         _tlv.Remove(tlv.T);
         return(true);
     }
     catch (Exception ex)
     {
         CLog.AddException(MethodBase.GetCurrentMethod().Name, ex, "TLV: " + tlv.ToString());
         return(false);
     }
 }
Exemple #10
0
 /// <summary>
 /// Add a connection for that endpoint
 /// </summary>
 /// <param name="accepted"></param>
 /// <returns></returns>
 public bool AddConnection(bool accepted)
 {
     try
     {
         Connections.Add(new Connection(accepted));
         return(true);
     }
     catch (Exception ex)
     {
         CLog.AddException($"{MethodBase.GetCurrentMethod().Module.Name}.{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}", ex);
     }
     return(false);
 }
Exemple #11
0
 /// <summary>
 /// Retrieve the value of a column inside an <see cref="OdbcDataAdapter"/>
 /// </summary>
 /// <param name="reader">The reader to look inside</param>
 /// <param name="columnName">The column name to fetch</param>
 /// <param name="value">The content of the column it it exists, null otherwise</param>
 /// <returns>True if the column has been found and its value returned to the caller, false otherwise</returns>
 public static bool ItemValue <TnX>(OdbcDataReader reader, string columnName, ref TnX value)
 {
     value = default(TnX);
     try
     {
         value = (TnX)reader[columnName];
         return(true);
     }
     catch (Exception ex)
     {
         CLog.AddException($"{MethodBase.GetCurrentMethod().Module.Name}.{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}", ex, $"Column name: {columnName}");
     }
     return(false);
 }
Exemple #12
0
 ///// <summary>
 ///// Add a TLV field to the message
 ///// </summary>
 ///// <param name="f">The field to add <see cref="CField"/></param>
 ///// <returns>The string representation of the field</returns>
 //public string AddField(CFieldTLV f)
 //	{
 //	try
 //		{
 //		Fields.Add(f.ID, f);
 //		Bitmap.SetBit(f.ID);
 //		return f.ToString();
 //		}
 //	catch (Exception ex)
 //		{
 //		CLog.AddException(MethodBase.GetCurrentMethod().Name, ex);
 //		return string.Empty;
 //		}
 //	}
 /// <summary>
 /// Remove a field from the APDU
 /// </summary>
 /// <param name="field">Field id to remove</param>
 /// <returns>TRUE if removed, FALSE otherwise (it could not be there)</returns>
 public bool RemoveField(int field)
 {
     try
     {
         Fields.Remove(field);
         Bitmap.UnsetBit(field);
         return(true);
     }
     catch (Exception ex)
     {
         CLog.AddException(MethodBase.GetCurrentMethod().Name, ex, "CHAMP: " + field);
         return(false);
     }
 }
Exemple #13
0
        /// <summary>
        /// Retrieve the value of a column inside an <see cref="OdbcDataAdapter"/>
        ///
        /// WARNING THIS FUNCTION MAY RAISE AN EXCEPTION
        ///
        /// </summary>
        /// <param name="reader">The reader to look inside</param>
        /// <param name="columnName">The column name to fetch</param>
        /// <returns>The content of the column it it exists, AN EXCEPTION IF AN ERROR OCCURS</returns>
        public static object ItemValue <TnX>(OdbcDataReader reader, string columnName)
        {
            object value = null;

            try
            {
                value = reader[columnName];
                return(value);
            }
            catch (Exception ex)
            {
                CLog.AddException($"{MethodBase.GetCurrentMethod().Module.Name}.{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}", ex, $"Column name: {columnName}");
                throw;
            }
        }
Exemple #14
0
 /// <summary>
 /// Calls the <see cref="Win32UIActivityDelegate"/> method which is in charge of taking care of the activity, passing it the arguments
 /// </summary>
 /// <param name="method">The <see cref="Win32UIActivityDelegate"/> method to call</param>
 /// <param name="activity">The <see cref="UIActivity"/> object to pass to the method</param>
 public static void AddActivity(Win32UIActivityDelegate method, UIActivity activity)
 {
     try
     {
         if (null != method && null != activity && null != activity.Ctrl)
         {
             activity.Ctrl.Invoke(method, activity);
         }
         else
         {
             CLog.Add($"{activity} could not be added", TLog.ERROR);
         }
     }
     catch (Exception ex)
     {
         CLog.AddException($"{MethodBase.GetCurrentMethod().Module.Name}.{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}", ex, $"Exception while processing {activity}");
     }
 }
Exemple #15
0
        /// <summary>
        /// Creates an APDU from an existing buffer building a database of fields as contained inside the APDU
        /// </summary>
        /// <param name="buffer">The buffer to scan to search the fields.</param>
        public CAPDU(byte[] buffer)
        {
            int offset = 0;

            // get message ID
            ID = new CAPDUID(buffer, offset, ref offset);
            if (0 != ID.Value)
            {
                // Get the bitmap
                Bitmap = new CBitmap(buffer, offset, ref offset);
                if (Bitmap.IsSet)
                {
                    // arrived here we can process the fields described inside the bitmap
                    CFields fields = new CFields();

                    // offset now points to the beginning of the fields - WE ONLY CONSIDER BITMAP1 as bitmap2 fields are not described in the dictionary
                    for (int field = 2; field <= 0xFF && 0 < buffer.Length - offset; field++)
                    {
                        if (Bitmap.IsBitSet(field))
                        {
                            // create and retrieve field
                            //CField fg = fields.GetField(field);
                            //CField f = new CField(fg.ID, fg.Type, fg.Minlen, fg.Maxlen);
                            CField f = fields.GetField(field);
                            if (f.SetData(buffer, offset, out offset))
                            {
                                // save the field inside the list of fields
                                try
                                {
                                    AddField(f);
                                }
                                catch (Exception ex)
                                {
                                    CLog.AddException(MethodBase.GetCurrentMethod().Name, ex, "CHAMP: " + field);
                                }
                            }
                        }
                    }
                }
            }
        }
Exemple #16
0
 /// <summary>
 /// Add a field to the message
 /// </summary>
 /// <param name="f">The field to add <see cref="CField"/></param>
 /// <returns>The string representation of the field</returns>
 public string AddField(CField f)
 {
     try
     {
         if (0 != f.RawdataLen)
         {
             Fields.Add(f.ID, f);
             Bitmap.SetBit(f.ID);
             return(f.ToString());
         }
         else
         {
             return(string.Empty);
         }
     }
     catch (Exception ex)
     {
         CLog.AddException(MethodBase.GetCurrentMethod().Name, ex);
         return(string.Empty);
     }
 }
Exemple #17
0
 /// <summary>
 /// Execute a non select request (insert, update, delete)
 /// </summary>
 /// <param name="command">A complete <see cref="OdbcCommand"/> detailing a non select request to launch</param>
 /// <param name="nbRows">Number of rows impacted by the request</param>
 /// <returns>True if the request was successfull processed, false otherwise</returns>
 public bool NonSelectRequest(OdbcCommand command, ref int nbRows)
 {
     nbRows = 0;
     if (IsOpen && null != command)
     {
         try
         {
             command.Connection = Database;
             nbRows             = command.ExecuteNonQuery();
             return(true);
         }
         catch (Exception ex)
         {
             CLog.AddException($"{MethodBase.GetCurrentMethod().Module.Name}.{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}", ex, $"SQL: {command.CommandText}");
         }
         finally
         {
             command.Dispose();
         }
     }
     return(false);
 }
Exemple #18
0
        /// <summary>
        /// Selects a set of objects from the database and returns them inside a <see cref="OdbcDataAdapter"/> object to be extracted by the caller
        /// </summary>
        /// <param name="command">A complete <see cref="OdbcCommand"/> detailing a select request to launch</param>
        /// <param name="reader">An <see cref="OdbcDataReader"/> object which can be used to fetch data from the result set</param>
        /// <returns>True if successfull, false otherwise</returns>
        public bool SelectRequest(OdbcCommand command, ref OdbcDataReader reader)
        {
            reader = null;
            bool f = false;

            if (IsOpen && null != command)
            {
                try
                {
                    command.Connection = Database;
                    reader             = command.ExecuteReader();
                    f = true;
                }
                catch (Exception ex)
                {
                    CLog.AddException($"{MethodBase.GetCurrentMethod().Module.Name}.{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}", ex);
                }
                finally
                {
                    command.Dispose();
                }
            }
            return(f);
        }
Exemple #19
0
 /// <summary>
 /// Fill a <see cref="DataTable"/> with data contained inside a database table
 /// </summary>
 /// <param name="sql"></param>
 /// <returns></returns>
 public DataTable FillTable(string sql)
 {
     if (IsOpen && !string.IsNullOrEmpty(sql))
     {
         try
         {
             sql = TryToFixTrueFalseTesting(sql);
             OdbcDataAdapter da        = new OdbcDataAdapter(sql, Database);
             DataTable       dataTable = new DataTable();
             da.Fill(dataTable);
             DataAdapter               = da;
             CommandBuilder            = new OdbcCommandBuilder(DataAdapter);
             DataAdapter.InsertCommand = CommandBuilder.GetInsertCommand();
             DataAdapter.UpdateCommand = CommandBuilder.GetUpdateCommand();
             DataAdapter.DeleteCommand = CommandBuilder.GetDeleteCommand();
             return(dataTable);
         }
         catch (Exception ex)
         {
             CLog.AddException($"{MethodBase.GetCurrentMethod().Module.Name}.{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}", ex, $"Connection string: {ConnectionString}");
         }
     }
     return(null);
 }