Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Foo
- {
- private Socket sock = null;
- private Thread tListenerThread = null
- private bool bInitialised;
- private Object InitLock = null;
- private Object DeInitLock = null;
- public Foo()
- {
- bInitialised = false;
- InitLock = new Object();
- DeInitLock = new Object();
- }
- public bool initialise()
- {
- if (null == InitLock)
- return false;
- lock (InitLock)
- {
- if (bInitialised)
- return false;
- sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 8);
- sock.Bind( /*localIpEndPoint*/);
- sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(mcIP));
- tListenerThread = new Thread(new ThreadStart(listener));
- tListenerThread.Start();
- bInitialised = true;
- return true;
- }
- }
- ~Foo()
- {
- if (bInitialised)
- deInitialise();
- }
- private void deInitialise()
- {
- if (null == DeInitLock)
- return;
- lock (DeInitLock)
- {
- if (bInitialised)
- {
- sock.Shutdown(SocketShutdown.Both); //throws System.ObjectDisposedException
- sock.Close();
- tListenerThread.Abort(); //terminates xunit test!
- tListenerThread = null;
- sock = null;
- bInitialised = false;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment