protected void ReadFromStream() { if (_netStream.CanRead) { try { int length = _netStream.Read(_recBuf, _recBufOffset, _recBuf.Length - _recBufOffset); _recBufOffset += length; if (length == 0) { FLog.Debug("NetStream.Read length = 0"); if (_fOnReceiveFail != null) { _fOnReceiveFail(); } SetTerminateFlag(); } } catch (Exception ex) { FLog.Error("ReceiverThread Exception:" + ex.ToString()); if (_fOnReceiveFail != null) { _fOnReceiveFail(); } SetTerminateFlag(); } } else { Thread.Sleep(16); } }
/// <summary> /// 非线程安全,只允许在主线程调用 /// </summary> public void DetachListenerNow(IEventListener listener, uint eventKey) { if (listener == null) { FLog.Error("DetachListenerNow failed due to no listener or event key specified in EventDispacher."); return; } if (System.Threading.Thread.CurrentThread.ManagedThreadId != 1) { FLog.Debug("DetachListenerNow -> " + eventKey); } if (!_listenerHashTable.ContainsKey(eventKey)) { return; } ArrayList listenerList = _listenerHashTable[eventKey] as ArrayList; if (!listenerList.Contains(listener)) { return; } listenerList.Remove(listener); }
/// <summary> /// 非线程安全,只允许在主线程调用 /// </summary> public void AttachListenerNow(IEventListener listener, uint eventKey) { if (listener == null) { FLog.Error("AttachListenerNow failed due to no listener or event key specified in EventDispacher."); return; } if (System.Threading.Thread.CurrentThread.ManagedThreadId != 1) { FLog.Debug("AttachListenerNow -> " + eventKey); } if (!_listenerHashTable.ContainsKey(eventKey)) { _listenerHashTable.Add(eventKey, new ArrayList()); } ArrayList listenerList = _listenerHashTable[eventKey] as ArrayList; if (listenerList.Contains(listener)) { FLog.Error("AttachListenerNow -> " + listener.GetType().ToString() + " is already in list for event: " + eventKey.ToString() + " in EventDispacher."); return; } AttachListner(eventKey, listenerList, listener); }
protected override void Main() { FLog.Debug("SenderThread.Main: Begin"); while (!IsTerminateFlagSet()) { bool sleep = false; byte[] _buffer = null; lock (_msgToSend) { if (_msgToSend.Count > 0) { _buffer = _msgToSend[0]; } else { sleep = true; } } if (_buffer != null) { try { _netStream.Write(_buffer, 0, _buffer.Length); _netStream.Flush(); } catch (Exception ex) { FLog.Error(string.Format("SenderThead, Main Write Exception, {0}, {1}, {2}", ex.Message, ex.StackTrace, ex.InnerException.Message)); } lock (_msgToSend) { if (_msgToSend.Count > 0) { _msgToSend.RemoveAt(0); } } } if (sleep) { Thread.Sleep(15); } } FLog.Debug("SenderThread.Main: End"); }
public void AttachListener <T>(IEventListener listener, T eventId, bool bIsAttach = true) { int idx = 0; FEventDispatcher evtDispatcher = GetByEventTypeName(typeof(T).FullName, ref idx); if (evtDispatcher != null) { evtDispatcher.AttachListenerNow(listener, Convert.ToUInt32(eventId), bIsAttach); } else { FLog.Error("Error event type: " + typeof(T).FullName); } }
public void FireEvent <T>(T eventId, object param1 = null, object param2 = null) { int iEventType = 0; FEventDispatcher evtDispatcher = GetByEventTypeName(typeof(T).FullName, ref iEventType); if (evtDispatcher != null) { uint iEventId = Convert.ToUInt32(eventId); string eventName = GetEventNameById(iEventId, iEventType); if (!string.IsNullOrEmpty(eventName)) { FLog.Debug("FireSync -> " + eventName); } evtDispatcher.FireEvent(iEventId, param1, param2); } else { FLog.Error("Error event type: " + typeof(T).FullName); } }