Пример #1
0
        public SocketResultCode StartListen(string address, int port)
        {
            _lastSocketError = SocketResultCode.OK;

            Socket sock;

            lock (this)
            {
                if (null == _socket)
                {
                    return(SocketResultCode.NotInitialized);
                }

                sock = _socket;
            }

            IPAddress addr;

            addr = address == null ? IPAddress.Any : GetIpForHostname(address);
            if (addr == NoAddress)
            {
                return(SocketResultCode.CantResolveHostname);
            }

            var endPoint = new IPEndPoint(addr, port);

            try
            {
                sock.ExclusiveAddressUse = true;
                _socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, false);
                _socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true);
                sock.Bind(endPoint);
            }
            catch (ObjectDisposedException)
            {
                return(SocketResultCode.SocketNotAvailableAnymore);
            }
            catch (SocketException e)
            {
                _lastSocketError = SocketTools.NativeToSocketError(e.NativeErrorCode);
                return(SocketResultCode.BindFailed);
            }

            try
            {
                sock.Listen(16);
            }
            catch (ObjectDisposedException)
            {
                return(SocketResultCode.SocketNotAvailableAnymore);
            }
            catch (SocketException e)
            {
                _lastSocketError = SocketTools.NativeToSocketError(e.NativeErrorCode);
                return(SocketResultCode.ListenFailed);
            }

            _isListening = true;
            return(StartAccept(sock));
        }
Пример #2
0
        private void OnSentCallback(IAsyncResult ar)
        {
            var sock = (Socket)ar.AsyncState;

            if (!sock.Connected)
            {
                Disconnect(true);
                return;
            }

            int sentBytes;

            try
            {
                sentBytes = sock.EndSend(ar);
            }
            catch (ObjectDisposedException)
            {
                OnConnectionError(SocketResultCode.SocketNotAvailableAnymore);
                Disconnect(true);
            }
            catch (SocketException e)
            {
                OnSocketError(SocketResultCode.SendFailed, SocketTools.NativeToSocketError(e.NativeErrorCode));
                Disconnect(true);
            }
        }
Пример #3
0
        public SocketResultCode Connect(string address, int port)
        {
            Socket sock;

            _lastSocketError = SocketResultCode.OK;

            lock (this)
            {
                if (null == _socket)
                {
                    return(SocketResultCode.NotInitialized);
                }
                sock = _socket;
            }

            if (sock.Connected)
            {
                return(SocketResultCode.AlreadyConnected);
            }

            var addr = GetIpForHostname(address);

            if (addr == NoAddress)
            {
                return(SocketResultCode.CantResolveHostname);
            }

            var endpt = new IPEndPoint(addr, port);

            try
            {
                sock.BeginConnect(endpt, OnConnectCallback, sock);
            }
            catch (ObjectDisposedException)
            {
                return(SocketResultCode.SocketNotAvailableAnymore);
            }
            catch (SocketException e)
            {
                _lastSocketError = SocketTools.NativeToSocketError(e.NativeErrorCode);
                return(SocketResultCode.CantConnect);
            }
            catch (InvalidOperationException)
            {
                return(SocketResultCode.CantConnect);
            }
            return(SocketResultCode.OK);
        }
Пример #4
0
        private void OnReceivedCallback(IAsyncResult ar)
        {
            var sock = (Socket)ar.AsyncState;

            int readBytes;

            try
            {
                readBytes = sock.EndReceive(ar);
            }
            catch (ObjectDisposedException)
            {
                OnConnectionError(SocketResultCode.SocketNotAvailableAnymore);
                Disconnect(true);
                return;
            }
            catch (SocketException e)
            {
                OnSocketError(SocketResultCode.ReceiveFailed, SocketTools.NativeToSocketError(e.NativeErrorCode));
                Disconnect(true);
                return;
            }

            if (readBytes <= 0)
            {
                Disconnect(true);
                return;
            }
            OnReceived(_receiveBuffer, readBytes);

            try
            {
                sock.BeginReceive(_receiveBuffer, 0, _receiveBuffer.Length, SocketFlags.None, OnReceivedCallback, sock);
            }
            catch (ObjectDisposedException)
            {
                OnConnectionError(SocketResultCode.SocketNotAvailableAnymore);
                Disconnect(true);
            }
            catch (SocketException e)
            {
                OnSocketError(SocketResultCode.CantStartReceive, SocketTools.NativeToSocketError(e.NativeErrorCode));
                Disconnect(true);
            }
        }
Пример #5
0
        public SocketResultCode StartReceive()
        {
            _lastSocketError = SocketResultCode.OK;

            Socket sock;

            lock (this)
            {
                if (null == _socket)
                {
                    return(SocketResultCode.NotInitialized);
                }
                sock = _socket;
            }

            if (!sock.Connected)
            {
                return(SocketResultCode.NotConnected);
            }

            if (_isListening)
            {
                return(SocketResultCode.IsListeningSocket);
            }

            if (null == _receiveBuffer)
            {
                _receiveBuffer = new byte[sock.ReceiveBufferSize];
            }

            try
            {
                sock.BeginReceive(_receiveBuffer, 0, _receiveBuffer.Length, SocketFlags.None, OnReceivedCallback, sock);
            }
            catch (ObjectDisposedException)
            {
                return(SocketResultCode.SocketNotAvailableAnymore);
            }
            catch (SocketException e)
            {
                _lastSocketError = SocketTools.NativeToSocketError(e.NativeErrorCode);
                return(SocketResultCode.CantStartReceive);
            }
            return(SocketResultCode.OK);
        }
Пример #6
0
        public SocketResultCode Init()
        {
            _lastSocketError = SocketResultCode.OK;
            if (null != _socket)
            {
                return(SocketResultCode.AlreadyInitialized);
            }

            try
            {
                _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                _socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
            }
            catch (SocketException e)
            {
                _lastSocketError = SocketTools.NativeToSocketError(e.NativeErrorCode);
                return(SocketResultCode.CantCreateSocket);
            }
            return(SocketResultCode.OK);
        }
Пример #7
0
        private void Disconnect(bool forceCallback)
        {
            _lastSocketError = SocketResultCode.OK;

            bool wasConnected;

            lock (this)
            {
                if (null == _socket)
                {
                    return;
                }

                wasConnected = _socket.Connected;

                try
                {
                    _socket.Shutdown(SocketShutdown.Both);
                }
                catch (ObjectDisposedException)
                {
                    // EMPTY
                }
                catch (SocketException e)
                {
                    _lastSocketError = SocketTools.NativeToSocketError(e.NativeErrorCode);
                }

                _socket.Close();
                _socket = null;
                System.Threading.Thread.Yield();
            }

            if (wasConnected || forceCallback)
            {
                OnDisconnected();
            }
        }
Пример #8
0
        private void OnConnectCallback(IAsyncResult ar)
        {
            var sock = (Socket)ar.AsyncState;

            try
            {
                sock.EndConnect(ar);
            }
            catch (ObjectDisposedException)
            {
                OnConnectFailed(SocketResultCode.SocketNotAvailableAnymore);
                return;
            }
            catch (SocketException e)
            {
                OnConnectFailed(SocketTools.NativeToSocketError(e.NativeErrorCode));
                return;
            }

            if (!sock.Connected)
            {
                OnConnectFailed(SocketResultCode.CantConnect);
                return;
            }

            OnConnected();

            var res = StartReceive();

            if (res == SocketResultCode.OK)
            {
                return;
            }

            OnSocketError(res, _lastSocketError);
            Disconnect();
        }
Пример #9
0
 private SocketResultCode StartAccept(Socket socket)
 {
     try
     {
         socket.BeginAccept(OnAcceptCallback, socket);
     }
     catch (ObjectDisposedException)
     {
         return(SocketResultCode.SocketNotAvailableAnymore);
     }
     catch (SocketException e)
     {
         return(SocketTools.NativeToSocketError(e.NativeErrorCode));
     }
     catch (InvalidOperationException)
     {
         return(SocketResultCode.CantStartListener);
     }
     catch (NotSupportedException)
     {
         return(SocketResultCode.NotSupportedOnOS);
     }
     return(SocketResultCode.OK);
 }