Beispiel #1
0
        public void Init(Socket socket = null)
        {
            if (_state != 0)
            {
                return;
            }

            var oldValue = Interlocked.Exchange(ref _state, 1);

            if (oldValue == 1)
            {
                return;
            }

            if (socket == null)
            {
                _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                var inOptionValues = NetworkSettings.GetClientKeepAliveInfo();
                _socket.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null);
                _socket.NoDelay        = true;
                _socket.ReceiveTimeout = NetworkSettings.ReceiveTimeout;
                _socket.SendTimeout    = NetworkSettings.SendTimeout;
                _socket.Connect(RemoteIP, RemotePort);
            }
            else
            {
                _socket         = socket;
                _socket.NoDelay = true;
            }

            BeginReceive();
        }
Beispiel #2
0
        public void Init(Socket socket = null)
        {
            if (_state != 0)
            {
                return;
            }

            var originalState = Interlocked.CompareExchange(ref _state, 1, 0);

            if (originalState != 0)
            {
                return;
            }

            if (socket == null)
            {
                _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                var inOptionValues = NetworkSettings.GetClientKeepAliveInfo();
                _socket.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null);
                _socket.NoDelay        = true;
                _socket.ReceiveTimeout = NetworkSettings.ReceiveTimeout;
                _socket.SendTimeout    = NetworkSettings.SendTimeout;

                //_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, 4000);
                //_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, 4000);
                //_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
                //_socket.LingerState = new System.Net.Sockets.LingerOption(true, 1);

                _socket.Connect(RemoteIP, RemotePort);
            }
            else
            {
                _socket         = socket;
                _socket.NoDelay = true;
            }

            var originalState2 = Interlocked.CompareExchange(ref _state, 2, 1);

            if (originalState2 != 1)
            {
                return;
            }

            BeginNewReceive(true);
        }