internal void OnDisconnect(EEvent eEvent) { if (this.Disconnect == null) { return; } this.Disconnect(eEvent); }
internal void OnConnected(EEvent eEvent) { if (this.Connected == null) { return; } Action <EEvent> localConnected = this.Connected; this.Connected = null; // 此调用将让await ConnectAsync返回,所以null必须在此之前设置 localConnected(eEvent); }
private EEvent GetEvent() { var enetEv = new ENetEvent(); int ret = NativeMethods.EnetHostCheckEvents(this.host, enetEv); if (ret <= 0) { return(null); } var e = new EEvent(enetEv); return(e); }
internal void OnReceived(EEvent eEvent) { // 如果应用层还未调用readasync则将包放到缓存队列 if (this.Received == null) { using (var packet = new EPacket(eEvent.PacketPtr)) { var bytes = packet.Bytes; this.recvBuffer.AddLast(bytes); } } else { Action <EEvent> localReceived = this.Received; this.Received = null; // 此调用将让await ReadAsync返回,所以null必须在此之前设置 localReceived(eEvent); } }
public Task <bool> AcceptAsync() { if (this.service.PeersManager.ContainsKey(IntPtr.Zero)) { throw new EException("do not accept twice!"); } var tcs = new TaskCompletionSource <bool>(); // 如果有请求连接缓存的包,从缓存中取 if (this.service.ConnEEvents.Count > 0) { EEvent eEvent = this.service.ConnEEvents.First.Value; this.service.ConnEEvents.RemoveFirst(); this.PeerPtr = eEvent.PeerPtr; this.service.PeersManager.Add(this.PeerPtr, this); tcs.TrySetResult(true); } else { this.service.PeersManager.Add(this.PeerPtr, this); this.Connected = eEvent => { if (eEvent.EventState == EventState.DISCONNECTED) { tcs.TrySetException(new EException("socket disconnected in accpet")); } this.service.PeersManager.Remove(IntPtr.Zero); this.PeerPtr = eEvent.PeerPtr; this.service.PeersManager.Add(this.PeerPtr, this); tcs.TrySetResult(true); }; } return(tcs.Task); }
public void RunOnce(int timeout = 0) { if (timeout < 0) { throw new ArgumentOutOfRangeException(string.Format("timeout: {0}", timeout)); } this.OnEvents(); if (this.Service(timeout) < 0) { return; } while (true) { EEvent eEvent = this.GetEvent(); if (eEvent == null) { return; } switch (eEvent.Type) { case EventType.Connect: { // 这是一个connect peer if (this.PeersManager.ContainsKey(eEvent.PeerPtr)) { ESocket eSocket = this.PeersManager[eEvent.PeerPtr]; eSocket.OnConnected(eEvent); } // accept peer else { // 如果server端没有acceptasync,则请求放入队列 if (!this.PeersManager.ContainsKey(IntPtr.Zero)) { this.connEEvents.AddLast(eEvent); } else { ESocket eSocket = this.PeersManager[IntPtr.Zero]; eSocket.OnConnected(eEvent); } } break; } case EventType.Receive: { ESocket eSocket = this.PeersManager[eEvent.PeerPtr]; eSocket.OnReceived(eEvent); break; } case EventType.Disconnect: { // 如果链接还在缓存中,则删除 foreach (EEvent connEEvent in this.connEEvents) { if (connEEvent.PeerPtr != eEvent.PeerPtr) { continue; } this.connEEvents.Remove(connEEvent); return; } // 链接已经被应用层接收 eEvent.EventState = EventState.DISCONNECTED; ESocket eSocket = this.PeersManager[eEvent.PeerPtr]; this.PeersManager.Remove(eEvent.PeerPtr); // 等待的task将抛出异常 if (eSocket.Connected != null) { eSocket.OnConnected(eEvent); } else if (eSocket.Received != null) { eSocket.OnReceived(eEvent); } else if (eSocket.Disconnect != null) { eSocket.OnDisconnect(eEvent); } eSocket.OnError(ErrorCode.ClientDisconnect); break; } } } }