Compare commits

..

2 Commits

Author SHA1 Message Date
ccaa256960 Merge pull request 'refactor code' (#9) from refactor_code1 into master
Reviewed-on: #9
2024-06-20 16:05:21 +00:00
126aba5ee5 refactor code 2024-06-20 19:03:35 +03:00
2 changed files with 102 additions and 80 deletions

View File

@ -2270,6 +2270,14 @@ namespace AppServer
{ {
SM.Debug("Load " + emailHT.Count + " subscribers emails from DB ..."); SM.Debug("Load " + emailHT.Count + " subscribers emails from DB ...");
} }
if (sslServer == null)
{
SM.Debug("sslServer is null");
return;
}
Int64 latestEmailTime = sslServer.getLastEmailTime(); Int64 latestEmailTime = sslServer.getLastEmailTime();
ArrayList EmailList = sslServer.getEmails(); ArrayList EmailList = sslServer.getEmails();

View File

@ -263,19 +263,24 @@ namespace SipComponent
} }
else else
_localIPaddress = localIPAddress; _localIPaddress = localIPAddress;
_sipDomainPort = sipDomainPort; _sipDomainPort = sipDomainPort;
_sipClient = CreateSipClientClass(sipDomain, _sipDomainPort, _localIPaddress, localSipPort, userName, password, requestTimeout); _sipClient = CreateSipClientClass(sipDomain, _sipDomainPort, _localIPaddress, localSipPort, userName, password, requestTimeout);
_bufferMiliseconds = bufferMiliseconds; _bufferMiliseconds = bufferMiliseconds;
// Registration Timer // Registration Timer
_registrationInterval = registrationInterval; _registrationInterval = registrationInterval;
_registrationData = new RegistrationData(userName, _registrationInterval + 2); _registrationData = new RegistrationData(userName, _registrationInterval + 2);
System.Timers.Timer registrationTimer = new System.Timers.Timer(); System.Timers.Timer registrationTimer = new System.Timers.Timer();
// Set up the registration timer // Set up the registration timer
registrationTimer.Interval = _registrationInterval * 1000; registrationTimer.Interval = _registrationInterval * 1000;
registrationTimer.Elapsed += _registrationTimer_Elapsed; registrationTimer.Elapsed += _registrationTimer_Elapsed;
_sipID_regTimer_regStatus_Dict.Add(userName, _sipID_regTimer_regStatus_Dict.Add(userName,
new Tuple<System.Timers.Timer, RegistrationStatus>(registrationTimer, RegistrationStatus.RegistrationNotStarted new Tuple<System.Timers.Timer, RegistrationStatus>(registrationTimer, RegistrationStatus.RegistrationNotStarted
)); ));
_IDsregisteredList.Add(userName); _IDsregisteredList.Add(userName);
StartRegistrationTimer(); StartRegistrationTimer();
@ -304,6 +309,7 @@ namespace SipComponent
throw new ObjectDisposedException("SipClientClass"); throw new ObjectDisposedException("SipClientClass");
if (idToInvite == null) if (idToInvite == null)
throw new ArgumentNullException("idToInvite"); throw new ArgumentNullException("idToInvite");
lock (_lockerSipDialog) lock (_lockerSipDialog)
{ {
if (!_IDsentInviteDict.ContainsKey(idToInvite) && !_IDreceivedInviteDict.ContainsKey(idToInvite) if (!_IDsentInviteDict.ContainsKey(idToInvite) && !_IDreceivedInviteDict.ContainsKey(idToInvite)
@ -324,6 +330,7 @@ namespace SipComponent
throw new ObjectDisposedException("SipClientClass"); throw new ObjectDisposedException("SipClientClass");
if (groupIDtoInvite == null) if (groupIDtoInvite == null)
throw new ArgumentNullException("groupIDtoInvite"); throw new ArgumentNullException("groupIDtoInvite");
// Check if registered to the group id // Check if registered to the group id
lock (_lockerSipDialog) lock (_lockerSipDialog)
{ {
@ -342,6 +349,7 @@ namespace SipComponent
{ {
if (_sipClassClosed) if (_sipClassClosed)
throw new ObjectDisposedException("SipClientClass"); throw new ObjectDisposedException("SipClientClass");
Task t = null; Task t = null;
CancelInvite_private(idToCancel, out t); CancelInvite_private(idToCancel, out t);
} }
@ -370,6 +378,7 @@ namespace SipComponent
{ {
return dialog.CallID == _IDsentInviteDict[idToCancel].CallID; return dialog.CallID == _IDsentInviteDict[idToCancel].CallID;
}); });
if (d != null) if (d != null)
{ {
//Send Cancel Request //Send Cancel Request
@ -387,6 +396,7 @@ namespace SipComponent
} }
}, d); }, d);
} }
// Remove from dict // Remove from dict
_IDsentInviteDict.Remove(idToCancel); _IDsentInviteDict.Remove(idToCancel);
_IDsCalledByMeList.Remove(idToCancel); _IDsCalledByMeList.Remove(idToCancel);
@ -585,6 +595,7 @@ namespace SipComponent
{ {
if (_sipClassClosed) if (_sipClassClosed)
throw new ObjectDisposedException("SipClientClass"); throw new ObjectDisposedException("SipClientClass");
lock (_lockerSipDialog) lock (_lockerSipDialog)
{ {
if (_IDdialogTuple.ContainsKey(idToSendVoice)) if (_IDdialogTuple.ContainsKey(idToSendVoice))
@ -603,8 +614,11 @@ namespace SipComponent
{ {
if (_sipClassClosed) if (_sipClassClosed)
throw new ObjectDisposedException("SipClientClass"); throw new ObjectDisposedException("SipClientClass");
if (idToRequestGps != null)
{ if (idToRequestGps == null)
throw new ArgumentNullException("idToRequestGps");
// Send Gps request for Linx // Send Gps request for Linx
// [#msgLen]#seqID#154# // [#msgLen]#seqID#154#
string cmdText = string.Format("#{0}#154#", seqID); string cmdText = string.Format("#{0}#154#", seqID);
@ -622,9 +636,6 @@ namespace SipComponent
; // Probably timeout exception, do not do anything ; // Probably timeout exception, do not do anything
} }
}); });
}
else
throw new ArgumentNullException("idToRequestGps");
} }
@ -636,8 +647,10 @@ namespace SipComponent
{ {
if (_sipClassClosed) if (_sipClassClosed)
throw new ObjectDisposedException("SipClientClass"); throw new ObjectDisposedException("SipClientClass");
if (linxID != null)
{ if (linxID == null)
throw new ArgumentNullException("linxID");
// Send [#msgLen]#seqID#238# // Send [#msgLen]#seqID#238#
string textToSend = string.Format("#{0}#238#", _rand.Next().ToString()); string textToSend = string.Format("#{0}#238#", _rand.Next().ToString());
string cmdToSend = AddMsgLenForMBus(textToSend); string cmdToSend = AddMsgLenForMBus(textToSend);
@ -653,9 +666,7 @@ namespace SipComponent
// Do nothing // Do nothing
} }
}); });
}
else
throw new ArgumentNullException("linxID");
} }
/// <summary> /// <summary>
@ -669,10 +680,12 @@ namespace SipComponent
{ {
if (_sipClassClosed) if (_sipClassClosed)
throw new ObjectDisposedException("SipClientClass"); throw new ObjectDisposedException("SipClientClass");
if (idToSendSMS != null && text != null) if (idToSendSMS != null && text != null)
{ {
bool sendSipMessage = true; bool sendSipMessage = true;
string unconfirmedSmsKey = null; string unconfirmedSmsKey = null;
if (_smsConfirmationFromServer) if (_smsConfirmationFromServer)
{ {
unconfirmedSmsKey = idToSendSMS + text.GetHashCode(); unconfirmedSmsKey = idToSendSMS + text.GetHashCode();
@ -691,6 +704,7 @@ namespace SipComponent
} }
} }
} }
if (sendSipMessage) if (sendSipMessage)
{ {
// Create a new thread on which: // Create a new thread on which:
@ -743,15 +757,16 @@ namespace SipComponent
throw new ArgumentNullException("text"); throw new ArgumentNullException("text");
} }
private Message GenerateSipMessage(string destinationID, string senderID, string sipServer, private Message GenerateSipMessage(string destinationID, string senderID, string sipServer, int sipServerPort, string text)
int sipServerPort, string text)
{ {
Message sipMessage = new Message(); Message sipMessage = new Message()
sipMessage.Uri = "sip:" + destinationID + "@" + sipServer + ":" + sipServerPort; {
sipMessage.From = new ContactInfo(string.Format("sip:{0}@{1}", senderID, sipServer)); Uri = $"sip:{destinationID}@{sipServer}:{sipServerPort}",
sipMessage.To = new ContactInfo(string.Format("sip:{0}@{1}", destinationID, sipServer)); From = new ContactInfo($"sip:{senderID}@{sipServer}"),
sipMessage.ContentType = "text/plain;charset=UTF-8"; To = new ContactInfo($"sip:{destinationID}@{sipServer}"),
sipMessage.Body = text; ContentType = "text/plain;charset=UTF-8",
Body = text
};
return sipMessage; return sipMessage;
} }
@ -833,10 +848,12 @@ namespace SipComponent
if (!_sipClassClosed) if (!_sipClassClosed)
{ {
_sipClassClosed = true; _sipClassClosed = true;
// Send ARS OFF and disconnect from socket IO // Send ARS OFF and disconnect from socket IO
if (_sendArsOnOff) if (_sendArsOnOff)
_socketIOClass.SendArs(new ArsInfo(false, "0", UserName)); _socketIOClass.SendArs(new ArsInfo(false, "0", UserName));
_socketIOClass.Disconect(); _socketIOClass.Disconect();
// //
List<string> sipIDs = new List<string>(); List<string> sipIDs = new List<string>();
Task task = null; Task task = null;
@ -892,6 +909,7 @@ namespace SipComponent
// Stop sip class // Stop sip class
_sipClient.Disconnect(); _sipClient.Disconnect();
}); });
if (!async) if (!async)
t.Wait(); t.Wait();
} }
@ -911,6 +929,8 @@ namespace SipComponent
zeroRegistrationData = new RegistrationData(id, 0, true); zeroRegistrationData = new RegistrationData(id, 0, true);
else else
zeroRegistrationData = new RegistrationData(id, 0); zeroRegistrationData = new RegistrationData(id, 0);
_sipID_regTimer_regStatus_Dict[id].Item1.Stop(); _sipID_regTimer_regStatus_Dict[id].Item1.Stop();
SendSipRegister(zeroRegistrationData); SendSipRegister(zeroRegistrationData);
} }
@ -1398,9 +1418,10 @@ namespace SipComponent
string lastWord = source.Substring(source.LastIndexOf(' ') + 1); string lastWord = source.Substring(source.LastIndexOf(' ') + 1);
if (lastWord.Contains("delivered")) if (lastWord.Contains("delivered"))
return true; return true;
else if (lastWord.Contains("failed"))
if (lastWord.Contains("failed"))
return false; return false;
else
throw new ApplicationException("Error on parsing the sms confirmation from sip server"); throw new ApplicationException("Error on parsing the sms confirmation from sip server");
} }
@ -1418,11 +1439,13 @@ namespace SipComponent
// Extract ip and port where to send voice to simoco // Extract ip and port where to send voice to simoco
IPAddress ipToSendAudio = null; IPAddress ipToSendAudio = null;
int portToSendAudio; int portToSendAudio;
if (IPAddress.TryParse(receivedSDP.Connection.Address, out ipToSendAudio)) if (IPAddress.TryParse(receivedSDP.Connection.Address, out ipToSendAudio))
{ {
portToSendAudio = receivedSDP.Media[0].Port; portToSendAudio = receivedSDP.Media[0].Port;
} }
else throw new ApplicationException("Canot determine ip where to send audio"); else
throw new ApplicationException("Canot determine ip where to send audio");
// Creez clientul de UDP conectat la portul pe care voi primi voce // Creez clientul de UDP conectat la portul pe care voi primi voce
UdpClient udpClient = new UdpClient(localRTPport); UdpClient udpClient = new UdpClient(localRTPport);
@ -1563,9 +1586,9 @@ namespace SipComponent
private SipClient CreateSipClientClass(string sipDomain, int sipDomainPort, string localIPaddress, int localSipPort, string userName, string password, int requestTimeout) private SipClient CreateSipClientClass(string sipDomain, int sipDomainPort, string localIPaddress, int localSipPort, string userName, string password, int requestTimeout)
{ {
SipClient sipClient;
// Set up the master SIP class // Set up the master SIP class
sipClient = new SipClient(sipDomain, sipDomainPort, Independentsoft.Sip.ProtocolType.Udp, userName, password); SipClient sipClient = new SipClient(sipDomain, sipDomainPort, Independentsoft.Sip.ProtocolType.Udp, userName, password);
sipClient.LocalIPEndPoint = new IPEndPoint(IPAddress.Parse(localIPaddress), localSipPort); sipClient.LocalIPEndPoint = new IPEndPoint(IPAddress.Parse(localIPaddress), localSipPort);
// Turn on logging // Turn on logging
//sipClient.Logger = new Logger(AppDomain.CurrentDomain.BaseDirectory + "\\sipLog.txt"); //sipClient.Logger = new Logger(AppDomain.CurrentDomain.BaseDirectory + "\\sipLog.txt");
@ -1590,6 +1613,7 @@ namespace SipComponent
int rtpPort = ReturnAvailablePort(); int rtpPort = ReturnAvailablePort();
SessionDescription sdp = CreateSDP(rtpPort); SessionDescription sdp = CreateSDP(rtpPort);
Invite inv = new Invite(); Invite inv = new Invite();
inv.Uri = "sip:" + idToCall + "@" + sipServerIP; inv.Uri = "sip:" + idToCall + "@" + sipServerIP;
inv.From = new ContactInfo("sip:" + sipID.ToString() + "@" + sipServerIP); inv.From = new ContactInfo("sip:" + sipID.ToString() + "@" + sipServerIP);
@ -1650,6 +1674,7 @@ namespace SipComponent
private void SendSipRegister(object registrationDataObj) private void SendSipRegister(object registrationDataObj)
{ {
string sipServerIP = _sipClient.Domain; string sipServerIP = _sipClient.Domain;
RegistrationData regData = (RegistrationData)registrationDataObj; RegistrationData regData = (RegistrationData)registrationDataObj;
string sipIDfrom = _sipClient.Username; string sipIDfrom = _sipClient.Username;
string sipIDto = regData.SipID; string sipIDto = regData.SipID;
@ -1658,12 +1683,8 @@ namespace SipComponent
Register reg = new Register(); Register reg = new Register();
reg.Uri = "sip:" + sipServerIP; reg.Uri = "sip:" + sipServerIP;
reg.From = new ContactInfo( reg.From = new ContactInfo( sipIDfrom.ToString(), $"sip:{sipIDfrom}@{sipServerIP}");
sipIDfrom.ToString(), reg.To = new ContactInfo( sipIDto.ToString(), $"sip:{sipIDto}@{sipServerIP}");
"sip:" + sipIDfrom + "@" + sipServerIP);
reg.To = new ContactInfo(
sipIDto.ToString(),
"sip:" + sipIDto.ToString() + "@" + sipServerIP);
reg.Contact = new Contact("sip:" + sipIDfrom.ToString() + "@" + _sipClient.LocalIPEndPoint.ToString()); reg.Contact = new Contact("sip:" + sipIDfrom.ToString() + "@" + _sipClient.LocalIPEndPoint.ToString());
reg.Expires = expiresValue; reg.Expires = expiresValue;
@ -1763,6 +1784,7 @@ namespace SipComponent
{ {
SendSipRegister(state); SendSipRegister(state);
}, _registrationData); }, _registrationData);
// Start registration timer // Start registration timer
_sipID_regTimer_regStatus_Dict[UserName].Item1.Start(); _sipID_regTimer_regStatus_Dict[UserName].Item1.Start();
} }
@ -1807,9 +1829,11 @@ namespace SipComponent
{ {
rtpPort += 2; rtpPort += 2;
} }
if (rtpPort < MaxRtpPortNumber) if (rtpPort < MaxRtpPortNumber)
return rtpPort; return rtpPort;
else
throw new SipClassException( throw new SipClassException(
string.Format("Nu gasesc port liber in range-ul {0} - {1}", MinRtpPortNumber, MaxRtpPortNumber)); string.Format("Nu gasesc port liber in range-ul {0} - {1}", MinRtpPortNumber, MaxRtpPortNumber));
} }
@ -1821,10 +1845,8 @@ namespace SipComponent
{ {
return invite.SessionDescription.Media[0].Port == portNb; return invite.SessionDescription.Media[0].Port == portNb;
}); });
if (inv != null)
return true; return (inv != null);
else
return false;
} }
private bool IsPortAllreadyInUse(int portNumber) private bool IsPortAllreadyInUse(int portNumber)
@ -1843,14 +1865,11 @@ namespace SipComponent
public static bool ValidRtpPort(int rtpPort) public static bool ValidRtpPort(int rtpPort)
{ {
if (rtpPort < 1024 || rtpPort > 65534) if (rtpPort < 1024 || rtpPort > 65534)
{
return false; return false;
}
else if (rtpPort % 2 == 1) if (rtpPort % 2 == 1)
{
return false; return false;
}
else
return true; return true;
} }
@ -1870,11 +1889,6 @@ namespace SipComponent
private bool IsInSameSubnet(IPAddress address2, IPAddress address, IPAddress subnetMask) private bool IsInSameSubnet(IPAddress address2, IPAddress address, IPAddress subnetMask)
{ {
IPAddress network1 = GetNetworkAddress(address, subnetMask); IPAddress network1 = GetNetworkAddress(address, subnetMask);