protected void SendCommand(Socket socket, string command, CommandSentEventHandler callback, bool closeConnection = false)
{
try
{
Wait();
var byteData = DongleEncoding.Default.GetBytes(command);
socket.BeginSend(byteData, 0, byteData.Length, 0, asyncResult =>
{
int bytesSent;
try
{
bytesSent = socket.EndSend(asyncResult);
}
catch (Exception exception)
{
if (OnErrorOcurred != null)
{
OnErrorOcurred(exception);
}
if (OnSocketDisconnected != null)
{
OnSocketDisconnected(socket);
}
return;
}
IsSending = false;
if (callback != null)
{
callback(command, bytesSent);
}
if (closeConnection)
{
try
{
socket.Disconnect(false);
if (OnSocketDisconnected != null)
{
OnSocketDisconnected(socket);
}
}
catch (Exception exception)
{
if (OnErrorOcurred != null)
{
OnErrorOcurred(exception);
}
}
}
}, null);
}
catch (Exception)
{
if (OnSocketDisconnected != null)
{
OnSocketDisconnected(socket);
}
}
}