private StateSet TransitState(StateSet st, TEvent e) { var state = new StateSet(); foreach (var s in st) { StateSet tmp; if (_table.TryGetValue(new KeyValuePair <State, TEvent>(s, e), out tmp)) { state.UnionWith(tmp); } } var epsilonState = new StateSet(); foreach (var s in state) { StateSet tmp; if (_epsClosures.TryGetValue(s, out tmp)) { epsilonState.UnionWith(tmp); } epsilonState.Add(s); } return(epsilonState); }
//stream异步结束写入 private void EndWrite(IAsyncResult ar) { SocketHandlerState state = (SocketHandlerState)ar.AsyncState; //锁定StateSet lock (StateSet) StateSet.Add(state.AsyncResult, state); try { state.Stream.EndWrite(ar); } catch { //出现Socket异常,发送失败 state.Completed = false; //允许等待线程继续 ((AutoResetEvent)state.AsyncResult.AsyncWaitHandle).Set(); //执行异步回调函数 state.AsyncCallBack(state.AsyncResult); return; } //发送成功 state.Completed = true; //允许等待线程继续 ((AutoResetEvent)state.AsyncResult.AsyncWaitHandle).Set(); //执行异步回调函数 state.AsyncCallBack(state.AsyncResult); //锁定SendQueue lock (SendQueue) { SocketHandlerState Prepare = null; //移除当前发送完成的数据 SendQueue.Remove(state); //如果SendQueue还有数据存在,则继续发送 if (SendQueue.Count > 0) { Prepare = SendQueue[0]; } if (Prepare != null) { //获取数据长度 //ushort的最大值为65535 //转换为byte[]长度为2 var dataLength = BitConverter.GetBytes((ushort)Prepare.Data.Length); //向对方发送长度为2的头信息,表示接下来要发送的数据长度 Prepare.Stream.Write(dataLength, 0, dataLength.Length); //开始异步发送数据 Prepare.Stream.BeginWrite(Prepare.Data, 0, Prepare.Data.Length, EndWrite, Prepare).AsyncWaitHandle.WaitOne(); } } }
public virtual void AddTransition(State @from, State to, TEvent e) { if (!States.Contains(@from)) { throw new StateNotFoundException(); } if (States.Add(to)) { LastAdded = to; } Events.Add(e); }
private void SaveStateAsScene() { var lvms = Locator.Get.LightVMs; var scene = new StateSet("New Scene"); foreach (var lvm in lvms) { scene.Add(lvm.Light); } Scenes.Add(scene); SaveLibrary(); }
//stream异步结束读取 private void EndRead(IAsyncResult ar) { SocketHandlerState state = (SocketHandlerState)ar.AsyncState; int dataLength; try { dataLength = state.Stream.EndRead(ar); } catch { dataLength = 0; } //dataLength为0则表示Socket断开连接 if (dataLength == 0) { lock (StateSet) StateSet.Add(state.AsyncResult, state); //设定接收到的数据位空byte数组 state.Data = new byte[0]; //允许等待线程继续 ((AutoResetEvent)state.AsyncResult.AsyncWaitHandle).Set(); //执行异步回调函数 state.AsyncCallBack(state.AsyncResult); return; } //如果是已完成状态,则表示state.Data的数据是头信息 if (state.Completed) { //设定状态为未完成 state.Completed = false; //已接收得数据长度为0 state.DataLength = 0; //获取主要数据长度 var length = BitConverter.ToUInt16(state.Data, 0); //初始化数据的byte数组 state.Data = new byte[length]; try { //开始异步接收主要数据 state.Stream.BeginRead(state.Data, 0, length, EndRead, state); } catch { //出现Socket异常 lock (StateSet) StateSet.Add(state.AsyncResult, state); state.Data = new byte[0]; ((AutoResetEvent)state.AsyncResult.AsyncWaitHandle).Set(); state.AsyncCallBack(state.AsyncResult); } return; } //接收到主要数据 else { //判断是否接收了完整的数据 if (dataLength + state.DataLength != state.Data.Length) { //增加已接收数据长度 state.DataLength += dataLength; try { //继续接收数据 state.Stream.BeginRead(state.Data, state.DataLength, state.Data.Length - state.DataLength, EndRead, state); } catch { //出现Socket异常 lock (StateSet) StateSet.Add(state.AsyncResult, state); state.Data = new byte[0]; ((AutoResetEvent)state.AsyncResult.AsyncWaitHandle).Set(); state.AsyncCallBack(state.AsyncResult); return; } return; } //接收完成 state.Completed = true; lock (StateSet) StateSet.Add(state.AsyncResult, state); ((AutoResetEvent)state.AsyncResult.AsyncWaitHandle).Set(); state.AsyncCallBack(state.AsyncResult); } }