Socket connects, crashes and reconnects - c#

so I have my client-server program which was working fine, but now I ran into an issue and I have not a single clue on how to solve it. So my client connects to the server, using async sockets. But when it tries to get the async result, it contains nothing. It disposes the socket and starts all over. This is my code(client):
public class Client
{
/// <summary>
/// Occurs as a result of an unrecoverable issue with the client.
/// </summary>
public event ClientFailEventHandler ClientFail;
/// <summary>
/// Represents a method that will handle failure of the client.
/// </summary>
/// <param name="s">The client that has failed.</param>
/// <param name="ex">The exception containing information about the cause of the client's failure.</param>
public delegate void ClientFailEventHandler(Client s, Exception ex);
/// <summary>
/// Fires an event that informs subscribers that the client has failed.
/// </summary>
/// <param name="ex">The exception containing information about the cause of the client's failure.</param>
private void OnClientFail(Exception ex)
{
var handler = ClientFail;
if (handler != null)
{
handler(this, ex);
}
}
/// <summary>
/// Occurs when the state of the client has changed.
/// </summary>
public event ClientStateEventHandler ClientState;
/// <summary>
/// Represents the method that will handle a change in the client's state
/// </summary>
/// <param name="s">The client which changed its state.</param>
/// <param name="connected">The new connection state of the client.</param>
public delegate void ClientStateEventHandler(Client s, bool connected);
/// <summary>
/// Fires an event that informs subscribers that the state of the client has changed.
/// </summary>
/// <param name="connected">The new connection state of the client.</param>
private void OnClientState(bool connected)
{
if (Connected == connected) return;
Connected = connected;
var handler = ClientState;
if (handler != null)
{
handler(this, connected);
}
}
/// <summary>
/// Occurs when a packet is received from the server.
/// </summary>
public event ClientReadEventHandler ClientRead;
/// <summary>
/// Represents a method that will handle a packet from the server.
/// </summary>
/// <param name="s">The client that has received the packet.</param>
/// <param name="packet">The packet that has been received by the server.</param>
public delegate void ClientReadEventHandler(Client s, IPacket packet);
/// <summary>
/// Fires an event that informs subscribers that a packet has been received by the server.
/// </summary>
/// <param name="packet">The packet that has been received by the server.</param>
private void OnClientRead(IPacket packet)
{
var handler = ClientRead;
if (handler != null)
{
handler(this, packet);
}
}
/// <summary>
/// Occurs when a packet is sent by the client.
/// </summary>
public event ClientWriteEventHandler ClientWrite;
/// <summary>
/// Represents the method that will handle the sent packet.
/// </summary>
/// <param name="s">The client that has sent the packet.</param>
/// <param name="packet">The packet that has been sent by the client.</param>
/// <param name="length">The length of the packet.</param>
/// <param name="rawData">The packet in raw bytes.</param>
public delegate void ClientWriteEventHandler(Client s, IPacket packet, long length, byte[] rawData);
/// <summary>
/// Fires an event that informs subscribers that the client has sent a packet.
/// </summary>
/// <param name="packet">The packet that has been sent by the client.</param>
/// <param name="length">The length of the packet.</param>
/// <param name="rawData">The packet in raw bytes.</param>
private void OnClientWrite(IPacket packet, long length, byte[] rawData)
{
var handler = ClientWrite;
if (handler != null)
{
handler(this, packet, length, rawData);
}
}
/// <summary>
/// The type of the packet received.
/// </summary>
public enum ReceiveType
{
Header,
Payload
}
/// <summary>
/// The buffer size for receiving data in bytes.
/// </summary>
public int BUFFER_SIZE { get { return 1024 * 16; } } // 16KB
/// <summary>
/// The keep-alive time in ms.
/// </summary>
public uint KEEP_ALIVE_TIME { get { return 25000; } } // 25s
/// <summary>
/// The keep-alive interval in ms.
/// </summary>
public uint KEEP_ALIVE_INTERVAL { get { return 25000; } } // 25s
/// <summary>
/// The header size in bytes.
/// </summary>
public int HEADER_SIZE { get { return 4; } } // 4B
/// <summary>
/// The maximum size of a packet in bytes.
/// </summary>
public int MAX_PACKET_SIZE { get { return (1024 * 1024) * 5; } } // 5MB
/// <summary>
/// Returns an array containing all of the proxy clients of this client.
/// </summary>
public ReverseProxyClient[] ProxyClients
{
get
{
lock (_proxyClientsLock)
{
return _proxyClients.ToArray();
}
}
}
/// <summary>
/// Handle of the Client Socket.
/// </summary>
private Socket _handle;
/// <summary>
/// A list of all the connected proxy clients that this client holds.
/// </summary>
private List<ReverseProxyClient> _proxyClients;
/// <summary>
/// Lock object for the list of proxy clients.
/// </summary>
private readonly object _proxyClientsLock = new object();
/// <summary>
/// The buffer for incoming packets.
/// </summary>
private byte[] _readBuffer;
/// <summary>
/// The buffer for the client's incoming payload.
/// </summary>
private byte[] _payloadBuffer;
/// <summary>
/// The Queue which holds buffers to send.
/// </summary>
private readonly Queue<byte[]> _sendBuffers = new Queue<byte[]>();
/// <summary>
/// Determines if the client is currently sending packets.
/// </summary>
private bool _sendingPackets;
/// <summary>
/// Lock object for the sending packets boolean.
/// </summary>
private readonly object _sendingPacketsLock = new object();
/// <summary>
/// The Queue which holds buffers to read.
/// </summary>
private readonly Queue<byte[]> _readBuffers = new Queue<byte[]>();
/// <summary>
/// Determines if the client is currently reading packets.
/// </summary>
private bool _readingPackets;
/// <summary>
/// Lock object for the reading packets boolean.
/// </summary>
private readonly object _readingPacketsLock = new object();
/// <summary>
/// The temporary header to store parts of the header.
/// </summary>
/// <remarks>
/// This temporary header is used when we have i.e.
/// only 2 bytes left to read from the buffer but need more
/// which can only be read in the next Receive callback
/// </remarks>
private byte[] _tempHeader;
/// <summary>
/// Decides if we need to append bytes to the header.
/// </summary>
private bool _appendHeader;
// Receive info
private int _readOffset;
private int _writeOffset;
private int _tempHeaderOffset;
private int _readableDataLen;
private int _payloadLen;
private ReceiveType _receiveState = ReceiveType.Header;
/// <summary>
/// Gets if the client is currently connected to a server.
/// </summary>
public bool Connected { get; private set; }
/// <summary>
/// The packet serializer.
/// </summary>
protected Serializer Serializer { get; set; }
private const bool encryptionEnabled = true;
private const bool compressionEnabled = true;
protected Client()
{
_proxyClients = new List<ReverseProxyClient>();
_readBuffer = new byte[BUFFER_SIZE];
_tempHeader = new byte[HEADER_SIZE];
}
/// <summary>
/// Attempts to connect to the specified ip address on the specified port.
/// </summary>
/// <param name="ip">The ip address to connect to.</param>
/// <param name="port">The port of the host.</param>
protected void Connect(IPAddress ip, ushort port)
{
try
{
Disconnect();
_handle = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_handle.SetKeepAliveEx(KEEP_ALIVE_INTERVAL, KEEP_ALIVE_TIME);
_handle.Connect(ip, port);
if (_handle.Connected)
{
_handle.BeginReceive(_readBuffer, 0, _readBuffer.Length, SocketFlags.None, AsyncReceive, null);
OnClientState(true);
}
}
catch (Exception ex)
{
OnClientFail(ex);
}
}
private void AsyncReceive(IAsyncResult result)
{
int bytesTransferred; // error occurs here
try
{
bytesTransferred = _handle.EndReceive(result);
if (bytesTransferred <= 0)
throw new Exception("no bytes transferred");
}
catch (NullReferenceException)
{
return;
}
catch (ObjectDisposedException)
{
return;
}
catch (Exception)
{
Disconnect();
return;
}
byte[] received = new byte[bytesTransferred];
try
{
Array.Copy(_readBuffer, received, received.Length);
}
catch (Exception ex)
{
OnClientFail(ex);
return;
}
lock (_readBuffers)
{
_readBuffers.Enqueue(received);
}
lock (_readingPacketsLock)
{
if (!_readingPackets)
{
_readingPackets = true;
ThreadPool.QueueUserWorkItem(AsyncReceive);
}
}
try
{
_handle.BeginReceive(_readBuffer, 0, _readBuffer.Length, SocketFlags.None, AsyncReceive, null);
}
catch (ObjectDisposedException)
{
}
catch (Exception ex)
{
OnClientFail(ex);
}
}
private void AsyncReceive(object state)
{
while (true)
{
byte[] readBuffer;
lock (_readBuffers)
{
if (_readBuffers.Count == 0)
{
lock (_readingPacketsLock)
{
_readingPackets = false;
}
return;
}
readBuffer = _readBuffers.Dequeue();
}
_readableDataLen += readBuffer.Length;
bool process = true;
while (process)
{
switch (_receiveState)
{
case ReceiveType.Header:
{
if (_readableDataLen + _tempHeaderOffset >= HEADER_SIZE)
{ // we can read the header
int headerLength = (_appendHeader)
? HEADER_SIZE - _tempHeaderOffset
: HEADER_SIZE;
try
{
if (_appendHeader)
{
try
{
Array.Copy(readBuffer, _readOffset, _tempHeader, _tempHeaderOffset,
headerLength);
}
catch (Exception ex)
{
process = false;
OnClientFail(ex);
break;
}
_payloadLen = BitConverter.ToInt32(_tempHeader, 0);
_tempHeaderOffset = 0;
_appendHeader = false;
}
else
{
_payloadLen = BitConverter.ToInt32(readBuffer, _readOffset);
}
if (_payloadLen <= 0 || _payloadLen > MAX_PACKET_SIZE)
throw new Exception("invalid header");
}
catch (Exception)
{
process = false;
Disconnect();
break;
}
_readableDataLen -= headerLength;
_readOffset += headerLength;
_receiveState = ReceiveType.Payload;
}
else // _readableDataLen < HEADER_SIZE
{
try
{
Array.Copy(readBuffer, _readOffset, _tempHeader, _tempHeaderOffset, _readableDataLen);
}
catch (Exception ex)
{
process = false;
OnClientFail(ex);
break;
}
_tempHeaderOffset += _readableDataLen;
_appendHeader = true;
process = false;
}
break;
}
case ReceiveType.Payload:
{
if (_payloadBuffer == null || _payloadBuffer.Length != _payloadLen)
_payloadBuffer = new byte[_payloadLen];
int length = (_writeOffset + _readableDataLen >= _payloadLen)
? _payloadLen - _writeOffset
: _readableDataLen;
try
{
Array.Copy(readBuffer, _readOffset, _payloadBuffer, _writeOffset, length);
}
catch (Exception ex)
{
process = false;
OnClientFail(ex);
break;
}
_writeOffset += length;
_readOffset += length;
_readableDataLen -= length;
if (_writeOffset == _payloadLen)
{
bool isError = _payloadBuffer.Length == 0;
if (!isError)
{
if (encryptionEnabled)
_payloadBuffer = AES.Decrypt(_payloadBuffer);
isError = _payloadBuffer.Length == 0; // check if payload decryption failed
}
if (!isError)
{
if (compressionEnabled)
{
try
{
_payloadBuffer = SafeQuickLZ.Decompress(_payloadBuffer);
}
catch (Exception)
{
process = false;
Disconnect();
break;
}
}
isError = _payloadBuffer.Length == 0; // check if payload decompression failed
}
if (isError)
{
process = false;
Disconnect();
break;
}
using (MemoryStream deserialized = new MemoryStream(_payloadBuffer))
{
try
{
IPacket packet = (IPacket)Serializer.Deserialize(deserialized);
OnClientRead(packet);
}
catch (Exception ex)
{
process = false;
OnClientFail(ex);
break;
}
}
_receiveState = ReceiveType.Header;
_payloadBuffer = null;
_payloadLen = 0;
_writeOffset = 0;
}
if (_readableDataLen == 0)
process = false;
break;
}
}
}
if (_receiveState == ReceiveType.Header)
{
_writeOffset = 0; // prepare for next packet
}
_readOffset = 0;
_readableDataLen = 0;
}
}
/// <summary>
/// Sends a packet to the connected server.
/// </summary>
/// <typeparam name="T">The type of the packet.</typeparam>
/// <param name="packet">The packet to be send.</param>
public void Send<T>(T packet) where T : IPacket
{
if (!Connected || packet == null) return;
lock (_sendBuffers)
{
using (MemoryStream ms = new MemoryStream())
{
try
{
Serializer.Serialize(ms, packet);
}
catch (Exception ex)
{
OnClientFail(ex);
return;
}
byte[] payload = ms.ToArray();
_sendBuffers.Enqueue(payload);
OnClientWrite(packet, payload.LongLength, payload);
lock (_sendingPacketsLock)
{
if (_sendingPackets) return;
_sendingPackets = true;
}
ThreadPool.QueueUserWorkItem(Send);
}
}
}
/// <summary>
/// Sends a packet to the connected server.
/// Blocks the thread until all packets have been sent.
/// </summary>
/// <typeparam name="T">The type of the packet.</typeparam>
/// <param name="packet">The packet to be send.</param>
public void SendBlocking<T>(T packet) where T : IPacket
{
Send(packet);
while (_sendingPackets)
{
Thread.Sleep(10);
}
}
private void Send(object state)
{
while (true)
{
if (!Connected)
{
SendCleanup(true);
return;
}
byte[] payload;
lock (_sendBuffers)
{
if (_sendBuffers.Count == 0)
{
SendCleanup();
return;
}
payload = _sendBuffers.Dequeue();
}
try
{
_handle.Send(BuildPacket(payload));
}
catch (Exception ex)
{
OnClientFail(ex);
SendCleanup(true);
return;
}
}
}
private byte[] BuildPacket(byte[] payload)
{
if (compressionEnabled)
payload = SafeQuickLZ.Compress(payload);
if (encryptionEnabled)
payload = AES.Encrypt(payload);
byte[] packet = new byte[payload.Length + HEADER_SIZE];
Array.Copy(BitConverter.GetBytes(payload.Length), packet, HEADER_SIZE);
Array.Copy(payload, 0, packet, HEADER_SIZE, payload.Length);
return packet;
}
private void SendCleanup(bool clear = false)
{
lock (_sendingPacketsLock)
{
_sendingPackets = false;
}
if (!clear) return;
lock (_sendBuffers)
{
_sendBuffers.Clear();
}
}
/// <summary>
/// Disconnect the client from the server, disconnect all proxies that
/// are held by this client, and dispose of other resources associated
/// with this client.
/// </summary>
public void Disconnect()
{
if (_handle != null)
{
_handle.Close();
_handle = null;
_readOffset = 0;
_writeOffset = 0;
_tempHeaderOffset = 0;
_readableDataLen = 0;
_payloadLen = 0;
_payloadBuffer = null;
_receiveState = ReceiveType.Header;
if (_proxyClients != null)
{
lock (_proxyClientsLock)
{
try
{
foreach (ReverseProxyClient proxy in _proxyClients)
proxy.Disconnect();
}
catch (Exception)
{
}
}
}
if (Commands.CommandHandler.StreamCodec != null)
{
Commands.CommandHandler.StreamCodec.Dispose();
Commands.CommandHandler.StreamCodec = null;
}
}
OnClientState(false);
}
public void ConnectReverseProxy(ReverseProxyConnect command)
{
lock (_proxyClientsLock)
{
_proxyClients.Add(new ReverseProxyClient(command, this));
}
}
public ReverseProxyClient GetReverseProxyByConnectionId(int connectionId)
{
lock (_proxyClientsLock)
{
return _proxyClients.FirstOrDefault(t => t.ConnectionId == connectionId);
}
}
public void RemoveProxyClient(int connectionId)
{
try
{
lock (_proxyClientsLock)
{
for (int i = 0; i < _proxyClients.Count; i++)
{
if (_proxyClients[i].ConnectionId == connectionId)
{
_proxyClients.RemoveAt(i);
break;
}
}
}
}
catch { }
}
}
The output from visual studio:
Exception thrown: 'System.Net.Sockets.SocketException' in System.dll
Client Fail - Exception Message: No connection could be made because the target machine actively refused it 127.0.0.1:4782
Exception thrown: 'System.NullReferenceException' in Client.exe
Exception thrown: 'System.ObjectDisposedException' in System.dll
Exception thrown: 'System.ObjectDisposedException' in System.dll
Exception thrown: 'System.NullReferenceException' in Client.exe
Exception thrown: 'System.ObjectDisposedException' in System.dll
Exception thrown: 'System.NullReferenceException' in Client.exe
Client Fail - Exception Message: Object reference not set to an instance of an object.
Exception thrown: 'System.ObjectDisposedException' in System.dll
Exception thrown: 'System.ObjectDisposedException' in System.dll
Exception thrown: 'System.ObjectDisposedException' in System.dll
Exception thrown: 'System.NullReferenceException' in Client.exe
Exception thrown: 'System.NullReferenceException' in Client.exe
The refuse was because the server wasnt running, when I start the server and it connects the crsahing loop begins
Thanks in advance

Found the error, the connection thread on the client had an empty ipadress string in the code and had the hostname it used didnt exist, I should've looked better at all code. Thanks

Related

How to play multiple sounds at once in C#

I want to be able to play multiple sounds at once. I tried this using multi threading but found they would still play one after the other. Is there a way to get them to play at the same time?
static void Main(string[] args)
{
Console.WriteLine("Hello World");
Thread th = new Thread(playSound);
Thread th1 = new Thread(playSound1);
th.Start();
th1.Start();
}
public static void playSound()
{
System.Media.SoundPlayer s1 = new System.Media.SoundPlayer(#"c:\Users\Ben\Documents\c#\note_c.wav");
s1.Load();
s1.PlaySync();
}
public static void playSound1()
{
System.Media.SoundPlayer s1 = new System.Media.SoundPlayer(#"c:\Users\Ben\Documents\c#\note_e.wav");
s1.Load();
s1.PlaySync();
}
}
How about if we schedule a parallel execution.
Like that:
var files = new List<string>() {"note_1.wav", "note_2.wav"};
Parallel.ForEach(files, (currentFile) =>
{
System.Media.SoundPlayer s1 = new System.Media.SoundPlayer(currentFile);
s1.Load();
s1.PlaySync();
});
Try using
[DllImport("winmm.dll")]
static extern Int32 mciSendString(string command, StringBuilder buffer, int bufferSize, IntPtr hwndCallback);
Usage:
mciSendString(#"open path\to\your\file.wav type waveaudio alias anAliasForYourSound", null, 0, IntPtr.Zero);
mciSendString(#"play anAliasForYourSound", null, 0, IntPtr.Zero);
mciSendString(#"open path\to\your\anotherFile.wav type waveaudio alias anAliasForYourAnotherSound", null, 0, IntPtr.Zero);
mciSendString(#"play anAliasForYourAnotherSound", null, 0, IntPtr.Zero);
You can't start threads at exactly same time but in this case you can improve your code by loading the sounds before starting the threads:
static void Main(string[] args)
{
System.Media.SoundPlayer s1 = new System.Media.SoundPlayer(
#"c:\Users\Ben\Documents\c#\note_c.wav");
s1.Load();
System.Media.SoundPlayer s2 = new System.Media.SoundPlayer(
#"c:\Users\Ben\Documents\c#\note_e.wav");
s2.Load();
Console.WriteLine("Hello World");
Thread th = new Thread(() => playSound(s1));
Thread th1 = new Thread(() => playSound(s2));
th.Start();
th1.Start();
}
public static void playSound(System.Media.SoundPlayer s)
{
s.PlaySync();
}
Otherwise you'll need to implement a synchronization system.
The best way is to use DirectX xAudio2 via SharpDX , you can find it here:
https://github.com/sharpdx/SharpDX-Samples/tree/master/Desktop/XAudio2/PlaySound
It works perfectly , but it would be a bit tricky to make the code better, by utilizing Events & tasks instead of the while loop and sync execution, and to be able to load the xAudio2 graph dynamically to change the sound without reconstructing the whole graph.
Add these Nuget packages SharpDx
SharpDX.MediaFoundation
SharpDx.Xaudio2
and use code below, it uses DirectX xAudio2 and constructs a graph with a mixer, it can play WAV 8 and 16 as well
public class SoundServices : IDisposable
{
/// <summary>
/// Gets current sound file
/// </summary>
string SoundFile { get; }
/// <summary>
/// Gets current sound stream
/// </summary>
public Stream SoundStream { get; private set; }
/// <summary>
/// Gets/Sets looping option, sound will loop if set to true.
/// </summary>
public bool IsLooping { get; private set; }
/// <summary>
/// Holds the message of last error if any, check it if some feature fails
/// </summary>
public string LastErrorMsg { get; private set; }
//Play control flags
bool IsPlaying = false;
bool IsUserStop = false;
AutoResetEvent Playing;
float CurrentVolume = 1.0f;
bool IsInitialized = false;
SoundStream stream;
WaveFormat waveFormat;
AudioBuffer buffer;
SourceVoice sourceVoice;
XAudio2 xaudio2;
MasteringVoice masteringVoice;
/// <summary>
/// Initializes an instance by creating xAudio2 graph and preparing audio Buffers
/// </summary>
/// <param name="soundStream">WAV format sound stream</param>
/// <param name="loop">Bool indicating to loop sound playing or not</param>
public SoundServices(Stream soundStream, bool loop)
{
try
{
if (soundStream == null)
{
throw new ArgumentNullException("soundStream", "Null is not allowed, please specify a valid stream");
}
SoundStream = soundStream;
SoundFile = null;
IsLooping = loop;
//Playing = new ManualResetEvent(false);
Playing = new AutoResetEvent(false);
BuildxAudio2Graph();
}
catch (Exception e)
{
throw e;
}
}
#region Sound Play API
/// <summary>
/// Plays the sound stream loaded during initialization
/// </summary>
/// <returns>Task of sound playing</returns>
public Task PlaySound()
{
return Task.Factory.StartNew(() =>
{
PlayRepeatAsync();
});
}
/// <summary>
/// Task for starting play of sound buffers using xAudio2 graph
/// </summary>
void PlayRepeatAsync()
{
try
{
IsPlaying = true;
if (buffer == null)
{
//stream = new SoundStream(SoundStream);
//waveFormat = stream.Format;
buffer = new AudioBuffer
{
Stream = stream.ToDataStream(),
AudioBytes = (int)stream.Length,
Flags = SharpDX.XAudio2.BufferFlags.EndOfStream
};
if (IsLooping)
buffer.LoopCount = AudioBuffer.LoopInfinite;
sourceVoice.SubmitSourceBuffer(buffer, stream.DecodedPacketsInfo);
//Close the stream as it is now loaded in buffer already
//stream.Close();
}
sourceVoice.Start();
Playing.WaitOne();
sourceVoice.Stop();
IsPlaying = false;
sourceVoice.FlushSourceBuffers();
//xAudio2 graph creation step (5) send the AudioBuffer to sourceVoice
sourceVoice.SubmitSourceBuffer(buffer, stream.DecodedPacketsInfo);
}
catch (Exception e)
{
LastErrorMsg = "PlayRepeatAsync(): "+ e.Message;
IsPlaying = false;
}
}
/// <summary>
/// Initializes xAudio2 graph to be used for sound playing
/// </summary>
void BuildxAudio2Graph()
{
try
{
stream = new SoundStream(SoundStream);
waveFormat = stream.Format;
//xAudio2 graph creation step (1) Create XAudio2 device
xaudio2 = new XAudio2();
//xAudio2 graph creation step (2) Create MasteringVoice and connect it to device
//*Note: You must use aMasteringVoice to connect to xAudioDevice
masteringVoice = new MasteringVoice(xaudio2);
//SetVolume(CurrentVolume);
//xAudio2 graph creation step (3) Prepare sourceVoice
sourceVoice = new SourceVoice(xaudio2, waveFormat, true);
// Adds a callback check buffer end and Looping option
sourceVoice.BufferEnd += SourceVoice_BufferEnd;
//xAudio2 graph creation step (5) send the AudioBuffer to sourceVoice
IsInitialized = true;
}
catch (Exception e)
{
LastErrorMsg = "BuildxAudio2Graph(): " + e.Message;
IsInitialized = false;
}
}
/// <summary>
/// Recreates source buffer allowing sound change or loop change
/// </summary>
void RecreateBuffer()
{
try
{
SoundStream.Seek(0, SeekOrigin.Begin);
stream = new SoundStream(SoundStream);
waveFormat = stream.Format;
//if (buffer == null)
{
//stream = new SoundStream(SoundStream);
//waveFormat = stream.Format;
buffer = new AudioBuffer
{
Stream = stream.ToDataStream(),
AudioBytes = (int)stream.Length,
Flags = SharpDX.XAudio2.BufferFlags.EndOfStream
};
if (IsLooping)
buffer.LoopCount = AudioBuffer.LoopInfinite;
sourceVoice.FlushSourceBuffers();
sourceVoice.SubmitSourceBuffer(buffer, stream.DecodedPacketsInfo);
//Close the stream as it is now loaded in buffer already
//stream.Close();
}
}
catch (Exception e)
{
LastErrorMsg = "RecreateBuffer(): " + e.Message;
}
}
/// <summary>
/// Changes current audio to a new one
/// </summary>
/// <param name="soundStream">a stream from wav file</param>
public void ChangeSoundTo(Stream soundStream, bool Loop)
{
try
{
IsLooping = Loop;
SoundStream = soundStream;
stream = new SoundStream(SoundStream);
waveFormat = stream.Format;
sourceVoice = new SourceVoice(xaudio2, waveFormat, true);
sourceVoice.BufferEnd += SourceVoice_BufferEnd;
RecreateBuffer();
}
catch (Exception e)
{
IsInitialized = false;
LastErrorMsg = "ChangeSoundTo(): " + e.Message;
}
}
/// <summary>
/// Set loop ot no loop
/// </summary>
/// <param name="loop">True = Loop forever, false = play till end</param>
public void SetLooping(bool loop)
{
if (IsPlaying)
Stop();
IsLooping = loop;
RecreateBuffer();
}
#endregion
/// <summary>
/// Immediately Stops currently playing sound
/// </summary>
public void Stop()
{
try
{
if (IsPlaying)
{
IsUserStop = true;
Playing.Set();
}
}
catch (Exception e)
{
LastErrorMsg = "Stop(): " + e.Message;
}
}
/// <summary>
/// Gets Current Volume
/// </summary>
/// <returns>Current volume</returns>
public float GetVolume()
{
float current = 0.0f;
try
{
if (sourceVoice == null || sourceVoice.IsDisposed) return CurrentVolume;
sourceVoice.GetVolume(out current);
}
catch (Exception e)
{
LastErrorMsg = "GetVolume(): " + e.Message;
}
return current;
}
/// <summary>
/// Sets the current volume
/// </summary>
/// <param name="newVolume">returns back the current setting for confirmation</param>
/// <returns>The current set volume</returns>
public float SetVolume(float newVolume)
{
try
{
if (newVolume > 1 || newVolume < 0) return GetVolume();
if (sourceVoice == null || sourceVoice.IsDisposed)
{
CurrentVolume = newVolume;
return newVolume;
}
sourceVoice.SetVolume(newVolume, 0);
return GetVolume();
}
catch (Exception e)
{
LastErrorMsg = "SetVolume(): " + e.Message;
return 0.0f;
}
}
/// <summary>
/// End of buffer event handler
/// </summary>
/// <param name="obj"></param>
private void SourceVoice_BufferEnd(IntPtr obj)
{
//Debug.WriteLine($"buffer end reached with looping {IsLooping}");
if (!IsLooping)
{
if (IsPlaying && !IsUserStop)
Playing.Set();
else if(IsUserStop)
{
IsUserStop = false;
}
}
}
public void Dispose()
{
if (sourceVoice != null && !sourceVoice.IsDisposed)
{
sourceVoice.DestroyVoice();
sourceVoice.Dispose();
}
if (buffer != null && buffer.Stream != null)
buffer.Stream.Dispose();
if (masteringVoice != null && !masteringVoice.IsDisposed)
masteringVoice.Dispose();
if (xaudio2 != null && !xaudio2.IsDisposed)
xaudio2.Dispose();
}
~SoundServices()
{
if (sourceVoice != null && !sourceVoice.IsDisposed)
{
sourceVoice.DestroyVoice();
sourceVoice.Dispose();
}
if (buffer != null && buffer.Stream != null)
buffer.Stream.Dispose();
if (masteringVoice != null && !masteringVoice.IsDisposed)
masteringVoice.Dispose();
if (xaudio2 != null && !xaudio2.IsDisposed)
xaudio2.Dispose();
}
}
And you can use multiple SoundServices instances as you need:
var sp = new SoundServices(new MemoryStream(File.ReadAllBytes(WavFileName)), true);
if (sp != null)
sp.PlaySound();

Simple TCP Chat Client Code Review and Questions

I am relatively new to programming and I took it upon myself to make a simple chat application; think AIM from back in the day. This is more of a request for a code review than a list of questions. For the most part, the application works with a few minor errors which I will highlight below. Please see the code from the project: https://github.com/N8STROMO/Basic-Chat-Tcp as well as the code listed below. Questions are embedded along the way. Thanks for any help you may be able to provide!
Here is the Server Code: Server.cs
namespace Server
{
public class Server
{
// This is the port that the server will be listening on
const int PORT = 500;
// This is where the usernames and their connections will be held
readonly Dictionary<string, ServerUserConnection> userToConnections = new Dictionary<string, ServerUserConnection>();
readonly TcpListener listener;
public static void Main()
{
// Constructor for the ChatServer
Server server = new Server();
while(true)
{
Thread.Sleep(1); // Temp to keep alive
}
}
/// <summary>
/// Listens for data from clients
/// </summary>
public Server()
{
listener = TcpListener.Create(PORT);
listener.Start();
WaitForConnections();
}
/// <summary>
/// Begins an asynchronous operation to accept an incoming connection attempt
/// </summary>
private void WaitForConnections()
{
listener.BeginAcceptTcpClient(OnConnect, null);
}
/// <summary>
/// This method is executed asynchronously
/// Connects the client to the server
/// Broadcasts the user to client to be displayed on the chatform
/// Then waits for another connection to be established
/// </summary>
/// <param name="ar"></param>
void OnConnect(IAsyncResult ar)
{
//Asynchronously accepts an incoming connection attempt and creates a new TcpClient to handle remote host communication.
TcpClient client = listener.EndAcceptTcpClient(ar);
Console.WriteLine("Connected");
ReceiveUser(client);
BroadcastUserList();
WaitForConnections();
}
/// <summary>
/// Connects a user to the server and adds them to the dictionary userToConnections
/// </summary>
/// <param name="client"></param>
public void ReceiveUser(TcpClient client)
{
ServerUserConnection connection = new ServerUserConnection(this, client); // Constructor
userToConnections.Add(connection.userName, connection);
}
/// <summary>
/// For each user that is connected append the userList to include that user
/// TODO Do not need to keep a running list of users; send the user over then throw it away
/// </summary>
void BroadcastUserList()
{
string userList = "";
foreach(var connection in userToConnections)
{
userList += $"{connection.Value.userName},";
}
SendMsgToAll(MessageType.UserList, null, userList);
}
/// <summary>
/// Pushes out messages to the connected clients
/// </summary>
/// <param name="type"></param>
/// <param name="user"></param>
/// <param name="message"></param>
public void SendMsgToAll(MessageType type, ServerUserConnection user, string message)
{
Console.WriteLine($"{user?.userName}: {message}");
foreach(var connection in userToConnections)
{
Console.WriteLine($"Sending to {connection.Value.userName}");
Utils.SendInformation(type, connection.Value.stream, message);
}
}
}
}
Here is the ServerUserConnection.cs: I am having trouble understanding how the inheritance here works and what the base keyword does.
namespace Server
{
public class ServerUserConnection : UserConnection
{
readonly Server server;
/// <summary>
/// Facilitates connection
/// </summary>
/// <param name="server"></param>
/// <param name="client"></param>
public ServerUserConnection(Server server, TcpClient client) : base(client, GetUsername(client)) // Inherits from UserConnection()
{
this.server = server;
}
private static string GetUsername(TcpClient client)
{
NetworkStream stream = client.GetStream();
if(stream.CanRead)
{
// Receives infromation from the stream, determines MessageType, and returns username
string userName = Utils.ReceiveInformation(stream, client, out MessageType type);
Console.WriteLine(userName);
return userName;
}
return null;
}
/// <summary>
///
/// </summary>
/// <param name="type"></param>
/// <param name="message"></param>
protected override void OnRead(MessageType type, string message)
{
if(type != MessageType.ChatMessage)
{
return;
}
server.SendMsgToAll(MessageType.ChatMessage, this, $"{userName} {message}");
}
}
}
Here is the Client Code: Client.cs
namespace Client
{
public class ChatClient
{
private const int PORT = 500;
TcpClient client = new TcpClient();
public ChatForm chatForm;
public ClientUserConnection userConnection;
string data;
[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var client = new ChatClient();
var form = new ConnectForm(client);
Application.Run(form);
}
/// <summary>
/// This is called when the ConnectForm btnSubmit is pressed.
/// </summary>
/// <param name="ipAddress"></param>
/// <param name="userName"></param>
public void ConnectToServer(string ipAddress, string userName)
{
client.Connect(ipAddress, PORT);
SendUserName(userName);
}
/// <summary>
/// Sends user to the server
/// </summary>
public void SendUserName(string user)
{
userConnection = new ClientUserConnection(client, this, user);
Utils.SendInformation(MessageType.Connect, client.GetStream(), user);
}
/// <summary>
/// Sends a message to the server
/// </summary>
/// <param name="msg"></param>
public void SendMessage(string msg)
{
Utils.SendInformation(MessageType.ChatMessage, userConnection.stream, msg);
}
}
}
Here is the ClientUserConnection.cs: Is there a way to refactor or optimize this so that instead of keeping a running list of messages just send the message over and throw it away? Again, the same question about inheritance.
public class ClientUserConnection : UserConnection
{
public readonly Client.ChatClient chatClient;
public string message = "";
public string userListText;
/// <summary>
///
/// </summary>
/// <param name="client"></param>
/// <param name="chatClient"></param>
/// <param name="userName"></param>
public ClientUserConnection(TcpClient client, Client.ChatClient chatClient, string userName) : base(client, userName) // Inherits from UserConnection()
{
this.chatClient = chatClient;
}
/// <summary>
/// When the data is reads determine what kind of message it is
/// Parse/split out the message and user; display only relevant data
/// TODO Do not need to keep a running list of messages; send the message over then throw it away
/// </summary>
/// <param name="type"></param>
/// <param name="message"></param>
protected override void OnRead(MessageType type, string message)
{
if(type == MessageType.ChatMessage)
{
int iSpace = message.IndexOf(" ");
if(iSpace < 0)
{
// if error
return;
}
string from = message.Substring(0, iSpace);
string chatMessage = message.Substring(iSpace + 1, message.Length - iSpace - 1);
this.message += $"[{from}]: {chatMessage}{Environment.NewLine}";
}
else if(type == MessageType.UserList)
{
string[] userList = message.Split(',');
string userListText = "";
for(int i = 0; i < userList.Length; i++)
{
userListText += $"{userList[i]}{Environment.NewLine}";
}
this.userListText = userListText;
}
}
}
There are also three files within a class library that are used within this project:
This is the MessageType.cs:
public enum MessageType
{
Connect, UserList, ChatMessage
}
This is the UserConnection.cs: I still need to deal with disconnecting users. If one of the chat forms is closed, it crashes the application.
public abstract class UserConnection
{
public readonly TcpClient client;
public readonly NetworkStream stream;
public readonly string userName;
byte[] data;
/// <summary>
///
/// </summary>
/// <param name="client"></param>
/// <param name="userName"></param>
public UserConnection(TcpClient client, string userName)
{
this.client = client;
this.userName = userName;
stream = client.GetStream();
data = new byte[client.ReceiveBufferSize];
WaitForData();
}
/// <summary>
///
/// </summary>
private void WaitForData()
{
Console.WriteLine("Wait");
stream.BeginRead(data, 0, data.Length, OnReadData, null);
}
/// <summary>
/// SocketException: An existing connection was forcibly closed by the remote host
/// </summary>
/// <param name="ar"></param>
void OnReadData(IAsyncResult ar)
{
Console.WriteLine("Read");
int result = stream.EndRead(ar); // TODO disconnect & error handling
Console.WriteLine("Read done");
if(result <= 0)
{
Console.WriteLine("Error reading");
return;
}
string message = Utils.ReceiveInformation(data, result, out MessageType type);
OnRead(type, message);
WaitForData();
}
/// <summary>
///
/// </summary>
/// <param name="type"></param>
/// <param name="message"></param>
protected abstract void OnRead(MessageType type, string message);
}
This is the Utils.cs: I am having trouble understanding how there are two methods called ReceiveInformation() and how they interact with each other
public class Utils
{
/// <summary>
///
/// </summary>
/// <param name="stream"></param>
/// <param name="connection"></param>
/// <param name="type"></param>
/// <returns></returns>
public static string ReceiveInformation(NetworkStream stream, TcpClient connection, out MessageType type)
{
byte[] bytes = new byte[connection.ReceiveBufferSize];
int length = stream.Read(bytes, 0, bytes.Length);
return ReceiveInformation(bytes, length, out type);
}
/// <summary>
///
/// </summary>
/// <param name="bytes"></param>
/// <param name="length"></param>
/// <param name="type"></param>
/// <returns></returns>
public static string ReceiveInformation(byte[] bytes, int length, out MessageType type)
{
string data = Encoding.ASCII.GetString(bytes, 0, length);
int iSpace = data.IndexOf(' ');
if(iSpace < 0)
{
// TODO
}
string typeString = data.Substring(0, iSpace);
type = (MessageType)Enum.Parse(typeof(MessageType), typeString);
string message = data.Substring(iSpace + 1, data.Length - iSpace - 1);
return message;
}
/// <summary>
///
/// </summary>
/// <param name="type"></param>
/// <param name="stream"></param>
/// <param name="message"></param>
public static void SendInformation(MessageType type, NetworkStream stream, string message)
{
Byte[] sendBytes = Encoding.UTF8.GetBytes($"{type} {message}");
stream.Write(sendBytes, 0, sendBytes.Length);
}
}

how to create a text file in c# using visual studio?

I have been trying to add a Write text file section to this project but i keep getting told that lots of things 'dont exist in the current context'.
The text tile is meant to be created my documents showing 'First line,new line 1, new line 2'
This should work alongside the kinect sample.
I am sorry that i am very new to this and may be doing it completely wrong.
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MainWindow.xaml.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Microsoft.Samples.Kinect.HDFaceBasics
{
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using Microsoft.Kinect;
using Microsoft.Kinect.Face;
using System.IO;
/// <summary>
/// Main Window
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged, IDisposable
{
// Create a string array with the lines of text
string text = "First line" + Environment.NewLine;
// Set a variable to the My Documents path.
string mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
// Write the text to a new file named "WriteFile.txt".
File.WriteAllText(mydocpath + #"\WriteFile.txt", text);
// Create a string array with the additional lines of text
string[] lines = { "New line 1", "New line 2" };
// Append new lines of text to the file
File.AppendAllLines(mydocpath + #"\WriteFile.txt", lines);
/// <summary>
/// Currently used KinectSensor
/// </summary>
private KinectSensor sensor = null;
/// <summary>
/// Body frame source to get a BodyFrameReader
/// </summary>
private BodyFrameSource bodySource = null;
/// <summary>
/// Body frame reader to get body frames
/// </summary>
private BodyFrameReader bodyReader = null;
/// <summary>
/// HighDefinitionFaceFrameSource to get a reader and a builder from.
/// Also to set the currently tracked user id to get High Definition Face Frames of
/// </summary>
private HighDefinitionFaceFrameSource highDefinitionFaceFrameSource = null;
/// <summary>
/// HighDefinitionFaceFrameReader to read HighDefinitionFaceFrame to get FaceAlignment
/// </summary>
private HighDefinitionFaceFrameReader highDefinitionFaceFrameReader = null;
/// <summary>
/// FaceAlignment is the result of tracking a face, it has face animations location and orientation
/// </summary>
private FaceAlignment currentFaceAlignment = null;
/// <summary>
/// FaceModel is a result of capturing a face
/// </summary>
private FaceModel currentFaceModel = null;
/// <summary>
/// FaceModelBuilder is used to produce a FaceModel
/// </summary>
private FaceModelBuilder faceModelBuilder = null;
/// <summary>
/// The currently tracked body
/// </summary>
private Body currentTrackedBody = null;
/// <summary>
/// The currently tracked body
/// </summary>
private ulong currentTrackingId = 0;
/// <summary>
/// Gets or sets the current tracked user id
/// </summary>
private string currentBuilderStatus = string.Empty;
/// <summary>
/// Gets or sets the current status text to display
/// </summary>
private string statusText = "Ready To Start Capture";
/// <summary>
/// Initializes a new instance of the MainWindow class.
/// </summary>
public MainWindow()
{
this.InitializeComponent();
this.DataContext = this;
}
/// <summary>
/// INotifyPropertyChangedPropertyChanged event to allow window controls to bind to changeable data
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Gets or sets the current status text to display
/// </summary>
public string StatusText
{
get
{
return this.statusText;
}
set
{
if (this.statusText != value)
{
this.statusText = value;
// notify any bound elements that the text has changed
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs("StatusText"));
}
}
}
}
/// <summary>
/// Gets or sets the current tracked user id
/// </summary>
private ulong CurrentTrackingId
{
get
{
return this.currentTrackingId;
}
set
{
this.currentTrackingId = value;
this.StatusText = this.MakeStatusText();
}
}
/// <summary>
/// Gets or sets the current Face Builder instructions to user
/// </summary>
private string CurrentBuilderStatus
{
get
{
return this.currentBuilderStatus;
}
set
{
this.currentBuilderStatus = value;
this.StatusText = this.MakeStatusText();
}
}
/// <summary>
/// Called when disposed of
/// </summary>
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Dispose based on whether or not managed or native resources should be freed
/// </summary>
/// <param name="disposing">Set to true to free both native and managed resources, false otherwise</param>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (this.currentFaceModel != null)
{
this.currentFaceModel.Dispose();
this.currentFaceModel = null;
}
}
}
/// <summary>
/// Returns the length of a vector from origin
/// </summary>
/// <param name="point">Point in space to find it's distance from origin</param>
/// <returns>Distance from origin</returns>
private static double VectorLength(CameraSpacePoint point)
{
var result = Math.Pow(point.X, 2) + Math.Pow(point.Y, 2) + Math.Pow(point.Z, 2);
result = Math.Sqrt(result);
return result;
}
/// <summary>
/// Finds the closest body from the sensor if any
/// </summary>
/// <param name="bodyFrame">A body frame</param>
/// <returns>Closest body, null of none</returns>
private static Body FindClosestBody(BodyFrame bodyFrame)
{
Body result = null;
double closestBodyDistance = double.MaxValue;
Body[] bodies = new Body[bodyFrame.BodyCount];
bodyFrame.GetAndRefreshBodyData(bodies);
foreach (var body in bodies)
{
if (body.IsTracked)
{
var currentLocation = body.Joints[JointType.SpineBase].Position;
var currentDistance = VectorLength(currentLocation);
if (result == null || currentDistance < closestBodyDistance)
{
result = body;
closestBodyDistance = currentDistance;
}
}
}
return result;
}
/// <summary>
/// Find if there is a body tracked with the given trackingId
/// </summary>
/// <param name="bodyFrame">A body frame</param>
/// <param name="trackingId">The tracking Id</param>
/// <returns>The body object, null of none</returns>
private static Body FindBodyWithTrackingId(BodyFrame bodyFrame, ulong trackingId)
{
Body result = null;
Body[] bodies = new Body[bodyFrame.BodyCount];
bodyFrame.GetAndRefreshBodyData(bodies);
foreach (var body in bodies)
{
if (body.IsTracked)
{
if (body.TrackingId == trackingId)
{
result = body;
break;
}
}
}
return result;
}
/// <summary>
/// Gets the current collection status
/// </summary>
/// <param name="status">Status value</param>
/// <returns>Status value as text</returns>
private static string GetCollectionStatusText(FaceModelBuilderCollectionStatus status)
{
string res = string.Empty;
if ((status & FaceModelBuilderCollectionStatus.FrontViewFramesNeeded) != 0)
{
res = "FrontViewFramesNeeded";
return res;
}
if ((status & FaceModelBuilderCollectionStatus.LeftViewsNeeded) != 0)
{
res = "LeftViewsNeeded";
return res;
}
if ((status & FaceModelBuilderCollectionStatus.RightViewsNeeded) != 0)
{
res = "RightViewsNeeded";
return res;
}
if ((status & FaceModelBuilderCollectionStatus.TiltedUpViewsNeeded) != 0)
{
res = "TiltedUpViewsNeeded";
return res;
}
if ((status & FaceModelBuilderCollectionStatus.Complete) != 0)
{
res = "Complete";
return res;
}
if ((status & FaceModelBuilderCollectionStatus.MoreFramesNeeded) != 0)
{
res = "TiltedUpViewsNeeded";
return res;
}
return res;
}
/// <summary>
/// Helper function to format a status message
/// </summary>
/// <returns>Status text</returns>
private string MakeStatusText()
{
string status = string.Format(System.Globalization.CultureInfo.CurrentCulture, "Builder Status: {0}, Current Tracking ID: {1}", this.CurrentBuilderStatus, this.CurrentTrackingId);
return status;
}
/// <summary>
/// Fires when Window is Loaded
/// </summary>
/// <param name="sender">object sending the event</param>
/// <param name="e">event arguments</param>
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.InitializeHDFace();
}
/// <summary>
/// Initialize Kinect object
/// </summary>
private void InitializeHDFace()
{
this.CurrentBuilderStatus = "Ready To Start Capture";
this.sensor = KinectSensor.GetDefault();
this.bodySource = this.sensor.BodyFrameSource;
this.bodyReader = this.bodySource.OpenReader();
this.bodyReader.FrameArrived += this.BodyReader_FrameArrived;
this.highDefinitionFaceFrameSource = new HighDefinitionFaceFrameSource(this.sensor);
this.highDefinitionFaceFrameSource.TrackingIdLost += this.HdFaceSource_TrackingIdLost;
this.highDefinitionFaceFrameReader = this.highDefinitionFaceFrameSource.OpenReader();
this.highDefinitionFaceFrameReader.FrameArrived += this.HdFaceReader_FrameArrived;
this.currentFaceModel = new FaceModel();
this.currentFaceAlignment = new FaceAlignment();
this.InitializeMesh();
this.UpdateMesh();
this.sensor.Open();
}
/// <summary>
/// Initializes a 3D mesh to deform every frame
/// </summary>
private void InitializeMesh()
{
var vertices = this.currentFaceModel.CalculateVerticesForAlignment(this.currentFaceAlignment);
var triangleIndices = this.currentFaceModel.TriangleIndices;
var indices = new Int32Collection(triangleIndices.Count);
for (int i = 0; i < triangleIndices.Count; i += 3)
{
uint index01 = triangleIndices[i];
uint index02 = triangleIndices[i + 1];
uint index03 = triangleIndices[i + 2];
indices.Add((int)index03);
indices.Add((int)index02);
indices.Add((int)index01);
}
this.theGeometry.TriangleIndices = indices;
this.theGeometry.Normals = null;
this.theGeometry.Positions = new Point3DCollection();
this.theGeometry.TextureCoordinates = new PointCollection();
foreach (var vert in vertices)
{
this.theGeometry.Positions.Add(new Point3D(vert.X, vert.Y, -vert.Z));
this.theGeometry.TextureCoordinates.Add(new Point());
}
}
/// <summary>
/// Sends the new deformed mesh to be drawn
/// </summary>
private void UpdateMesh()
{
var vertices = this.currentFaceModel.CalculateVerticesForAlignment(this.currentFaceAlignment);
for (int i = 0; i < vertices.Count; i++)
{
var vert = vertices[i];
this.theGeometry.Positions[i] = new Point3D(vert.X, vert.Y, -vert.Z);
}
}
/// <summary>
/// Start a face capture on clicking the button
/// </summary>
/// <param name="sender">object sending the event</param>
/// <param name="e">event arguments</param>
private void StartCapture_Button_Click(object sender, RoutedEventArgs e)
{
this.StartCapture();
}
/// <summary>
/// This event fires when a BodyFrame is ready for consumption
/// </summary>
/// <param name="sender">object sending the event</param>
/// <param name="e">event arguments</param>
private void BodyReader_FrameArrived(object sender, BodyFrameArrivedEventArgs e)
{
this.CheckOnBuilderStatus();
var frameReference = e.FrameReference;
using (var frame = frameReference.AcquireFrame())
{
if (frame == null)
{
// We might miss the chance to acquire the frame, it will be null if it's missed
return;
}
if (this.currentTrackedBody != null)
{
this.currentTrackedBody = FindBodyWithTrackingId(frame, this.CurrentTrackingId);
if (this.currentTrackedBody != null)
{
return;
}
}
Body selectedBody = FindClosestBody(frame);
if (selectedBody == null)
{
return;
}
this.currentTrackedBody = selectedBody;
this.CurrentTrackingId = selectedBody.TrackingId;
this.highDefinitionFaceFrameSource.TrackingId = this.CurrentTrackingId;
}
}
/// <summary>
/// This event is fired when a tracking is lost for a body tracked by HDFace Tracker
/// </summary>
/// <param name="sender">object sending the event</param>
/// <param name="e">event arguments</param>
private void HdFaceSource_TrackingIdLost(object sender, TrackingIdLostEventArgs e)
{
var lostTrackingID = e.TrackingId;
if (this.CurrentTrackingId == lostTrackingID)
{
this.CurrentTrackingId = 0;
this.currentTrackedBody = null;
if (this.faceModelBuilder != null)
{
this.faceModelBuilder.Dispose();
this.faceModelBuilder = null;
}
this.highDefinitionFaceFrameSource.TrackingId = 0;
}
}
/// <summary>
/// This event is fired when a new HDFace frame is ready for consumption
/// </summary>
/// <param name="sender">object sending the event</param>
/// <param name="e">event arguments</param>
private void HdFaceReader_FrameArrived(object sender, HighDefinitionFaceFrameArrivedEventArgs e)
{
using (var frame = e.FrameReference.AcquireFrame())
{
// We might miss the chance to acquire the frame; it will be null if it's missed.
// Also ignore this frame if face tracking failed.
if (frame == null || !frame.IsFaceTracked)
{
return;
}
frame.GetAndRefreshFaceAlignmentResult(this.currentFaceAlignment);
this.UpdateMesh();
}
}
/// <summary>
/// Start a face capture operation
/// </summary>
private void StartCapture()
{
this.StopFaceCapture();
this.faceModelBuilder = null;
this.faceModelBuilder = this.highDefinitionFaceFrameSource.OpenModelBuilder(FaceModelBuilderAttributes.None);
this.faceModelBuilder.BeginFaceDataCollection();
this.faceModelBuilder.CollectionCompleted += this.HdFaceBuilder_CollectionCompleted;
}
/// <summary>
/// Cancel the current face capture operation
/// </summary>
private void StopFaceCapture()
{
if (this.faceModelBuilder != null)
{
this.faceModelBuilder.Dispose();
this.faceModelBuilder = null;
}
}
/// <summary>
/// This event fires when the face capture operation is completed
/// </summary>
/// <param name="sender">object sending the event</param>
/// <param name="e">event arguments</param>
private void HdFaceBuilder_CollectionCompleted(object sender, FaceModelBuilderCollectionCompletedEventArgs e)
{
var modelData = e.ModelData;
this.currentFaceModel = modelData.ProduceFaceModel();
this.faceModelBuilder.Dispose();
this.faceModelBuilder = null;
this.CurrentBuilderStatus = "Capture Complete";
}
/// <summary>
/// Check the face model builder status
/// </summary>
private void CheckOnBuilderStatus()
{
if (this.faceModelBuilder == null)
{
return;
}
string newStatus = string.Empty;
var captureStatus = this.faceModelBuilder.CaptureStatus;
newStatus += captureStatus.ToString();
var collectionStatus = this.faceModelBuilder.CollectionStatus;
newStatus += ", " + GetCollectionStatusText(collectionStatus);
this.CurrentBuilderStatus = newStatus;
}
}
}
create a method that you can call that will allow you to create as well as append to the file
private static void WriteAndOrAppendText(string path, string strText)
{
using (StreamWriter fileStream = new StreamWriter(path, true))
{
fileStream.WriteLine(strText);
fileStream.Flush();
fileStream.Close();
}
}
// Set a variable to the My Documents path.
string mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
// Write the text to a new file named "WriteFile.txt".
WriteAndOrAppendText(mydocpath + #"\WriteFile.txt", text);
string[] lines = { "New line 1", "New line 2" };
WriteAndOrAppendText(mydocpath , string.Join(",", lines.ToArray()));

C# 2.0 Multi-threading with a plug-in architecture

I have a well established console application in c# 2.0 that uses plugin architecture.
As of right now, the program uses basic multi-threading that can run several instances. The threads are created and continue on until the application is stopped.
Each instance can load its own variety of plugins and configured separately.
Plugins are inherited from a base plugin. This system has been working like a charm for years.
The plugins are event driven, they all read various events to see if they are called upon, if not they return and let the next plugin read the events to see if they are called out to fire.
This system has been working for years. However, I would like to further the scope of multi-threading to allow the plugins to listen to the events in an asynchronous fashion rather than synchronous. One of the drawbacks of this setup is that once a plugin fires and does its work, it locks out the instance. When the next event is fired it has to wait for the previous work to be completed. Then it will allow the next process to take place.
What I would like it to do, is execute the plugin and not have to wait for the process to end before moving on to the next process to begin by an event.
I am stuck with .Net 2.0 for the time being, and must find a solution in that framework. I have looked at numerous examples and I can not find one that meets the criteria. One of the problems is that each plugin has its own time that it may take to process, and there is no way to count to track the percentage that the plugin is complete. The plugins will start and ends its process when it is done. Depending on the parameters of the event, and the plugin it can take any range of time to complete.
My question would be what would be the best way to handle multi-threading in this situation where plugins are executed by events. I have looked at pages such as http://msdn.microsoft.com/en-us/library/2e08f6yc(v=vs.80).aspx and I can figure out where I would be able to have an entry point in an event driven plugin architecture.
If anyone has any clue, I would appreciate it. The lack of multi-threading in this manner has been the Achilles' heel for this application for years.
Plugin base: These contain some functions that are triggered by events:
using System;
using VhaBot.Communication;
namespace VhaBot
{
/// <summary>
/// Plugin BaseClass, must be inherited by all plugins
/// </summary>
public abstract class PluginBase : MarshalByRefObject
{
private bool _locked;
private string _name;
private string _internalName;
private int _version;
private string _author;
private string[] _contributors;
private string _description;
private PluginState _defaultState;
private string[] _dependencies;
private Command[] _commands;
/// <summary>
/// Friendly display name of plugin
/// </summary>
/// <example>
/// <code>
/// this.Name = "Message of the Day";
/// </code>
/// </example>
public string Name
{
set
{
if (_locked)
{
throw new Exception();
}
_name = value;
}
get { return _name; }
}
/// <summary>
/// Internal name of the plugin
/// </summary>
/// <example>
/// <code>
/// this.InternalName = "VhMotd";
/// </code>
/// </example>
public string InternalName
{
set
{
if (_locked)
{
throw new Exception();
}
_internalName = value.ToLower();
}
get { return _internalName; }
}
/// <summary>
/// Pluigin Version
/// </summary>
/// <remarks>
/// Versions are stored as integers only. Version 1.0.0 would have a value of 100
/// </remarks>
/// <example>
/// <code>
/// this.Version = 100;
/// </code>
/// </example>
public int Version
{
set
{
if (_locked)
{
throw new Exception();
}
_version = value;
}
get { return _version; }
}
/// <summary>
/// Author of the plugin
/// </summary>
/// <example>
/// <code>
/// this.Author = "Vhab";
/// </code>
/// </example>
public string Author
{
set
{
if (_locked)
{
throw new Exception();
}
_author = value;
}
get { return _author; }
}
/// <summary>
/// List of contributors to the development of the plugin.
/// </summary>
/// <example>
/// <code>
/// this.Contributors = new string[] { "Iriche", "Kilmanagh" };
/// </code>
/// </example>
public string[] Contributors
{
set
{
if (_locked)
{
throw new Exception();
}
_contributors = value;
}
get
{
if (_contributors != null)
{
return _contributors;
}
return new string[0];
}
}
/// <summary>
/// Description of the plugin
/// </summary>
/// <example>
/// <code>
/// this.Description = "Provides an interface to the user to view who is online and/or on the private channel.";
/// </code>
/// </example>
public string Description
{
set
{
if (_locked)
{
throw new Exception();
}
_description = value;
}
get { return _description; }
}
/// <summary>
/// The default <see cref="VhaBot.PluginState" /> of the plugin
/// </summary>
/// <example>
/// <code>
/// this.DefaultState = PluginState.Installed;
/// </code>
/// </example>
/// <seealso cref="VhaBot.PluginState" />
public PluginState DefaultState
{
set
{
if (_locked)
{
throw new Exception();
}
_defaultState = value;
}
get { return _defaultState; }
}
/// <summary>
/// List of other plugins that a plugin is dependent on to function
/// </summary>
/// <remarks>
/// Plugins are referred to using their internal names. See <see cref="VhaBot.PluginBase.InternalName" />
/// </remarks>
/// <example>
/// <code>
/// this.Dependencies = new string[] { "vhItems" };
/// </code>
/// </example>
public string[] Dependencies
{
set
{
if (_locked)
{
throw new Exception();
}
_dependencies = value;
}
get
{
if (_dependencies != null)
{
return _dependencies;
}
return new string[0];
}
}
public Command[] Commands
{
set
{
if (_locked)
{
throw new Exception();
}
_commands = value;
}
get
{
if (_commands != null)
{
return _commands;
}
return new Command[0];
}
}
internal void Init()
{
_locked = true;
}
/// <summary>
/// A plugin has loaded in response to <see cref="VhaBot.ShellModules.Plugins.Load" />
/// </summary>
/// <param name="bot"></param>
/// ///
/// <remarks>Code inside this method will be executed when a plugin is loading</remarks>
public virtual void OnLoad(BotShell bot)
{
}
/// <summary>
/// A plugin has unloaded in response to <see cref="VhaBot.ShellModules.Plugins.Unload" />
/// </summary>
/// <param name="bot"></param>
/// <remarks>Code inside this method will be executed when a plugin is unloading</remarks>
public virtual void OnUnload(BotShell bot)
{
}
/// <summary>
/// A plugin has installed in response to <see cref="VhaBot.ShellModules.Plugins.Install" />
/// </summary>
/// <param name="bot"></param>
public virtual void OnInstall(BotShell bot)
{
}
/// <summary>
/// A plugin as been uninstalled in response to <see cref="VhaBot.ShellModules.Plugins.Uninstall" />
/// </summary>
/// <param name="bot"></param>
public virtual void OnUninstall(BotShell bot)
{
}
/// <summary>
/// A plugin has been upgraded (Unused)
/// </summary>
/// <param name="bot"></param>
/// <param name="version"></param>
/// <remarks>This function is not active</remarks>
public virtual void OnUpgrade(BotShell bot, Int32 version)
{
}
/// <summary>
/// Response to a command
/// </summary>
/// <param name="bot"></param>
/// <param name="e"></param>
public virtual void OnCommand(BotShell bot, CommandArgs e)
{
}
/// <summary>
/// Response to an unauthorized command
/// </summary>
/// <param name="bot"></param>
/// <param name="e"></param>
public virtual void OnUnauthorizedCommand(BotShell bot, CommandArgs e)
{
}
/// <summary>
/// Response to a command help query <see cref="VhaBot.ShellModules.Commands.GetHelp." />
/// </summary>
/// <param name="bot"></param>
/// <param name="command"></param>
/// <returns></returns>
/// <remarks>Code inside this method will be executed when help is requested</remarks>
public virtual string OnHelp(BotShell bot, string command)
{
return null;
}
/// <summary>
/// Response to a custom configuration
/// </summary>
/// <param name="bot"></param>
/// <param name="key"></param>
/// <returns></returns>
public virtual string OnCustomConfiguration(BotShell bot, string key)
{
return null;
}
/// <summary>
/// Response to a plugin message
/// </summary>
/// <param name="bot"></param>
/// <param name="message"></param>
public virtual void OnPluginMessage(BotShell bot, PluginMessage message)
{
}
/// <summary>
/// Response to a bot message
/// </summary>
/// <param name="bot"></param>
/// <param name="message"></param>
public virtual void OnBotMessage(BotShell bot, BotMessage message)
{
}
/// <summary>
/// Returns display name of bot and current version
/// </summary>
/// <returns></returns>
public override string ToString()
{
return Name + " v" + Version;
}
/// <summary>
/// There is no information to document this command
/// </summary>
/// <param name="bot"></param>
/// <param name="args"></param>
public void FireOnCommand(BotShell bot, CommandArgs args)
{
try
{
if (args.Authorized)
OnCommand(bot, args);
else
OnUnauthorizedCommand(bot, args);
}
catch (Exception ex)
{
CommandArgs e = args;
var window = new RichTextWindow(bot);
window.AppendTitle("Error Report");
window.AppendHighlight("Error: ");
window.AppendNormal(ex.Message);
window.AppendLinkEnd();
window.AppendLineBreak();
window.AppendHighlight("Source: ");
window.AppendNormal(ex.Source);
window.AppendLinkEnd();
window.AppendLineBreak();
window.AppendHighlight("Target Site: ");
window.AppendNormal(ex.TargetSite.ToString());
window.AppendLinkEnd();
window.AppendLineBreak();
window.AppendHighlight("Stack Trace:");
window.AppendLineBreak();
window.AppendNormal(ex.StackTrace);
window.AppendLinkEnd();
window.AppendLineBreak();
bot.SendReply(e,
"There has been an error while executing this command »» " +
window.ToString("More Information"));
BotShell.Output("[Plugin Execution Error] " + ex);
}
}
}
}
Events Class:
namespace VhaBot.ShellModules
{
/// <summary>
/// VhaBot Events
/// </summary>
public class Events
{
public event BotStateChangedHandler BotStateChangedEvent;
public event ChannelJoinEventHandler ChannelJoinEvent;
public event UserJoinChannelHandler UserJoinChannelEvent;
public event UserLeaveChannelHandler UserLeaveChannelEvent;
public event UserLogonHandler UserLogonEvent;
public event UserLogoffHandler UserLogoffEvent;
public event PrivateMessageHandler PrivateMessageEvent;
public event PrivateChannelMessageHandler PrivateChannelMessageEvent;
public event ChannelMessageHandler ChannelMessageEvent;
public event MemberAddedHandler MemberAddedEvent;
public event MemberRemovedHandler MemberRemovedEvent;
public event MemberUpdatedHandler MemberUpdatedEvent;
public event AltAddedHandler AltAddedEvent;
public event AltRemovedHandler AltRemovedEvent;
/// <summary>
/// A message was sent to the IRC channel in response to a <see cref="VhaBot.BotShell.SendIrcMessage" /> request
/// </summary>
public event IrcMessageHandler IrcMessageEvent;
public event ConfigurationChangedHandler ConfigurationChangedEvent;
internal void OnBotStateChanged(BotShell bot, BotStateChangedArgs e)
{
if (BotStateChangedEvent != null)
try
{
BotStateChangedEvent(bot, e);
}
catch
{
}
}
internal void OnChannelJoin(BotShell bot, ChannelJoinEventArgs e)
{
if (ChannelJoinEvent != null)
try
{
ChannelJoinEvent(bot, e);
}
catch
{
}
}
internal void OnUserJoinChannel(BotShell bot, UserJoinChannelArgs e)
{
if (UserJoinChannelEvent != null)
try
{
UserJoinChannelEvent(bot, e);
}
catch
{
}
}
internal void OnUserLeaveChannel(BotShell bot, UserLeaveChannelArgs e)
{
if (UserLeaveChannelEvent != null)
try
{
UserLeaveChannelEvent(bot, e);
}
catch
{
}
}
internal void OnUserLogon(BotShell bot, UserLogonArgs e)
{
if (UserLogonEvent != null)
try
{
UserLogonEvent(bot, e);
}
catch
{
}
}
internal void OnUserLogoff(BotShell bot, UserLogoffArgs e)
{
if (UserLogoffEvent != null)
try
{
UserLogoffEvent(bot, e);
}
catch
{
}
}
internal void OnPrivateMessage(BotShell bot, PrivateMessageArgs e)
{
if (PrivateMessageEvent != null)
try
{
PrivateMessageEvent(bot, e);
}
catch
{
}
}
internal void OnPrivateChannelMessage(BotShell bot, PrivateChannelMessageArgs e)
{
if (PrivateChannelMessageEvent != null)
try
{
PrivateChannelMessageEvent(bot, e);
}
catch
{
}
}
internal void OnChannelMessage(BotShell bot, ChannelMessageArgs e)
{
if (ChannelMessageEvent != null)
try
{
ChannelMessageEvent(bot, e);
}
catch
{
}
}
internal void OnMemberAdded(BotShell bot, MemberAddedArgs e)
{
if (MemberAddedEvent != null)
try
{
MemberAddedEvent(bot, e);
}
catch
{
}
}
internal void OnMemberRemoved(BotShell bot, MemberRemovedArgs e)
{
if (MemberRemovedEvent != null)
try
{
MemberRemovedEvent(bot, e);
}
catch
{
}
}
internal void OnMemberUpdated(BotShell bot, MemberUpdatedArgs e)
{
if (MemberUpdatedEvent != null)
try
{
MemberUpdatedEvent(bot, e);
}
catch
{
}
}
internal void OnAltAdded(BotShell bot, AltAddedArgs e)
{
if (AltAddedEvent != null)
try
{
AltAddedEvent(bot, e);
}
catch
{
}
}
internal void OnAltRemoved(BotShell bot, AltRemovedArgs e)
{
if (AltRemovedEvent != null)
try
{
AltRemovedEvent(bot, e);
}
catch
{
}
}
internal void OnConfigurationChanged(BotShell bot, ConfigurationChangedArgs e)
{
if (ConfigurationChangedEvent != null)
try
{
ConfigurationChangedEvent(bot, e);
}
catch
{
}
}
internal void OnIrcMessage(BotShell bot, IrcMessageArgs e)
{
if (IrcMessageEvent != null)
{
IrcMessageEvent(bot, e);
}
try
{
}
catch
{
}
}
}
}
I've got little to go on as your description of the system is a bit vague but I'll give it a shot.
From your description it seems you have some plugin, say
interface IPlugin {
PluginResult ReadAndExecuteEvents(Events e);
// Added asynchronous methods.
IAsyncResult BeginReadAndExecuteEvents(Events e, AsyncCallback cb, Object state);
PluginResult EndReadAndExecuteEvents(IAsyncResult result);
}
with
class PluginResult
{
public Boolean Stop;
// etc.
}
also you don't seem to be using .NET events, but rather some sort of Event class/enumeration.
Your old code seems to be something like:
foreach (var eventList in ReadEvents())
foreach (var plugin in pluginList)
if (plugin.ReadAndExecuteEvents(eventList).Stop)
break;
You can make this asynchronous doing something like:
foreach (var eventList in ReadEvents())
{
// It seems this is what you want, only one event processed at a time by an "instance"? So block here until unlocked.
LockProcess();
var pluginIndex = 0;
AsyncCallback handleResult = null;
handleResult = delegate(IAsyncResult result)
{
if (pluginList[pluginIndex].EndReadAndExecuteEvents(result).Stop)
goto STOP;
pluginIndex += 1;
if (pluginIndex == pluginList.Count)
goto STOP;
Events e = (Events)result.AsyncState;
pluginList[pluginIndex].BeginReadAndExecuteEvents(e, handleResult, e);
return;
STOP:
UnlockProcess();
};
pluginList[0].BeginReadAndExecuteEvents(eventList, handleResult, eventList);
}
So in .NET 2 style you could add some BeginXxx method and in its AsyncCallback do your stuff.
Of course it is up to the actual plugin to do its multithreading/asynchronisity, say if it writes a file by using BeginWrite to a FileStream etc.
I have conveniently ignored exception handling here.
So, to make your whole application use this asynchronisity you can put this code in a BeginRunEvents method, say, following the same "APM" pattern. You can then schedule this to the threadpool if you wish.
If this is not at all what you are looking for please provide some more code examples/info.

Socket disconnect behavior

I have a small method I use to disable my socket that listens for incoming connections.
/// <summary>
/// Stops and disables the service
/// </summary>
public void Disable() {
if (Running) {
try {
thread.Abort();
}
catch (System.Threading.ThreadAbortException) {
// This catch is not raised.
// We can simply process our closing of the socket and nullify the thread
}
finally {
socket.Close();
socket = null;
thread = null;
if (socket == null && thread == null) {
m_Running = false;
OnDisabled(this, new EventArgs());
}
}
}
}
My problem is that even after I call Close() and nullify the socket, the clients still remain connected. I ran a check using netstat -a, and it shows the clients are still connected.
TCP 127.0.0.1:2161 activate:7777 ESTABLISHED
TCP 127.0.0.1:7777 activate:2161 ESTABLISHED
7777 is the port my host socket listens on. So my question is, after closing the host socket, why do the client sockets not disconnect. How do they remain connected to a socket that is null, and is no longer listenning?
Some additional info
/// <summary>
/// Enables and runs the service
/// </summary>
public void Enable() {
if (!Running) {
ThreadStart start = new ThreadStart(RunServiceAsync);
thread = new Thread(start);
thread.IsBackground = true;
thread.Start();
m_Running = true;
OnEnabled(this, new EventArgs());
}
}
The above method is how the thread is created. Everything works fine, the threading, connections; the only issue is when I close the socket (host), and nullify it the clients are still connected to it.
The question is, once the host socket is closed and set to null, what are the clients connected to? Shouldn't they disconnect and lose connection to the host, because the host socket is closed?
Here is the full code to help
// *********************************************************************
// [DCOM Productions]
// [Copyright (C) DCOM Productions All rights reserved.]
// ***************************************
namespace CipherBox.Drivers {
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.Net;
using System.ComponentModel;
using CipherBox.Objects;
/// <summary>
/// Driver that manages the network connection between the master program and clients, also provides informational events
/// </summary>
public class NetworkDriver : IDriver {
#region Fields
private Socket socket;
private Thread thread;
#endregion
#region Properties
private int m_Port = 7777;
/// <summary>
/// Gets the port that the network runs on. The default port is 7777.
/// </summary>
public int Port {
get {
return m_Port;
}
}
#endregion
#region Events
/// <summary>
/// Delegate for when a node connects to the service
/// </summary>
public delegate void NodeConnectedEventHandler(object sender, NetworkNodeEventArgs e);
/// <summary>
/// Triggers when an node connects to the service
/// </summary>
public event NodeConnectedEventHandler NodeConnected;
/// <summary>
/// Event callback for NodeConnected
/// </summary>
private void OnNodeConnected(object sender, NetworkNodeEventArgs e) {
if (NodeConnected != null) {
foreach (NodeConnectedEventHandler handler in NodeConnected.GetInvocationList()) {
ISynchronizeInvoke syncInvoke = handler.Target as ISynchronizeInvoke;
if (syncInvoke != null && syncInvoke.InvokeRequired) {
syncInvoke.Invoke(handler, new object[] { handler.Target, e });
}
else {
NodeConnected(this, e);
}
}
}
}
/// <summary>
/// Delegate for when a node disconnects from the service
/// </summary>
public delegate void NodeDisconnectedEventHandler(object sender, NetworkNodeEventArgs e);
/// <summary>
/// Triggers when an node disconnects from the service
/// </summary>
public event NodeDisconnectedEventHandler NodeDisconnected;
/// <summary>
/// Event callback for NodeDisconnected
/// </summary>
private void OnNodeDisconnected(object sender, NetworkNodeEventArgs e) {
if (NodeDisconnected != null) {
foreach (NodeDisconnectedEventHandler handler in NodeDisconnected.GetInvocationList()) {
ISynchronizeInvoke syncInvoke = handler.Target as ISynchronizeInvoke;
if (syncInvoke != null && syncInvoke.InvokeRequired) {
syncInvoke.Invoke(handler, new object[] { handler.Target, e });
}
else {
NodeDisconnected(this, e);
}
}
}
}
#endregion
#region Methods
private NetworkNode FillNode(Socket socket) {
StringBuilder stream = new StringBuilder();
byte[] buffer = new byte[4096];
int bytesReceived = -1;
do {
try {
bytesReceived = socket.Receive(buffer, 0, buffer.Length, SocketFlags.None);
}
catch (System.Net.Sockets.SocketException) {
return null;
}
finally {
stream.Append(Encoding.ASCII.GetString(buffer, 0, bytesReceived));
}
} while (!stream.ToString().EndsWith("\r\n\r\n"));
string[] packet = stream.ToString().Split(new string[] { "\r\n" }, StringSplitOptions.None);
if (packet.Length == 9) {
if (packet[0].ToLower() == "CipherBox".ToLower()) {
NetworkNode node = new NetworkNode();
node.Domain = packet[1];
node.LocalIP = IPAddress.Parse(packet[2]);
node.MachineName = packet[3];
node.Platform = packet[4];
node.RemoteIP = IPAddress.Parse(packet[5]);
node.Workgroup = packet[6];
node.Socket = socket;
return node;
}
else {
return null;
}
}
else {
return null;
}
}
private bool IsDisconnected(Socket socket) {
bool connected = false;
try {
connected = !(socket.Poll(1, SelectMode.SelectRead) && socket.Available == 0);
}
catch (System.Net.Sockets.SocketException) {
connected = false;
}
return !connected;
}
private void MonitorNode(NetworkNode node) {
ParameterizedThreadStart start = new ParameterizedThreadStart(MonitorNodeAsync);
Thread thread = new Thread(start);
thread.IsBackground = true;
thread.Start(node);
}
private void MonitorNodeAsync(object obj) {
NetworkNode node = obj as NetworkNode;
while (Running || node != null) {
if (IsDisconnected(node.Socket)) {
node.Socket.Shutdown(SocketShutdown.Both);
node.Socket.Close();
node.Socket = null;
OnNodeDisconnected(null, new NetworkNodeEventArgs(node));
return;
}
else {
Thread.Sleep(1000);
}
}
}
private void RunServiceAsync() {
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint localEP = new IPEndPoint(IPAddress.Any, Port);
socket.Bind(localEP);
socket.Listen(1);
do {
Socket client;
try {
client = socket.Accept();
}
catch (System.Net.Sockets.SocketException) {
continue;
}
NetworkNode node = FillNode(client);
if (node != null) {
OnNodeConnected(null, new NetworkNodeEventArgs(node));
MonitorNode(node);
}
} while (Running);
}
/// <summary>
/// Sets the port that the network runs on
/// </summary>
/// <param name="port">The port to set</param>
public void SetPort(int port) {
m_Port = port;
}
#endregion
#region IDriver Members
/// <summary>
/// Triggered when the network driver is disabled
/// </summary>
public event EventHandler<EventArgs> Disabled;
/// <summary>
/// Event callback for Disabled
/// </summary>
private void OnDisabled(object sender, System.EventArgs e) {
if (Disabled != null) {
foreach (EventHandler<EventArgs> handler in Disabled.GetInvocationList()) {
ISynchronizeInvoke syncInvoke = handler.Target as ISynchronizeInvoke;
if (syncInvoke != null && syncInvoke.InvokeRequired) {
syncInvoke.Invoke(handler, new object[] { handler.Target, e });
}
else {
Disabled(this, e);
}
}
}
}
/// <summary>
/// Triggered when the network driver is enabled
/// </summary>
public event EventHandler<EventArgs> Enabled;
/// <summary>
/// Event callback for Enabled
/// </summary>
private void OnEnabled(object sender, System.EventArgs e) {
if (Enabled != null) {
foreach (EventHandler<EventArgs> handler in Enabled.GetInvocationList()) {
ISynchronizeInvoke syncInvoke = handler.Target as ISynchronizeInvoke;
if (syncInvoke != null && syncInvoke.InvokeRequired) {
syncInvoke.Invoke(handler, new object[] { handler.Target, e });
}
else {
Enabled(this, e);
}
}
}
}
/// <summary>
/// Stops and disables the service
/// </summary>
public void Disable() {
if (Running) {
try {
thread.Abort();
}
catch (System.Threading.ThreadAbortException) {
// This catch is not raised.
// We can simply process our closing of the socket and nullify the thread
}
finally {
socket.Close();
socket = null;
thread = null;
if (socket == null && thread == null) {
m_Running = false;
OnDisabled(this, new EventArgs());
}
}
}
}
/// <summary>
/// Enables and runs the service
/// </summary>
public void Enable() {
if (!Running) {
ThreadStart start = new ThreadStart(RunServiceAsync);
thread = new Thread(start);
thread.IsBackground = true;
thread.Start();
m_Running = true;
OnEnabled(this, new EventArgs());
}
}
private bool m_Running = false;
/// <summary>
/// Gets a System.Boolean value indicating whether the service is running or not
/// </summary>
public bool Running {
get {
return m_Running;
}
}
#endregion
}
}
Yo have to call socket.shutdown(Both) the parameter could be Send, Receive or Both Depending of how you like to end the connection. This function send the necessary TCP message to the client to close the connection.
I corrected the issue by storing references to each connection in a collection and closing all the connections using the client sockets.
private void DestructConnections() {
foreach (Socket connection in connections) {
connection.Shutdown(SocketShutdown.Both);
connection.Close();
}
connections.Clear();
}
It is recommended that when using a connection oriented socket you should call Shutdown before you Close the connection. Shutdown is for notifying communication end. Close is for releasing the managed / unmanaged resources
Resource:
http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.shutdown.aspx
When you work with sockets in any language, you create server socket which only listen and accept connections and there're many p2p sockets to send and/or receive data.
So your answer is design of sockets subsystem in Microsoft Windows, BSD, Linux, etc.

Categories