style and security enhancement from codacy

This commit is contained in:
Federico Barresi
2020-05-07 00:42:10 +02:00
parent a073997211
commit e1486d646e
4 changed files with 650 additions and 566 deletions

View File

@@ -11,8 +11,7 @@ namespace Sharp7
private int _ReadTimeout = 2000; private int _ReadTimeout = 2000;
private int _WriteTimeout = 2000; private int _WriteTimeout = 2000;
private int _ConnectTimeout = 1000; private int _ConnectTimeout = 1000;
public int LastError = 0; private int LastError;
public MsgSocket() public MsgSocket()
{ {
} }
@@ -49,7 +48,7 @@ namespace Sharp7
IAsyncResult result = PingSocket.BeginConnect(Host, Port, null, null); IAsyncResult result = PingSocket.BeginConnect(Host, Port, null, null);
bool success = result.AsyncWaitHandle.WaitOne(_ConnectTimeout, true); bool success = result.AsyncWaitHandle.WaitOne(_ConnectTimeout, true);
if (!success) if (!success)
{ {
LastError = S7Consts.errTCPConnectionFailed; LastError = S7Consts.errTCPConnectionFailed;
} }
@@ -57,7 +56,8 @@ namespace Sharp7
catch catch
{ {
LastError = S7Consts.errTCPConnectionFailed; LastError = S7Consts.errTCPConnectionFailed;
}; }
PingSocket.Close(); PingSocket.Close();
} }
@@ -150,7 +150,7 @@ namespace Sharp7
LastError = 0; LastError = 0;
try try
{ {
int BytesSent = TCPSocket.Send(Buffer, Size, SocketFlags.None); TCPSocket.Send(Buffer, Size, SocketFlags.None);
} }
catch catch
{ {

File diff suppressed because it is too large Load Diff

View File

@@ -9,11 +9,11 @@ namespace Sharp7
#region [MultiRead/Write Helper] #region [MultiRead/Write Helper]
private S7Client FClient; private S7Client FClient;
private GCHandle[] Handles = new GCHandle[S7Client.MaxVars]; private GCHandle[] Handles = new GCHandle[S7Client.MaxVars];
private int Count = 0; private int Count;
private S7Client.S7DataItem[] Items = new S7Client.S7DataItem[S7Client.MaxVars]; private S7Client.S7DataItem[] Items = new S7Client.S7DataItem[S7Client.MaxVars];
public int[] Results = new int[S7Client.MaxVars]; public int[] Results { get; } = new int[S7Client.MaxVars];
private bool AdjustWordLength(int Area, ref int WordLen, ref int Amount, ref int Start) private bool AdjustWordLength(int Area, ref int WordLen, ref int Amount, ref int Start)
{ {
@@ -45,7 +45,7 @@ namespace Sharp7
{ {
FClient = Client; FClient = Client;
for (int c = 0; c < S7Client.MaxVars; c++) for (int c = 0; c < S7Client.MaxVars; c++)
Results[c] = (int)S7Consts.errCliItemNotAvailable; Results[c] = S7Consts.errCliItemNotAvailable;
} }
~S7MultiVar() ~S7MultiVar()
{ {
@@ -80,32 +80,27 @@ namespace Sharp7
Items[Count].Start = Start; Items[Count].Start = Start;
Items[Count].Amount = Amount; Items[Count].Amount = Amount;
GCHandle handle = GCHandle.Alloc(Buffer, GCHandleType.Pinned); GCHandle handle = GCHandle.Alloc(Buffer, GCHandleType.Pinned);
#if WINDOWS_UWP || NETFX_CORE
if (IntPtr.Size == 4)
Items[Count].pData = (IntPtr)(handle.AddrOfPinnedObject().ToInt32() + Offset * Marshal.SizeOf<T>());
else
Items[Count].pData = (IntPtr)(handle.AddrOfPinnedObject().ToInt64() + Offset * Marshal.SizeOf<T>());
#else
if (IntPtr.Size == 4) if (IntPtr.Size == 4)
Items[Count].pData = (IntPtr)(handle.AddrOfPinnedObject().ToInt32() + Offset * Marshal.SizeOf(typeof(T))); Items[Count].pData = (IntPtr)(handle.AddrOfPinnedObject().ToInt32() + Offset * Marshal.SizeOf(typeof(T)));
else else
Items[Count].pData = (IntPtr)(handle.AddrOfPinnedObject().ToInt64() + Offset * Marshal.SizeOf(typeof(T))); Items[Count].pData = (IntPtr)(handle.AddrOfPinnedObject().ToInt64() + Offset * Marshal.SizeOf(typeof(T)));
#endif
Handles[Count] = handle; Handles[Count] = handle;
Count++; Count++;
return true; return true;
} }
else
return false;
}
else
return false; return false;
}
return false;
} }
public int Read() public int Read()
{ {
int FunctionResult; int FunctionResult;
int GlobalResult = (int)S7Consts.errCliFunctionRefused; int GlobalResult;
try try
{ {
if (Count > 0) if (Count > 0)
@@ -117,7 +112,7 @@ namespace Sharp7
GlobalResult = FunctionResult; GlobalResult = FunctionResult;
} }
else else
GlobalResult = (int)S7Consts.errCliFunctionRefused; GlobalResult = S7Consts.errCliFunctionRefused;
} }
finally finally
{ {
@@ -129,7 +124,7 @@ namespace Sharp7
public int Write() public int Write()
{ {
int FunctionResult; int FunctionResult;
int GlobalResult = (int)S7Consts.errCliFunctionRefused; int GlobalResult;
try try
{ {
if (Count > 0) if (Count > 0)
@@ -141,7 +136,7 @@ namespace Sharp7
GlobalResult = FunctionResult; GlobalResult = FunctionResult;
} }
else else
GlobalResult = (int)S7Consts.errCliFunctionRefused; GlobalResult = S7Consts.errCliFunctionRefused;
} }
finally finally
{ {

View File

@@ -9,17 +9,13 @@ namespace Sharp7
#region S7Timer #region S7Timer
TimeSpan pt; TimeSpan pt;
TimeSpan et; TimeSpan et;
bool input = false; bool input ;
bool q = false; bool q;
public S7Timer(byte[] buff, int position) public S7Timer(byte[] buff, int position)
{ {
if (position + 12 < buff.Length) if (position + 12 < buff.Length)
{ {
return; SetTimer(new List<byte>(buff).GetRange(position, 12).ToArray());
}
else
{
SetTimer(new List<byte>(buff).GetRange(position, 16).ToArray());
} }
} }