Example #1
0
        public async Task <Connection> EstablishConnectionAsync()
        {
            SocketWrapper closableWrapper = null;

            try
            {
                Stream stream = null;
#if TARGET_BROWSER
                closableWrapper = new SocketWrapper(_listenSocket);
                stream          = new WebSocketStream(_listenSocket, ownsSocket: true);
#else
                var socket = await _listenSocket.AcceptAsync().ConfigureAwait(false);

                closableWrapper = new SocketWrapper(socket);

                try
                {
                    socket.NoDelay = true;
                }
                // OSX can throw if socket is in weird state during close or cancellation
                catch (SocketException ex) when(ex.SocketErrorCode == SocketError.InvalidArgument && PlatformDetection.IsOSXLike)
                {
                }

                stream = new NetworkStream(socket, ownsSocket: false);
#endif
                return(await Connection.CreateAsync(closableWrapper, stream, _options).ConfigureAwait(false));
            }
            catch (Exception)
            {
                closableWrapper?.Close();
                throw;
            }
        }
Example #2
0
        private void Communication_Begin(SocketWrapper socket)
        {
            var stop = false;

            while (!stop)
            {
                var command = socket.ReadString();

                switch (command)
                {
                case Connection.STOP:
                    stop = true;
                    socket.Close(false);
                    this.IsConnectionInProgress = false;
                    break;

                case Connection.EXIT:
                    stop           = true;
                    this.IsExiting = true;
                    break;

                default:
                    this.Execute(socket, command);
                    break;
                }
            }
        }