Example #1
0
 public UdpEndpointI(BasicStream s)
 {
     instance_ = s.instance();
     s.startReadEncaps();
     _host = s.readString();
     _port = s.readInt();
     _protocolMajor = s.readByte();
     _protocolMinor = s.readByte();
     _encodingMajor = s.readByte();
     _encodingMinor = s.readByte();
     if(_protocolMajor != Protocol.protocolMajor)
     {
         Ice.UnsupportedProtocolException e = new Ice.UnsupportedProtocolException();
         e.badMajor = _protocolMajor < 0?_protocolMajor + 255:_protocolMajor;
         e.badMinor = _protocolMinor < 0?_protocolMinor + 255:_protocolMinor;
         e.major = Protocol.protocolMajor;
         e.minor = Protocol.protocolMinor;
         throw e;
     }
     if(_encodingMajor != Protocol.encodingMajor)
     {
         Ice.UnsupportedEncodingException e = new Ice.UnsupportedEncodingException();
         e.badMajor = _encodingMajor < 0?_encodingMajor + 255:_encodingMajor;
         e.badMinor = _encodingMinor < 0?_encodingMinor + 255:_encodingMinor;
         e.major = Protocol.encodingMajor;
         e.minor = Protocol.encodingMinor;
         throw e;
     }
     // Not transmitted.
     //_connect = s.readBool();
     _connect = false;
     _compress = s.readBool();
     s.endReadEncaps();
     calcHashValue();
 }
Example #2
0
        public virtual void startReadEncaps()
        {
            {
                ReadEncaps curr = _readEncapsCache;
                if(curr != null)
                {
                    curr.reset();
                    _readEncapsCache = _readEncapsCache.next;
                }
                else
                {
                    curr = new ReadEncaps();
                }
                curr.next = _readEncapsStack;
                _readEncapsStack = curr;
            }

            _readEncapsStack.start = _buf.b.position();

            //
            // I don't use readSize() and writeSize() for
            // encapsulations, because when creating an encapsulation,
            // I must know in advance how many bytes the size
            // information will require in the data stream. If I use
            // an Int, it is always 4 bytes. For
            // readSize()/writeSize(), it could be 1 or 5 bytes.
            //
            int sz = readInt();
            if(sz < 6)
            {
                throw new Ice.UnmarshalOutOfBoundsException();
            }

            if(sz - 4 > _buf.b.remaining())
            {
                throw new Ice.UnmarshalOutOfBoundsException();
            }
            _readEncapsStack.sz = sz;

            byte eMajor = readByte();
            byte eMinor = readByte();
            if(eMajor != Protocol.encodingMajor || eMinor > Protocol.encodingMinor)
            {
                Ice.UnsupportedEncodingException e = new Ice.UnsupportedEncodingException();
                e.badMajor = eMajor < 0 ? eMajor + 256 : eMajor;
                e.badMinor = eMinor < 0 ? eMinor + 256 : eMinor;
                e.major = Protocol.encodingMajor;
                e.minor = Protocol.encodingMinor;
                throw e;
            }
            // _readEncapsStack.encodingMajor = eMajor; // Currently unused
            // _readEncapsStack.encodingMinor = eMinor; // Currently unused
        }
Example #3
0
        public UdpEndpointI(Instance instance, string str, bool oaEndpoint)
        {
            instance_ = instance;
            _host = null;
            _port = 0;
            _protocolMajor = Protocol.protocolMajor;
            _protocolMinor = Protocol.protocolMinor;
            _encodingMajor = Protocol.encodingMajor;
            _encodingMinor = Protocol.encodingMinor;
            _connect = false;
            _compress = false;

            string delim = " \t\n\r";

            int beg;
            int end = 0;

            while(true)
            {
                beg = IceUtilInternal.StringUtil.findFirstNotOf(str, delim, end);
                if(beg == -1)
                {
                    break;
                }

                end = IceUtilInternal.StringUtil.findFirstOf(str, delim, beg);
                if(end == -1)
                {
                    end = str.Length;
                }

                string option = str.Substring(beg, end - beg);
                if(option[0] != '-')
                {
                    Ice.EndpointParseException e = new Ice.EndpointParseException();
                    e.str = "expected an endpoint option but found `" + option + "' in endpoint `udp " + str + "'";
                    throw e;
                }

                string argument = null;
                int argumentBeg = IceUtilInternal.StringUtil.findFirstNotOf(str, delim, end);
                if(argumentBeg != -1 && str[argumentBeg] != '-')
                {
                    beg = argumentBeg;
                    if(str[beg] == '\"')
                    {
                        end = IceUtilInternal.StringUtil.findFirstOf(str, "\"", beg + 1);
                        if(end == -1)
                        {
                            Ice.EndpointParseException e = new Ice.EndpointParseException();
                            e.str = "mismatched quotes around `" + argument + "' in endpoint `udp " + str + "'";
                            throw e;
                        }
                        else
                        {
                            ++end;
                        }
                    }
                    else
                    {
                        end = IceUtilInternal.StringUtil.findFirstOf(str, delim, beg);
                        if(end == -1)
                        {
                            end = str.Length;
                        }
                    }
                    argument = str.Substring(beg, end - beg);
                    if(argument[0] == '\"' && argument[argument.Length - 1] == '\"')
                    {
                        argument = argument.Substring(1, argument.Length - 2);
                    }
                }

                if(option.Equals("-v"))
                {
                    if(argument == null)
                    {
                        Ice.EndpointParseException e = new Ice.EndpointParseException();
                        e.str = "no argument provided for -v option in endpoint `udp " + str + "'";
                        throw e;
                    }

                    int pos = argument.IndexOf((System.Char) '.');
                    if(pos == -1)
                    {
                        Ice.EndpointParseException e = new Ice.EndpointParseException();
                        e.str = "malformed protocol version `" + argument + "' in endpoint `udp " + str + "'";
                        throw e;
                    }

                    string majStr = argument.Substring(0, (pos) - (0));
                    string minStr = argument.Substring(pos + 1, (argument.Length) - (pos + 1));
                    int majVersion;
                    int minVersion;
                    try
                    {
                        majVersion = System.Int32.Parse(majStr, CultureInfo.InvariantCulture);
                        minVersion = System.Int32.Parse(minStr, CultureInfo.InvariantCulture);
                    }
                    catch(System.FormatException ex)
                    {
                        Ice.EndpointParseException e = new Ice.EndpointParseException(ex);
                        e.str = "invalid protocol version `" + argument + "' in endpoint `udp " + str + "'";
                        throw e;
                    }

                    if(majVersion < 1 || majVersion > 255 || minVersion < 0 || minVersion > 255)
                    {
                        Ice.EndpointParseException e = new Ice.EndpointParseException();
                        e.str = "range error in protocol version `" + argument + "' in endpoint `udp " + str + "'";
                        throw e;
                    }

                    if(majVersion != Protocol.protocolMajor)
                    {
                        Ice.UnsupportedProtocolException e = new Ice.UnsupportedProtocolException();
                        e.badMajor = majVersion < 0?majVersion + 255:majVersion;
                        e.badMinor = minVersion < 0?minVersion + 255:minVersion;
                        e.major = Protocol.protocolMajor;
                        e.minor = Protocol.protocolMinor;
                        throw e;
                    }

                    _protocolMajor = (byte)majVersion;
                    _protocolMinor = (byte)minVersion;
                }
                else if(option.Equals("-e"))
                {
                    if(argument == null)
                    {
                        Ice.EndpointParseException e = new Ice.EndpointParseException();
                        e.str = "no argument provided for -e option in endpoint `udp " + str + "'";
                        throw e;
                    }

                    int pos = argument.IndexOf((System.Char) '.');
                    if(pos == -1)
                    {
                        Ice.EndpointParseException e = new Ice.EndpointParseException();
                        e.str = "malformed encoding version `" + argument + "' in endpoint `udp " + str + "'";
                        throw e;
                    }

                    string majStr = argument.Substring(0, (pos) - (0));
                    string minStr = argument.Substring(pos + 1, (argument.Length) - (pos + 1));
                    int majVersion;
                    int minVersion;
                    try
                    {
                        majVersion = System.Int32.Parse(majStr, CultureInfo.InvariantCulture);
                        minVersion = System.Int32.Parse(minStr, CultureInfo.InvariantCulture);
                    }
                    catch(System.FormatException ex)
                    {
                        Ice.EndpointParseException e = new Ice.EndpointParseException(ex);
                        e.str = "invalid encoding version `" + argument + "' in endpoint `udp " + str + "'";
                        throw e;
                    }

                    if(majVersion < 1 || majVersion > 255 || minVersion < 0 || minVersion > 255)
                    {
                        Ice.EndpointParseException e = new Ice.EndpointParseException();
                        e.str = "range error in encoding version `" + argument + "' in endpoint `udp " + str + "'";
                        throw e;
                    }

                    if(majVersion != Protocol.encodingMajor)
                    {
                        Ice.UnsupportedEncodingException e = new Ice.UnsupportedEncodingException();
                        e.badMajor = majVersion < 0?majVersion + 255:majVersion;
                        e.badMinor = minVersion < 0?minVersion + 255:minVersion;
                        e.major = Protocol.encodingMajor;
                        e.minor = Protocol.encodingMinor;
                        throw e;
                    }

                    _encodingMajor = (byte)majVersion;
                    _encodingMinor = (byte)minVersion;
                }
                else if(option.Equals("-h"))
                {
                    if(argument == null)
                    {
                        Ice.EndpointParseException e = new Ice.EndpointParseException();
                        e.str = "no argument provided for -h option in endpoint `udp " + str + "'";
                        throw e;
                    }

                    _host = argument;
                }
                else if(option.Equals("-p"))
                {
                    if(argument == null)
                    {
                        Ice.EndpointParseException e = new Ice.EndpointParseException();
                        e.str = "no argument provided for -p option in endpoint `udp " + str + "'";
                        throw e;
                    }

                    try
                    {
                        _port = System.Int32.Parse(argument, CultureInfo.InvariantCulture);
                    }
                    catch(System.FormatException ex)
                    {
                        Ice.EndpointParseException e = new Ice.EndpointParseException(ex);
                        e.str = "invalid port value `" + argument + "' in endpoint `udp " + str + "'";
                        throw e;
                    }

                    if(_port < 0 || _port > 65535)
                    {
                        Ice.EndpointParseException e = new Ice.EndpointParseException();
                        e.str = "port value `" + argument + "' out of range in endpoint `udp " + str + "'";
                        throw e;
                    }
                }
                else if(option.Equals("-c"))
                {
                    if(argument != null)
                    {
                        Ice.EndpointParseException e = new Ice.EndpointParseException();
                        e.str = "unexpected argument `" + argument + "' provided for -c option in `udp " + str + "'";
                        throw e;
                    }

                    _connect = true;
                }
                else if(option.Equals("-z"))
                {
                    if(argument != null)
                    {
                        Ice.EndpointParseException e = new Ice.EndpointParseException();
                        e.str = "unexpected argument `" + argument + "' provided for -z option in `udp " + str + "'";
                        throw e;
                    }

                    _compress = true;
                }
                else if(option.Equals("--interface"))
                {
                    if(argument == null)
                    {
                        Ice.EndpointParseException e = new Ice.EndpointParseException();
                        e.str = "no argument provided for --interface option in endpoint `udp " + str + "'";
                        throw e;
                    }

                    _mcastInterface = argument;
                }
                else if(option.Equals("--ttl"))
                {
                    if(argument == null)
                    {
                        Ice.EndpointParseException e = new Ice.EndpointParseException();
                        e.str = "no argument provided for --ttl option in endpoint `udp " + str + "'";
                        throw e;
                    }

                    try
                    {
                        _mcastTtl = System.Int32.Parse(argument, CultureInfo.InvariantCulture);
                    }
                    catch(System.FormatException ex)
                    {
                        Ice.EndpointParseException e = new Ice.EndpointParseException(ex);
                        e.str = "invalid TTL value `" + argument + "' in endpoint `udp " + str + "'";
                        throw e;
                    }

                    if(_mcastTtl < 0)
                    {
                        Ice.EndpointParseException e = new Ice.EndpointParseException();
                        e.str = "TTL value `" + argument + "' out of range in endpoint `udp " + str + "'";
                        throw e;
                    }
                }
                else
                {
                    Ice.EndpointParseException e = new Ice.EndpointParseException();
                    e.str = "unknown option `" + option + "' in `udp " + str + "'";
                    throw e;
                }
            }

            if(_host == null)
            {
                _host = instance_.defaultsAndOverrides().defaultHost;
            }
            else if(_host.Equals("*"))
            {
                if(oaEndpoint)
                {
                    _host = null;
                }
                else
                {
                    throw new Ice.EndpointParseException("`-h *' not valid for proxy endpoint `udp " + str + "'");
                }
            }

            if(_host == null)
            {
                _host = "";
            }

            calcHashValue();
        }
Example #4
0
        public override void message(ref IceInternal.ThreadPoolCurrent current)
        {
            StartCallback startCB = null;
            Queue<OutgoingMessage> sentCBs = null;
            MessageInfo info = new MessageInfo();

            IceInternal.ThreadPoolMessage msg = new IceInternal.ThreadPoolMessage(_m);
            _m.Lock();
            try
            {
                if(!msg.startIOScope(ref current))
                {
                    return;
                }

                if(_state >= StateClosed)
                {
                    return;
                }

                try
                {
                    unscheduleTimeout(current.operation);
                    if((current.operation & IceInternal.SocketOperation.Write) != 0 && !_writeStream.isEmpty())
                    {
                        if(_writeStream.getBuffer().b.hasRemaining() && !_transceiver.write(_writeStream.getBuffer()))
                        {
                            Debug.Assert(!_writeStream.isEmpty());
                            scheduleTimeout(IceInternal.SocketOperation.Write, _endpoint.timeout());
                            return;
                        }
                        Debug.Assert(!_writeStream.getBuffer().b.hasRemaining());
                    }
                    if((current.operation & IceInternal.SocketOperation.Read) != 0 && !_readStream.isEmpty())
                    {
                        if(_readHeader) // Read header if necessary.
                        {
                            if(_readStream.getBuffer().b.hasRemaining() && !_transceiver.read(_readStream.getBuffer()))
                            {
                                return;
                            }
                            Debug.Assert(!_readStream.getBuffer().b.hasRemaining());
                            _readHeader = true;

                            int pos = _readStream.pos();
                            if(pos < IceInternal.Protocol.headerSize)
                            {
                                //
                                // This situation is possible for small UDP packets.
                                //
                                throw new Ice.IllegalMessageSizeException();
                            }

                            _readStream.pos(0);
                            byte[] m = new byte[4];
                            m[0] = _readStream.readByte();
                            m[1] = _readStream.readByte();
                            m[2] = _readStream.readByte();
                            m[3] = _readStream.readByte();
                            if(m[0] != IceInternal.Protocol.magic[0] || m[1] != IceInternal.Protocol.magic[1] ||
                               m[2] != IceInternal.Protocol.magic[2] || m[3] != IceInternal.Protocol.magic[3])
                            {
                                Ice.BadMagicException ex = new Ice.BadMagicException();
                                ex.badMagic = m;
                                throw ex;
                            }

                            byte pMajor = _readStream.readByte();
                            byte pMinor = _readStream.readByte();
                            if(pMajor != IceInternal.Protocol.protocolMajor ||
                               pMinor > IceInternal.Protocol.protocolMinor)
                            {
                                Ice.UnsupportedProtocolException e = new Ice.UnsupportedProtocolException();
                                e.badMajor = pMajor < 0 ? pMajor + 255 : pMajor;
                                e.badMinor = pMinor < 0 ? pMinor + 255 : pMinor;
                                e.major = IceInternal.Protocol.protocolMajor;
                                e.minor = IceInternal.Protocol.protocolMinor;
                                throw e;
                            }

                            byte eMajor = _readStream.readByte();
                            byte eMinor = _readStream.readByte();
                            if(eMajor != IceInternal.Protocol.encodingMajor ||
                               eMinor > IceInternal.Protocol.encodingMinor)
                            {
                                Ice.UnsupportedEncodingException e = new Ice.UnsupportedEncodingException();
                                e.badMajor = eMajor < 0 ? eMajor + 255 : eMajor;
                                e.badMinor = eMinor < 0 ? eMinor + 255 : eMinor;
                                e.major = IceInternal.Protocol.encodingMajor;
                                e.minor = IceInternal.Protocol.encodingMinor;
                                throw e;
                            }

                            _readStream.readByte(); // messageType
                            _readStream.readByte(); // compress
                            int size = _readStream.readInt();
                            if(size < IceInternal.Protocol.headerSize)
                            {
                                throw new Ice.IllegalMessageSizeException();
                            }
                            if(size > _instance.messageSizeMax())
                            {
                                IceInternal.Ex.throwMemoryLimitException(size, _instance.messageSizeMax());
                            }
                            if(size > _readStream.size())
                            {
                                _readStream.resize(size, true);
                            }
                            _readStream.pos(pos);
                        }

                        if(_readStream.pos() != _readStream.size())
                        {
                            if(_endpoint.datagram())
                            {
                                throw new Ice.DatagramLimitException(); // The message was truncated.
                            }
                            else
                            {
                                if(_readStream.getBuffer().b.hasRemaining() &&
                                   !_transceiver.read(_readStream.getBuffer()))
                                {
                                    Debug.Assert(!_readStream.isEmpty());
                                    scheduleTimeout(IceInternal.SocketOperation.Read, _endpoint.timeout());
                                    return;
                                }
                                Debug.Assert(!_readStream.getBuffer().b.hasRemaining());
                            }
                        }
                    }

                    if(_state <= StateNotValidated)
                    {
                        if(_state == StateNotInitialized && !initialize(current.operation))
                        {
                            return;
                        }

                        if(_state <= StateNotValidated && !validate(current.operation))
                        {
                            return;
                        }

                        _threadPool.unregister(this, current.operation);

                        //
                        // We start out in holding state.
                        //
                        setState(StateHolding);
                        startCB = _startCallback;
                        _startCallback = null;
                    }
                    else
                    {
                        Debug.Assert(_state <= StateClosing);

                        if((current.operation & IceInternal.SocketOperation.Write) != 0)
                        {
                            sentCBs = sendNextMessage();
                        }

                        if((current.operation & IceInternal.SocketOperation.Read) != 0)
                        {
                            parseMessage(new IceInternal.BasicStream(_instance), ref info);
                        }
                    }

                    //
                    // We increment the dispatch count to prevent the
                    // communicator destruction during the callback.
                    //
                    if(sentCBs != null || info.outAsync != null)
                    {
                        ++_dispatchCount;
                    }

                    if(_acmTimeout > 0)
                    {
                        _acmAbsoluteTimeoutMillis = IceInternal.Time.currentMonotonicTimeMillis() + _acmTimeout * 1000;
                    }

                    if(startCB == null && sentCBs == null && info.invokeNum == 0 && info.outAsync == null)
                    {
                        return; // Nothing to dispatch.
                    }

                    msg.completed(ref current);
                }
                catch(DatagramLimitException) // Expected.
                {
                    if(_warnUdp)
                    {
                        _logger.warning("maximum datagram size of " + _readStream.pos() + " exceeded");
                    }
                    _readStream.resize(IceInternal.Protocol.headerSize, true);
                    _readStream.pos(0);
                    _readHeader = true;
                    return;
                }
                catch(SocketException ex)
                {
                    setState(StateClosed, ex);
                    return;
                }
                catch(LocalException ex)
                {
                    if(_endpoint.datagram())
                    {
                        if(_warn)
                        {
                            String s = "datagram connection exception:\n" + ex + '\n' + _desc;
                            _logger.warning(s);
                        }
                        _readStream.resize(IceInternal.Protocol.headerSize, true);
                        _readStream.pos(0);
                        _readHeader = true;
                    }
                    else
                    {
                        setState(StateClosed, ex);
                    }
                    return;
                }
                finally
                {
                    msg.finishIOScope(ref current);
                }

                //
                // Unlike C++/Java, this method is called from an IO thread of the .NET thread
                // pool or from the communicator async IO thread. While it's fine to handle the
                // non-blocking activity of the connection from these threads, the dispatching
                // of the message must be taken care of by the Ice thread pool.
                //
                IceInternal.ThreadPoolCurrent c = current;
                _threadPool.execute(
                    delegate()
                    {
                        if(_dispatcher != null)
                        {
                            try
                            {
                                _dispatcher(delegate()
                                            {
                                                dispatch(startCB, sentCBs, info);
                                            },
                                            this);
                            }
                            catch(System.Exception ex)
                            {
                                if(_instance.initializationData().properties.getPropertyAsIntWithDefault(
                                       "Ice.Warn.Dispatch", 1) > 1)
                                {
                                    warning("dispatch exception", ex);
                                }
                            }
                        }
                        else
                        {
                            dispatch(startCB, sentCBs, info);
                        }
                        msg.destroy(ref c);
                    });
            }
            finally
            {
                _m.Unlock();
            }
        }