public void Recv() { if (this.socket == null) { return; } while (socket != null && this.socket.Available > 0) { int messageLength = 0; try { messageLength = this.socket.ReceiveFrom(this.cache, ref this.ipEndPoint); } catch (Exception e) { Log.Error(e); continue; } // 长度小于1,不是正常的消息 if (messageLength < 1) { continue; } // accept byte flag = this.cache[0]; // conn从1000开始,如果为1,2,3则是特殊包 uint remoteConn = 0; uint localConn = 0; KChannel kChannel = null; switch (flag) { case KcpProtocalType.SYN: // accept // 长度!=5,不是accpet消息 if (messageLength != 5) { break; } IPEndPoint acceptIpEndPoint = (IPEndPoint)this.ipEndPoint; this.ipEndPoint = new IPEndPoint(0, 0); remoteConn = BitConverter.ToUInt32(this.cache, 1); // 如果已经收到连接,则忽略 if (this.waitConnectChannels.TryGetValue(remoteConn, out kChannel)) { break; } localConn = ++this.IdGenerater; kChannel = new KChannel(localConn, remoteConn, this.socket, acceptIpEndPoint, this); this.localConnChannels[kChannel.LocalConn] = kChannel; this.waitConnectChannels[remoteConn] = kChannel; this.OnAccept(kChannel); break; case KcpProtocalType.ACK: // connect返回 // 长度!=9,不是connect消息 if (messageLength != 9) { break; } remoteConn = BitConverter.ToUInt32(this.cache, 1); localConn = BitConverter.ToUInt32(this.cache, 5); kChannel = this.GetKChannel(localConn); if (kChannel != null) { kChannel.HandleConnnect(remoteConn); } break; case KcpProtocalType.FIN: // 断开 // 长度!=13,不是DisConnect消息 if (messageLength != 13) { break; } remoteConn = BitConverter.ToUInt32(this.cache, 1); localConn = BitConverter.ToUInt32(this.cache, 5); // 处理chanel kChannel = this.GetKChannel(localConn); if (kChannel != null) { // 校验remoteConn,防止第三方攻击 if (kChannel.RemoteConn == remoteConn) { kChannel.Disconnect(ErrorCode.ERR_PeerDisconnect); } } break; case KcpProtocalType.MSG: // 断开 // 长度<9,不是Msg消息 if (messageLength < 9) { break; } // 处理chanel remoteConn = BitConverter.ToUInt32(this.cache, 1); localConn = BitConverter.ToUInt32(this.cache, 5); this.waitConnectChannels.Remove(remoteConn); kChannel = this.GetKChannel(localConn); if (kChannel != null) { // 校验remoteConn,防止第三方攻击 if (kChannel.RemoteConn == remoteConn) { kChannel.HandleRecv(this.cache, 5, messageLength - 5); } } break; } } }
public void Add(DLLType dllType, Assembly assembly) { this.assemblies[dllType] = assembly; this.types.Clear(); foreach (Assembly value in this.assemblies.Values) { foreach (Type type in value.GetTypes()) { object[] objects = type.GetCustomAttributes(typeof(BaseAttribute), false); if (objects.Length == 0) { continue; } BaseAttribute baseAttribute = (BaseAttribute) objects[0]; this.types.Add(baseAttribute.AttributeType, type); } } this.awakeSystems.Clear(); this.lateUpdateSystems.Clear(); this.updateSystems.Clear(); this.startSystems.Clear(); this.loadSystems.Clear(); this.changeSystems.Clear(); this.destroySystems.Clear(); this.deserializeSystems.Clear(); foreach (Type type in types[typeof(ObjectSystemAttribute)]) { object[] attrs = type.GetCustomAttributes(typeof(ObjectSystemAttribute), false); if (attrs.Length == 0) { continue; } object obj = Activator.CreateInstance(type); switch (obj) { case IAwakeSystem objectSystem: this.awakeSystems.Add(objectSystem.Type(), objectSystem); break; case IUpdateSystem updateSystem: this.updateSystems.Add(updateSystem.Type(), updateSystem); break; case ILateUpdateSystem lateUpdateSystem: this.lateUpdateSystems.Add(lateUpdateSystem.Type(), lateUpdateSystem); break; case IStartSystem startSystem: this.startSystems.Add(startSystem.Type(), startSystem); break; case IDestroySystem destroySystem: this.destroySystems.Add(destroySystem.Type(), destroySystem); break; case ILoadSystem loadSystem: this.loadSystems.Add(loadSystem.Type(), loadSystem); break; case IChangeSystem changeSystem: this.changeSystems.Add(changeSystem.Type(), changeSystem); break; case IDeserializeSystem deserializeSystem: this.deserializeSystems.Add(deserializeSystem.Type(), deserializeSystem); break; } } this.allEvents.Clear(); foreach (Type type in types[typeof(EventAttribute)]) { object[] attrs = type.GetCustomAttributes(typeof(EventAttribute), false); foreach (object attr in attrs) { EventAttribute aEventAttribute = (EventAttribute)attr; object obj = Activator.CreateInstance(type); IEvent iEvent = obj as IEvent; if (iEvent == null) { Log.Error($"{obj.GetType().Name} 没有继承IEvent"); } this.RegisterEvent(aEventAttribute.Type, iEvent); } } this.Load(); }
public async void Run() { await Task.Delay(2000); long.TryParse(levelDBStore.Get("UndoHeight"), out transferHeight); Log.Debug($"Consensus.Run at height {transferHeight}"); // 恢复到停机前的高度 ApplyBlockChain(); // 算力统计 calculatePower.Clear(); for (long ii = Math.Max(1, transferHeight - calculatePower.statistic); ii <= transferHeight; ii++) { calculatePower.Insert(BlockChainHelper.GetMcBlock(ii)); } int blockIndex = 0; while (true) { try { if (newBlocks.Count > 0) { for (blockIndex = blockIndex % newBlocks.Count; blockIndex < newBlocks.Count; blockIndex++) { if (newBlocks[blockIndex] == null) { continue; } var ipEndPoint = newBlocks[blockIndex].ipEndPoint; Block otherMcBlk = JsonHelper.FromJson <Block>(newBlocks[blockIndex].block); if (Check(otherMcBlk)) { if (await SyncHeight(otherMcBlk, newBlocks[blockIndex].ipEndPoint)) { newBlocks.RemoveAll((x) => { return(x.ipEndPoint == ipEndPoint); }); break; } //if (await SyncHeight(otherMcBlk, newBlocks[ii].ipEndPoint)) ApplyBlockChain(); } //break; } } ApplyBlockChain(); lock (this) { runAction?.Invoke(); runAction = null; } if (bifurcatedReportTime.IsPassOnce() && bifurcatedReport != "") { Log.Info(bifurcatedReport); bifurcatedReport = ""; } await Task.Delay(1000); } catch (Exception e) { newBlocks.Clear(); Log.Error(e); await Task.Delay(1000); } } }
/// <summary> /// 接收完成时 /// </summary> /// <param name="o"></param> private void OnRecvComplete(object o) { if (this.socket == null) { return; } SocketAsyncEventArgs e = (SocketAsyncEventArgs)o; if (e.SocketError != SocketError.Success) { this.OnError((int)e.SocketError); return; } //获取在套接字操作中传输的字节数 //如果等于0 if (e.BytesTransferred == 0) { this.OnError(ErrorCode.ERR_PeerDisconnect); return; } //上一次的索引加上本次接收到的数据数量 this.recvBuffer.LastIndex += e.BytesTransferred; //如果刚好等于8192了 说明缓冲区写满了 if (this.recvBuffer.LastIndex == this.recvBuffer.ChunkSize) { this.recvBuffer.AddLast(); //将recvBuffer中的lastBuffer指向另一个可用的数组 this.recvBuffer.LastIndex = 0; } // 收到消息回调 while (true) { try { //进行解析 把完整的报文(去掉包头表示包体大小的字段)写入内存流中 //如果解析失败就跳出 if (!this.parser.Parse()) { break; } } catch (Exception ee) { Log.Error($"ip: {this.RemoteAddress} {ee}"); this.OnError(ErrorCode.ERR_SocketError); return; } try { //将上面解析得到的memoryStream传递给OnRead //执行读取的委托 外部谁注册了 就由谁进行读取 数据在内存流中 当作委托参数进行传递给委托的注册方了. //这个框架中 由Session在Awake方法中 进行读取的委托注册 this.OnRead(this.parser.GetPacket()); } catch (Exception ee) { Log.Error(ee); } } if (this.socket == null) { return; } //继续下一次的异步接收 this.StartRecv(); }