示例#1
0
 public void async_connect(string ip, int port, functional.func <socket_result> cb)
 {
     try
     {
         _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
         _socket.BeginConnect(IPAddress.Parse(ip), port, delegate(IAsyncResult ar)
         {
             socket_result res = new socket_result();
             try
             {
                 _socket.EndConnect(ar);
                 _socket.NoDelay = true;
                 res.ok          = true;
             }
             catch (System.Exception ec)
             {
                 res.message = ec.Message;
             }
             cb(res);
         }, null);
     }
     catch (System.Exception)
     {
         close();
         cb(new socket_result());
     }
 }
示例#2
0
 void _async_write(int currTotal, ArraySegment <byte> buff, functional.func <socket_result> cb)
 {
     async_write_same(buff, delegate(socket_result tempRes)
     {
         if (tempRes.ok)
         {
             currTotal += tempRes.s;
             if (buff.Count == currTotal)
             {
                 socket_result result = new socket_result();
                 result.s             = buff.Count;
                 result.ok            = true;
                 cb(result);
             }
             else
             {
                 _async_write(currTotal, new ArraySegment <byte>(buff.Array, buff.Offset + tempRes.s, buff.Count - tempRes.s), cb);
             }
         }
         else
         {
             socket_result res = new socket_result();
             res.s             = currTotal;
             res.message       = tempRes.message;
             cb(res);
         }
     });
 }
示例#3
0
 public void async_accept(socket_tcp sck, functional.func <socket_result> cb)
 {
     try
     {
         _socket.BeginAccept(delegate(IAsyncResult ar)
         {
             socket_result res = new socket_result();
             try
             {
                 sck._socket         = _socket.EndAccept(ar);
                 sck._socket.NoDelay = true;
                 res.ok = true;
             }
             catch (System.Exception ec)
             {
                 res.message = ec.Message;
             }
             cb(res);
         }, null);
     }
     catch (System.Exception)
     {
         close();
         cb(new socket_result());
     }
 }
示例#4
0
        public long run()
        {
            long count = 0;

            while (_runSign)
            {
                Monitor.Enter(this);
                if (0 != _opQueue.Count)
                {
                    functional.func handler = _opQueue.First();
                    _opQueue.RemoveFirst();
                    Monitor.Exit(this);
                    count++;
                    handler();
                }
                else if (0 != _work)
                {
                    _waiting++;
                    Monitor.Wait(this);
                }
                else
                {
                    break;
                }
            }
            return(count);
        }
示例#5
0
 public override bool distribute(functional.func action)
 {
     if (running_in_this_thread())
     {
         functional.catch_invoke(action);
         return(true);
     }
     else if (_checkRequired && !_ctrl.InvokeRequired)
     {
         _mutex.WaitOne();
         if (_locked)
         {
             _waitQueue.AddLast(action);
             _mutex.ReleaseMutex();
             return(false);
         }
         else
         {
             _locked = true;
             _readyQueue.AddLast(action);
             _mutex.ReleaseMutex();
             return(run_a_round1());
         }
     }
     else
     {
         post(action);
         return(false);
     }
 }
示例#6
0
        public void async_read_same(ArraySegment <byte> buff, functional.func <socket_result> cb)
        {
            IList <ArraySegment <byte> > buffs = new List <ArraySegment <byte> >();

            buffs.Add(buff);
            async_read_same(buffs, cb);
        }
示例#7
0
 public void wait(long id, mutex_base mutex, functional.func ntf)
 {
     _strand.distribute(delegate()
     {
         _waitQueue.AddLast(delegate()
         {
             mutex.Lock(id, ntf);
         });
     });
 }
示例#8
0
 public virtual bool distribute(functional.func action)
 {
     if (running_in_this_thread())
     {
         functional.catch_invoke(action);
         return(true);
     }
     post(action);
     return(false);
 }
示例#9
0
 public void push_option(functional.func handler)
 {
     Monitor.Enter(this);
     _opQueue.AddLast(handler);
     if (0 != _waiting)
     {
         _waiting--;
         Monitor.Pulse(this);
     }
     Monitor.Exit(this);
 }
示例#10
0
        public void timeout(int ms, functional.func handler)
        {
#if DEBUG
            Trace.Assert(_strand.running_in_this_thread() && null == _timer && null != handler);
#endif
            _isInterval  = false;
            _handler     = handler;
            _lastTimeout = ms;
            _timerCount++;
            _beginTick = system_tick.get_tick_ms();
            begin_timer(_beginTick, ms, 0);
        }
示例#11
0
 public void notify_one()
 {
     _strand.distribute(delegate()
     {
         if (_waitQueue.Count > 0)
         {
             functional.func ntf = _waitQueue.First.Value;
             _waitQueue.RemoveFirst();
             ntf();
         }
     });
 }
示例#12
0
 public bool run_one()
 {
     Monitor.Enter(this);
     if (_runSign && 0 != _opQueue.Count)
     {
         functional.func handler = _opQueue.First();
         _opQueue.RemoveFirst();
         Monitor.Exit(this);
         handler();
         return(true);
     }
     Monitor.Exit(this);
     return(false);
 }
示例#13
0
 public void try_lock_shared(long id, functional.func <chan_async_state> ntf)
 {
     self_strand().distribute(delegate()
     {
         if (0 != _sharedMap.Count || 0 == _upgradeMutex._lockID)
         {
             find_map(id)._count++;
             ntf(chan_async_state.async_ok);
         }
         else
         {
             ntf(chan_async_state.async_fail);
         }
     });
 }
示例#14
0
 public long cancel()
 {
     if (null != _timer)
     {
         _timerCount++;
         long lastBegin = _beginTick;
         _beginTick = 0;
         _timer.Change(0, 0);
         _timer   = null;
         _handler = null;
         _strand.release_work();
         return(lastBegin);
     }
     return(0);
 }
示例#15
0
 public void lock_pess_shared(long id, functional.func ntf)
 {
     self_strand().distribute(delegate()
     {
         if (0 == _waitQueue.Count && (0 != _sharedMap.Count || 0 == _upgradeMutex._lockID))
         {
             find_map(id)._count++;
             ntf();
         }
         else
         {
             _waitQueue.AddLast(new wait_node((chan_async_state) => ntf(), id, lock_status.st_shared));
         }
     });
 }
示例#16
0
 public void unlock_shared(long id, functional.func ntf)
 {
     self_strand().distribute(delegate()
     {
         if (0 == --find_map(id)._count)
         {
             _sharedMap.Remove(id);
             if (0 == _sharedMap.Count && 0 != _waitQueue.Count)
             {
                 LinkedList <functional.func <chan_async_state> > ntfs = new LinkedList <functional.func <chan_async_state> >();
                 wait_node queueFront = _waitQueue.First.Value;
                 _waitQueue.RemoveFirst();
                 ntfs.AddLast(queueFront._ntf);
                 if (lock_status.st_shared == queueFront._status)
                 {
                     _upgradeMutex._lockID = 0;
                     find_map(queueFront._waitHostID)._count++;
                     for (LinkedListNode <wait_node> it = _waitQueue.First; null != it;)
                     {
                         if (lock_status.st_shared == it.Value._status)
                         {
                             find_map(it.Value._waitHostID)._count++;
                             ntfs.AddLast(it.Value._ntf);
                             LinkedListNode <wait_node> oit = it;
                             it = it.Next;
                             _waitQueue.Remove(oit);
                         }
                         else
                         {
                             it = it.Next;
                         }
                     }
                 }
                 else
                 {
                     _upgradeMutex._lockID = queueFront._waitHostID;
                     _upgradeMutex._recCount++;
                 }
                 while (0 != ntfs.Count)
                 {
                     ntfs.First.Value(chan_async_state.async_ok);
                     ntfs.RemoveFirst();
                 }
             }
         }
         ntf();
     });
 }
示例#17
0
 public override void try_lock(long id, functional.func <chan_async_state> ntf)
 {
     self_strand().distribute(delegate()
     {
         if (0 == _sharedMap.Count && (0 == _upgradeMutex._lockID || id == _upgradeMutex._lockID))
         {
             _upgradeMutex._lockID = id;
             _upgradeMutex._recCount++;
             ntf(chan_async_state.async_ok);
         }
         else
         {
             ntf(chan_async_state.async_fail);
         }
     });
 }
示例#18
0
 public override void Lock(long id, functional.func ntf)
 {
     self_strand().distribute(delegate()
     {
         if (0 == _sharedMap.Count && (0 == _upgradeMutex._lockID || id == _upgradeMutex._lockID))
         {
             _upgradeMutex._lockID = id;
             _upgradeMutex._recCount++;
             ntf();
         }
         else
         {
             _waitQueue.AddLast(new wait_node((chan_async_state) => ntf(), id, lock_status.st_unique));
         }
     });
 }
示例#19
0
 public override void try_lock(long id, functional.func <chan_async_state> ntf)
 {
     _strand.distribute(delegate()
     {
         if (0 == _lockID || id == _lockID)
         {
             _lockID = id;
             _recCount++;
             ntf(chan_async_state.async_ok);
         }
         else
         {
             ntf(chan_async_state.async_fail);
         }
     });
 }
示例#20
0
 public override void Lock(long id, functional.func ntf)
 {
     _strand.distribute(delegate()
     {
         if (0 == _lockID || id == _lockID)
         {
             _lockID = id;
             _recCount++;
             ntf();
         }
         else
         {
             _waitQueue.AddLast(new wait_node((chan_async_state) => ntf(), id));
         }
     });
 }
示例#21
0
 public void post(functional.func action)
 {
     _mutex.WaitOne();
     if (_locked)
     {
         _waitQueue.AddLast(action);
         _mutex.ReleaseMutex();
     }
     else
     {
         _locked = true;
         _readyQueue.AddLast(action);
         _mutex.ReleaseMutex();
         run_task();
     }
 }
示例#22
0
        public void deadline(long ms, functional.func handler)
        {
#if DEBUG
            Trace.Assert(_strand.running_in_this_thread() && null == _timer && null != handler);
#endif
            _isInterval = false;
            _handler    = handler;
            _timerCount++;
            _beginTick = system_tick.get_tick_ms();
            if (ms > _beginTick)
            {
                _lastTimeout = (int)(ms - _beginTick);
                begin_timer(_beginTick, _lastTimeout, 0);
            }
            else
            {
                _lastTimeout = 0;
                begin_timer(_beginTick, 0, 0);
            }
        }
示例#23
0
 public void async_read_same(IList <ArraySegment <byte> > buffs, functional.func <socket_result> cb)
 {
     try
     {
         _socket.BeginReceive(buffs, 0, delegate(IAsyncResult ar)
         {
             socket_result res = new socket_result();
             try
             {
                 res.s  = _socket.EndReceive(ar);
                 res.ok = true;
             }
             catch (System.Exception ec)
             {
                 res.message = ec.Message;
             }
             cb(res);
         }, null);
     }
     catch (System.Exception) { cb(new socket_result()); }
 }
示例#24
0
 private void begin_timer(long tick, long dueTime, int period)
 {
     if (null == _timer)
     {
         _strand.hold_work();
     }
     _timer = new Timer(delegate(object state)
     {
         _strand.post(delegate()
         {
             if ((int)state == _timerCount)
             {
                 _beginTick = 0;
                 _onTopCall = true;
                 if (_isInterval)
                 {
                     functional.func handler = _handler;
                     handler();
                     if ((int)state == _timerCount)
                     {
                         tick   += period;
                         dueTime = tick - system_tick.get_tick_ms();
                         begin_timer(tick, dueTime > 0 ? dueTime : 0, period);
                     }
                 }
                 else
                 {
                     functional.func handler = _handler;
                     _handler = null;
                     _timer   = null;
                     _strand.release_work();
                     handler();
                 }
                 _onTopCall = false;
             }
         });
     }, _timerCount, dueTime, 0);
 }
示例#25
0
        public bool advance()
        {
#if DEBUG
            Trace.Assert(_strand.running_in_this_thread());
#endif
            if (null != _timer)
            {
                if (!_isInterval)
                {
                    functional.func handler = _handler;
                    cancel();
                    handler();
                    return(true);
                }
                else if (!_onTopCall)
                {
                    functional.func handler = _handler;
                    handler();
                    return(true);
                }
            }
            return(false);
        }
示例#26
0
 public void unlock_shared_and_lock(long id, functional.func ntf)
 {
     unlock_shared(id, () => Lock(id, ntf));
 }
示例#27
0
 public void unlock_and_lock_upgrade(long id, functional.func ntf)
 {
     unlock_and_lock_shared(id, () => lock_upgrade(id, ntf));
 }
示例#28
0
 public void unlock_upgrade(long id, functional.func ntf)
 {
     _upgradeMutex.unlock(id, ntf);
 }
示例#29
0
 public void try_lock_upgrade(long id, functional.func <chan_async_state> ntf)
 {
     _upgradeMutex.try_lock(id, ntf);
 }
示例#30
0
 public wait_node(functional.func <chan_async_state> ntf, long id)
 {
     _ntf = ntf;
     _id  = id;
 }