Пример #1
0
        public unsafe PosixResult TryGetPeerIPAddress(out IPEndPointStruct ep)
        {
            IPSocketAddress socketAddress;
            var             rv = SocketInterop.GetPeerName(this, (byte *)&socketAddress, sizeof(IPSocketAddress));

            if (rv.IsSuccess)
            {
                ep = socketAddress.ToIPEndPoint();
            }
            else
            {
                ep = default(IPEndPointStruct);
            }
            return(rv);
        }
        public unsafe IPSocketAddress(IPEndPointStruct endPoint)
        {
            bool ipv4 = endPoint.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork;

            FlowInfo = 0;
            _family  = (short)(ipv4 ? AddressFamily.InterNetwork : AddressFamily.InterNetworkV6);
            ScopeId  = ipv4 ? 0 : (uint)endPoint.Address.ScopeId;
            Port     = (ushort)endPoint.Port;
            var bytes = endPoint.Address.GetAddressBytes();

            fixed(byte *address = Address)
            {
                for (int i = 0; i < (ipv4 ? 4 : 16); i++)
                {
                    address[i] = bytes[i];
                }
            }
        }
Пример #3
0
            private int HandleAccept(TSocket tacceptSocket)
            {
                var         type     = tacceptSocket.Type;
                int         clientFd = -1;
                PosixResult result;

                if (type == SocketFlags.TypeAccept)
                {
                    // TODO: should we handle more than 1 accept? If we do, we shouldn't be to eager
                    //       as that might give the kernel the impression we have nothing to do
                    //       which could interfere with the SO_REUSEPORT load-balancing.
                    result = tacceptSocket.TryAccept(out clientFd, blocking: false);
                }
                else
                {
                    result = tacceptSocket.TryReceiveSocket(out clientFd, blocking: false);
                    if (result.Value == 0)
                    {
                        // The socket passing us file descriptors has closed.
                        // We dispose our end so we get get removed from the epoll.
                        tacceptSocket.Close();
                        return(0);
                    }
                }
                if (result.IsSuccess)
                {
                    TSocket tsocket;
                    try
                    {
                        SocketFlags flags = SocketFlags.TypeClient | (tacceptSocket.IsDeferSend ? SocketFlags.DeferSend : SocketFlags.None);
                        tsocket = new TSocket(this, clientFd, flags)
                        {
                            ZeroCopyThreshold = tacceptSocket.ZeroCopyThreshold
                        };

                        bool ipSocket = !object.ReferenceEquals(tacceptSocket.LocalAddress, NotIPSocket);

                        // Store the last LocalAddress on the tacceptSocket so we might reuse it instead
                        // of allocating a new one for the same address.
                        IPEndPointStruct localAddress  = default(IPEndPointStruct);
                        IPEndPointStruct remoteAddress = default(IPEndPointStruct);
                        if (ipSocket && tsocket.TryGetLocalIPAddress(out localAddress, tacceptSocket.LocalAddress))
                        {
                            tsocket.LocalAddress = localAddress.Address;
                            tsocket.LocalPort    = localAddress.Port;
                            if (tsocket.TryGetPeerIPAddress(out remoteAddress))
                            {
                                tsocket.RemoteAddress = remoteAddress.Address;
                                tsocket.RemotePort    = remoteAddress.Port;
                            }
                        }
                        else
                        {
                            // This is not an IP socket.
                            tacceptSocket.LocalAddress = NotIPSocket;
                            ipSocket = false;
                        }

                        if (ipSocket)
                        {
                            tsocket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, 1);
                        }
                    }
                    catch
                    {
                        IOInterop.Close(clientFd);
                        return(0);
                    }

                    _connectionDispatcher.OnConnection(tsocket);

                    lock (_sockets)
                    {
                        _sockets.Add(clientFd, tsocket);
                    }

                    bool dataMayBeAvailable = tacceptSocket.IsDeferAccept;
                    tsocket.Start(dataMayBeAvailable);

                    return(1);
                }
                else
                {
                    return(0);
                }
            }
Пример #4
0
        public unsafe PosixResult TryConnect(IPEndPointStruct endpoint)
        {
            IPSocketAddress socketAddress = new IPSocketAddress(endpoint);

            return(SocketInterop.Connect(this, (byte *)&socketAddress, sizeof(IPSocketAddress)));
        }
Пример #5
0
 public void Connect(IPEndPointStruct endpoint)
 {
     TryConnect(endpoint)
     .ThrowOnError();
 }
Пример #6
0
 public void Bind(IPEndPointStruct endpoint)
 {
     TryBind(endpoint)
     .ThrowOnError();
 }