Beispiel #1
0
        public StreamSocket(TransportInstance instance, Socket fd)
        {
            _instance = instance;
            _fd       = fd;
            _state    = StateConnected;

            try
            {
                _desc = Network.FdToString(_fd);
            }
            catch (Exception)
            {
                Network.CloseSocketNoThrow(_fd);
                throw;
            }

            Network.SetBlock(_fd, false);
            Network.SetTcpBufSize(_fd, _instance);

            _readEventArgs            = new SocketAsyncEventArgs();
            _readEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(IoCompleted);

            _writeEventArgs            = new SocketAsyncEventArgs();
            _writeEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(IoCompleted);

            //
            // For timeouts to work properly, we need to receive/send
            // the data in several chunks. Otherwise, we would only be
            // notified when all the data is received/written. The
            // connection timeout could easily be triggered when
            // receiving/sending large messages.
            //
            _maxSendPacketSize = Math.Max(512, Network.GetSendBufferSize(_fd));
            _maxRecvPacketSize = Math.Max(512, Network.GetRecvBufferSize(_fd));
        }
Beispiel #2
0
        public StreamSocket(TransportInstance instance, INetworkProxy?proxy, EndPoint addr, IPAddress?sourceAddr)
        {
            _instance   = instance;
            _proxy      = proxy;
            _addr       = addr;
            _sourceAddr = sourceAddr;
            _fd         = Network.CreateSocket(false, (_proxy != null ? _proxy.GetAddress() : _addr).AddressFamily);
            _state      = StateNeedConnect;

            Network.SetBlock(_fd, false);
            Network.SetTcpBufSize(_fd, _instance);

            _readEventArgs            = new SocketAsyncEventArgs();
            _readEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(IoCompleted);

            _writeEventArgs            = new SocketAsyncEventArgs();
            _writeEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(IoCompleted);

            //
            // For timeouts to work properly, we need to receive/send
            // the data in several chunks. Otherwise, we would only be
            // notified when all the data is received/written. The
            // connection timeout could easily be triggered when
            // receiving/sending large messages.
            //
            _maxSendPacketSize = Math.Max(512, Network.GetSendBufferSize(_fd));
            _maxRecvPacketSize = Math.Max(512, Network.GetRecvBufferSize(_fd));
        }
Beispiel #3
0
        public Ice.ConnectionInfo GetInfo()
        {
            var    info = new Ice.TCPConnectionInfo();
            Socket?fd   = _stream.Fd();

            if (fd != null)
            {
                EndPoint localEndpoint = Network.GetLocalAddress(fd);
                info.LocalAddress = Network.EndpointAddressToString(localEndpoint);
                info.LocalPort    = Network.EndpointPort(localEndpoint);
                EndPoint?remoteEndpoint = Network.GetRemoteAddress(fd);
                info.RemoteAddress = Network.EndpointAddressToString(remoteEndpoint);
                info.RemotePort    = Network.EndpointPort(remoteEndpoint);
                info.RcvSize       = Network.GetRecvBufferSize(fd);
                info.SndSize       = Network.GetSendBufferSize(fd);
            }
            return(info);
        }
Beispiel #4
0
        public Ice.ConnectionInfo GetInfo()
        {
            var info = new Ice.UDPConnectionInfo();

            if (_fd != null)
            {
                EndPoint localEndpoint = Network.GetLocalAddress(_fd);
                info.LocalAddress = Network.EndpointAddressToString(localEndpoint);
                info.LocalPort    = Network.EndpointPort(localEndpoint);
                if (_state == StateNotConnected)
                {
                    if (_peerAddr != null)
                    {
                        info.RemoteAddress = Network.EndpointAddressToString(_peerAddr);
                        info.RemotePort    = Network.EndpointPort(_peerAddr);
                    }
                }
                else
                {
                    EndPoint?remoteEndpoint = Network.GetRemoteAddress(_fd);
                    if (remoteEndpoint != null)
                    {
                        info.RemoteAddress = Network.EndpointAddressToString(remoteEndpoint);
                        info.RemotePort    = Network.EndpointPort(remoteEndpoint);
                    }
                }
                info.RcvSize = Network.GetRecvBufferSize(_fd);
                info.SndSize = Network.GetSendBufferSize(_fd);
            }

            if (_mcastAddr != null)
            {
                info.McastAddress = Network.EndpointAddressToString(_mcastAddr);
                info.McastPort    = Network.EndpointPort(_mcastAddr);
            }
            return(info);
        }
Beispiel #5
0
        private void SetBufSize(int rcvSize, int sndSize)
        {
            Debug.Assert(_fd != null);

            for (int i = 0; i < 2; ++i)
            {
                bool   isSnd;
                string direction;
                string prop;
                int    dfltSize;
                int    sizeRequested;
                if (i == 0)
                {
                    isSnd         = false;
                    direction     = "receive";
                    prop          = "Ice.UDP.RcvSize";
                    dfltSize      = Network.GetRecvBufferSize(_fd);
                    sizeRequested = rcvSize;
                    _rcvSize      = dfltSize;
                }
                else
                {
                    isSnd         = true;
                    direction     = "send";
                    prop          = "Ice.UDP.SndSize";
                    dfltSize      = Network.GetSendBufferSize(_fd);
                    sizeRequested = sndSize;
                    _sndSize      = dfltSize;
                }

                //
                // Get property for buffer size if size not passed in.
                //
                if (sizeRequested == -1)
                {
                    sizeRequested = _instance.Communicator.GetPropertyAsInt(prop) ?? dfltSize;
                }
                //
                // Check for sanity.
                //
                if (sizeRequested < (UdpOverhead + IceInternal.Protocol.headerSize))
                {
                    _instance.Logger.Warning($"Invalid {prop} value of {sizeRequested} adjusted to {dfltSize}");
                    sizeRequested = dfltSize;
                }

                if (sizeRequested != dfltSize)
                {
                    //
                    // Try to set the buffer size. The kernel will silently adjust
                    // the size to an acceptable value. Then read the size back to
                    // get the size that was actually set.
                    //
                    int sizeSet;
                    if (i == 0)
                    {
                        Network.SetRecvBufferSize(_fd, sizeRequested);
                        _rcvSize = Network.GetRecvBufferSize(_fd);
                        sizeSet  = _rcvSize;
                    }
                    else
                    {
                        Network.SetSendBufferSize(_fd, sizeRequested);
                        _sndSize = Network.GetSendBufferSize(_fd);
                        sizeSet  = _sndSize;
                    }

                    //
                    // Warn if the size that was set is less than the requested size
                    // and we have not already warned
                    //
                    if (sizeSet < sizeRequested)
                    {
                        Ice.BufSizeWarnInfo winfo = _instance.GetBufSizeWarn(Ice.UDPEndpointType.Value);
                        if ((isSnd && (!winfo.SndWarn || winfo.SndSize != sizeRequested)) ||
                            (!isSnd && (!winfo.RcvWarn || winfo.RcvSize != sizeRequested)))
                        {
                            _instance.Logger.Warning(
                                $"UDP {direction} buffer size: requested size of {sizeRequested} adjusted to {sizeSet}");

                            if (isSnd)
                            {
                                _instance.SetSndBufSizeWarn(Ice.UDPEndpointType.Value, sizeRequested);
                            }
                            else
                            {
                                _instance.SetRcvBufSizeWarn(Ice.UDPEndpointType.Value, sizeRequested);
                            }
                        }
                    }
                }
            }
        }