Example #1
0
 public void update(byte[] data, int off, int len)
 {
     if (check && Interlocked.Add(ref sendsize, len) > cfgsize)
     {
         if (Trace.isWarnEnabled())
         {
             Trace.warn(t.getManager() + " " + t + " send buffer is full! sendbuffersize " + sendsize + " " + cfgsize);
         }
         t.close();
     }
     else
     {
         try
         {
             byte[] tmp = new byte[len];
             Buffer.BlockCopy(data, off, tmp, 0, len);
             t.ns.BeginWrite(tmp, 0, len, ar =>
             {
                 if (check)
                 {
                     Interlocked.Add(ref sendsize, -len);
                 }
                 try { t.ns.EndWrite(ar); }
                 catch (Exception e) { t.close(e); }
             }, null);
         }
         catch (Exception e) { t.close(e); }
     }
 }
Example #2
0
 internal override void removeProtocolTransport(StateTransportImpl transport)
 {
     lock (locker)
     {
         if (state != State.EXCHANGE)
         {
             return;
         }
         if (this.transport != null)
         {
             transport.close();
             this.transport = null;
         }
         Exception t = dispatcher.run(() => listener.onTransportRemoved(transport));
         if (t == null)
         {
             if (!scheduleReconnect())
             {
                 _close();
             }
         }
         else
         {
             if (Trace.isErrorEnabled())
             {
                 Trace.error(this + " removeProtocolTransport = " + transport, t);
             }
             _close();
         }
         Monitor.Pulse(locker);
     }
 }
Example #3
0
 internal override void addProtocolTransport(StateTransportImpl transport)
 {
     lock (locker)
     {
         if (state != State.CONNECTING)
         {
             transport.close();
             return;
         }
         Exception t = dispatcher.run(() => listener.onTransportAdded(transport));
         if (t == null)
         {
             this.transport = transport;
             transport.ready();
             state      = State.EXCHANGE;
             shrinktime = SHRINKTIME_MIN;
         }
         else
         {
             if (Trace.isErrorEnabled())
             {
                 Trace.error(this + " addProtocolTransport = " + transport, t);
             }
             transport.close();
             _close();
         }
     }
 }
Example #4
0
        private void doConnect()
        {
            StateTransportImpl transport = null;

            try
            {
                TcpClient c = new TcpClient();
                c.ReceiveBufferSize = config.getInputBufferSize();
                c.SendBufferSize    = config.getOutputBufferSize();
                transport           = new StateTransportImpl(this, () => c.Close());
                transport.setupAlarm(new Alarm(() => transport.close(new Exception("connect timeout"))));
                transport.resetAlarm(config.getConnectTimeout());
                lock (locker)
                {
                    c.BeginConnect(config.getPeerAddress().Address, config.getPeerAddress().Port, ar =>
                    {
                        try
                        {
                            c.EndConnect(ar);
                            transport.startup(c.GetStream(), (IPEndPoint)c.Client.LocalEndPoint, (IPEndPoint)c.Client.RemoteEndPoint);
                        }
                        catch (Exception e)
                        {
                            if (Trace.isErrorEnabled())
                            {
                                Trace.error("ClientManagerImpl.doConnect", e);
                            }
                            transport.close(e);
                        }
                    }, null);
                    state = State.CONNECTING;
                }
            }
            catch (Exception t)
            {
                if (Trace.isErrorEnabled())
                {
                    Trace.error(this + " doConnect", t);
                }
                _close();
            }
        }
Example #5
0
        public override void close()
        {
            if (Engine.remove(this))
            {
                return;
            }
            lock (locker)
            {
                autoReconnect = false;
                switch (state)
                {
                case State.EXCHANGE:
                    StateTransportImpl _transport = transport;
                    transport = null;
                    _transport.close();
                    while (state != State.CLOSE)
                    {
                        Monitor.Wait(locker);
                    }
                    break;

                case State.CONNECTING:
                    Exception e = dispatcher.run(() => listener.onAbort(null));
                    if (e != null && Trace.isErrorEnabled())
                    {
                        Trace.error(this + " connectAbort", e);
                    }
                    break;

                case State.INIT:
                    if (future != null)
                    {
                        future.Dispose();
                    }
                    break;
                }
                dispatcher.await();
                dispatcher.run(() => listener.onManagerUninitialized(this));
            }
        }