예제 #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));
        }
        private void OnNewSocketConnectFailed(SocketWrapper sock, SocketResultCode result)
        {
            var conn = GetConnection(sock);

            sock.Connected     -= OnNewSocketConnected;
            sock.ConnectFailed -= OnNewSocketConnectFailed;
            OnConnectFailed(conn, Converter.SockResToConnRes(result));
            conn.Shutdown();
        }
예제 #3
0
        private void SocketNativeErrorHandler(SocketWrapper sock, SocketResultCode result, SocketResultCode nativeError)
        {
            lock (_receiveQueue)
            {
                _receiveQueue.Enqueue(null);

                var resultInt   = (int)result;
                var resultBytes = BitConverter.GetBytes(resultInt);
                _receiveQueue.Enqueue(resultBytes);

                resultInt   = (int)nativeError;
                resultBytes = BitConverter.GetBytes(resultInt);
                _receiveQueue.Enqueue(resultBytes);
            }
        }
예제 #4
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);
        }
예제 #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();
            }
        }
        public static ConnectionResult SockResToConnRes(SocketResultCode res)
        {
            switch (res)
            {
            case SocketResultCode.CantResolveHostname:
                return(ConnectionResult.CantResolveHostname);

            case SocketResultCode.NotConnected:
                return(ConnectionResult.NotConnected);

            case SocketResultCode.CantStartSend:
                return(ConnectionResult.CantSend);

            case SocketResultCode.WSAECONNREFUSED:
                return(ConnectionResult.ConnectionRefused);

            case SocketResultCode.WSAEADDRINUSE:
                return(ConnectionResult.AddressInUse);

            case SocketResultCode.WSAECONNRESET:
                return(ConnectionResult.ConnectionResetByPeer);

            case SocketResultCode.WSAETIMEDOUT:
                return(ConnectionResult.Timeout);

            case SocketResultCode.WSAESHUTDOWN:
                return(ConnectionResult.SocketIsShutdown);

            case SocketResultCode.WSAECONNABORTED:
                return(ConnectionResult.ConnectionAborted);

            case SocketResultCode.WSAEINVAL:
                return(ConnectionResult.InvalidArguments);
            }
            Console.WriteLine("Unresolved error code: {0}", res);
            return(ConnectionResult.Unknown);
        }
 private void OnListenSocketConnectionError(SocketWrapper sock, SocketResultCode result)
 {
     //OnConnectFailed( null, ConnectionResult.CantStartListeningSocket );
 }
예제 #10
0
 private void SocketErrorHandler(SocketWrapper sock, SocketResultCode result)
 {
     SocketNativeErrorHandler(sock, result, SocketResultCode.GeneralFailure);
 }
예제 #11
0
 protected void OnSocketError(SocketResultCode result, SocketResultCode nativeError)
 {
     SocketError?.Invoke(this, result, nativeError);
 }
예제 #12
0
 protected void OnConnectionError(SocketResultCode result)
 {
     ConnectionError?.Invoke(this, result);
 }
예제 #13
0
 protected void OnConnectFailed(SocketResultCode result)
 {
     ConnectFailed?.Invoke(this, result);
 }