Beispiel #1
0
 public bool CloseConnection()
 {
     lock (lockObject)
     {
         if (_socketQueue == null)
         {
             return(true);
         }
         _establishSocket = null;
         var socketQueue = _socketQueue;
         _socketQueue = null;
         socketQueue.disconnectSocket();
         if (Thread.CurrentThread != _socketReceiveThread)
         {
             _socketReceiveThread.Abort();
         }
         socketQueue.sendThreadCancellation.Cancel(true);
     }
     // If send or receive thread is calling CloseConnection, it will exit after this call
     if (Thread.CurrentThread != _socketReceiveThread)
     {
         _socketReceiveThread.Join();
     }
     if (Thread.CurrentThread != _socketSendQueueThread)
     {
         _socketSendQueueThread.Join();
     }
     return(true);
 }
        public bool EstablishConnection(Action<MetaLinkProtobuf.Edit> EditMessageReceived, Action<Exception> connectionClosed)
        {
            lock (lockObject)
            {
                _socketQueue = new SocketQueue();
                if (!_socketQueue.establishSocket())
                {
                    _socketQueue = null;
                    return false;
                }
                _socketQueue.EditMessageReceived += EditMessageReceived;
                _socketQueue.ReceiveError += x =>
                {
                    CloseConnection();
                    connectionClosed(x);
                };

                _socketSendQueueThread = new Thread(new ThreadStart(_socketQueue.sendThread));
                _socketSendQueueThread.Name = "MetaLinkBridge Send";
                _socketSendQueueThread.Start();

                _receiveThread = new Thread(new ThreadStart(_socketQueue.receiveThread));
                _receiveThread.Name = "MetaLinkBridge Receive";
                _receiveThread.Start();

                ConnectionEnabled = true;
                closeRequested = false;

                return true;
            }
        }
Beispiel #3
0
        public async Task <bool> EstablishConnection(Action <MetaLinkProtobuf.Edit> EditMessageReceived, Action <Exception> connectionClosed, Action <MetaLinkProtobuf.Edit> EditMessageSent = null)
        {
            if (_establishSocket != null)
            {
                // our only client passes the same callbacks, so this isn't necessary:
                // assert connectionClosed == this.connectionClosed
                return(await _establishSocket);
            }
            var socketQueue = new SocketQueue();

            lock (lockObject)
            {
                socketQueue      = new SocketQueue();
                _establishSocket = socketQueue.establishSocket();
            }
            bool connected = await _establishSocket;

            lock (lockObject)
            {
                if (!connected)
                {
                    _establishSocket = null;
                    return(false);
                }
                if (_establishSocket == null)
                {
                    // Disconnect() was called
                    return(false);
                }
                _socketQueue = socketQueue;
                if (EditMessageSent != null)
                {
                    _socketQueue.EditMessageSent += EditMessageSent;
                }
                _socketQueue.EditMessageReceived += EditMessageReceived;
                _socketQueue.ReceiveError        += x =>
                {
                    CloseConnection();
                    connectionClosed(x);
                };

                _socketSendQueueThread      = new Thread(new ThreadStart(_socketQueue.sendThread));
                _socketSendQueueThread.Name = "MetaLinkBridge Send";
                _socketSendQueueThread.Start();

                _socketReceiveThread      = new Thread(new ThreadStart(_socketQueue.receiveThread));
                _socketReceiveThread.Name = "MetaLinkBridge Receive";
                _socketReceiveThread.Start();

                return(true);
            }
        }
        public bool CloseConnection()
        {
            lock (lockObject)
            {
                if (_socketQueue == null)
                    return true;
                if (!closeRequested)
                {
                    closeRequested = true;

                    if (_socketQueue == null)
                    {
                        return true;
                    }
                    _socketQueue.disconnectSocket();
                    if (Thread.CurrentThread != _receiveThread)
                    {
                        _receiveThread.Abort();
                    }
                    if (Thread.CurrentThread != _socketSendQueueThread)
                    {
                        _socketQueue.sendThreadCancellation.Cancel(true);
                        _socketSendQueueThread.Abort();
                    }
                    _socketQueue = null;
                    ConnectionEnabled = false;
                }
            }
            // If send or receive thread is calling CloseConnection, it will exit after this call
            if (Thread.CurrentThread != _receiveThread)
            {
                _receiveThread.Join();
            }
            if (Thread.CurrentThread != _socketSendQueueThread)
            {
                _socketSendQueueThread.Join();
            }
            return true;
        }