コード例 #1
0
        public WebSocketServer(string location, bool supportDualStack = true)
        {
            var uri = new Uri(location);

            Port             = uri.Port;
            Location         = location;
            SupportDualStack = supportDualStack;

            _locationIP = ParseIPAddress(uri);
            _scheme     = uri.Scheme;
            var socket = new Socket(_locationIP.AddressFamily, SocketType.Stream, ProtocolType.IP);

            socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);

            if (SupportDualStack)
            {
                if (!FleckRuntime.IsRunningOnMono() && FleckRuntime.IsRunningOnWindows())
                {
                    socket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false);
                }
            }

            ListenerSocket        = new SocketWrapper(socket);
            SupportedSubProtocols = new string[0];
        }
コード例 #2
0
        public static void SetKeepAlive(this Socket socket, UInt32 keepAliveInterval, UInt32 retryInterval)
        {
            if (FleckRuntime.IsRunningOnWindows())
            {
                int    size = sizeof(UInt32);
                UInt32 on   = 1;

                byte[] inArray = new byte[size * 3];
                Array.Copy(BitConverter.GetBytes(on), 0, inArray, 0, size);
                Array.Copy(BitConverter.GetBytes(keepAliveInterval), 0, inArray, size, size);
                Array.Copy(BitConverter.GetBytes(retryInterval), 0, inArray, size * 2, size);
                socket.IOControl(IOControlCode.KeepAliveValues, inArray, null);
            }
        }
コード例 #3
0
ファイル: SocketWrapper.cs プロジェクト: HubWong/Edu2
        public SocketWrapper(Socket socket)
        {
            _tokenSource = new CancellationTokenSource();
            _taskFactory = new TaskFactory(_tokenSource.Token);
            _socket      = socket;
            if (_socket.Connected)
            {
                _stream = new NetworkStream(_socket);
            }

            // The tcp keepalive default values on most systems
            // are huge (~7200s). Set them to something more reasonable.
            if (FleckRuntime.IsRunningOnWindows())
            {
                SetKeepAlive(socket, KeepAliveInterval, RetryInterval);
            }
        }