public Task <USocket> AcceptAsync() { if (this.AcceptTcs != null) { throw new Exception("do not accept twice!"); } var tcs = new TaskCompletionSource <USocket>(); // 如果有请求连接缓存的包,从缓存中取 if (this.connQueue.Count > 0) { IntPtr ptr = this.connQueue.FirstKey; this.connQueue.Remove(ptr); USocket socket = new USocket(ptr, this); this.USocketManager.Add(ptr, socket); tcs.SetResult(socket); } else { this.AcceptTcs = tcs; } return(tcs.Task); }
public override AChannel ConnectChannel(string host, int port) { USocket newSocket = new USocket(this.poller); UChannel channel = new UChannel(newSocket, host, port, this); this.idChannels[channel.Id] = channel; return channel; }
public void Update() { this.OnEvents(); if (this.Service() < 0) { return; } while (true) { ENetEvent eNetEvent = this.TryGetEvent(); if (eNetEvent == null) { return; } switch (eNetEvent.Type) { case EventType.Connect: { // 这是一个connect peer if (this.USocketManager.ContainsKey(eNetEvent.Peer)) { USocket uSocket = this.USocketManager[eNetEvent.Peer]; uSocket.OnConnected(); break; } // 这是accept peer if (this.AcceptTcs != null) { this.OnAccepted(eNetEvent); break; } // 如果server端没有acceptasync,则请求放入队列 this.connQueue.Add(eNetEvent.Peer, eNetEvent); break; } case EventType.Receive: { USocket uSocket = this.USocketManager[eNetEvent.Peer]; uSocket.OnReceived(eNetEvent); break; } case EventType.Disconnect: { USocket uSocket = this.USocketManager[eNetEvent.Peer]; this.USocketManager.Remove(uSocket.PeerPtr); uSocket.PeerPtr = IntPtr.Zero; uSocket.OnDisconnect(eNetEvent); break; } } } }
/// <summary> /// accept /// </summary> public UChannel(USocket socket, UService service) : base(service, ChannelType.Accept) { this.socket = socket; this.service = service; this.RemoteAddress = socket.RemoteAddress; this.socket.Received += this.OnRecv; this.socket.Disconnect += () => { this.OnError(this, SocketError.SocketError); }; }
public override AChannel ConnectChannel(string host, int port) { USocket newSocket = new USocket(this.poller); UChannel channel = new UChannel(newSocket, host, port, this); this.idChannels[channel.Id] = channel; return(channel); }
public void Update() { this.OnEvents(); if (this.Service() < 0) { return; } while (true) { if (NativeMethods.enet_host_check_events(this.host, ref this.eNetEventCache) <= 0) { return; } switch (this.eNetEventCache.Type) { case EventType.Connect: { // 这是一个connect peer if (this.USocketManager.ContainsKey(this.eNetEventCache.Peer)) { USocket uSocket = this.USocketManager[this.eNetEventCache.Peer]; uSocket.OnConnected(); break; } // 这是accept peer if (this.AcceptTcs != null) { this.OnAccepted(this.eNetEventCache); break; } // 如果server端没有acceptasync,则请求放入队列 this.connQueue.Enqueue(this.eNetEventCache.Peer); break; } case EventType.Receive: { USocket uSocket = this.USocketManager[this.eNetEventCache.Peer]; uSocket.OnReceived(this.eNetEventCache); break; } case EventType.Disconnect: { USocket uSocket = this.USocketManager[this.eNetEventCache.Peer]; this.USocketManager.Remove(uSocket.PeerPtr); uSocket.PeerPtr = IntPtr.Zero; uSocket.OnDisconnect(this.eNetEventCache); break; } } } }
/// <summary> /// connect /// </summary> public UChannel(USocket socket, string host, int port, UService service) : base(service, ChannelType.Connect) { this.socket = socket; this.service = service; this.RemoteAddress = host + ":" + port; this.socket.ConnectAsync(host, (ushort)port); this.socket.Received += this.OnRecv; this.socket.Disconnect += () => { this.OnError(this, SocketError.SocketError); }; }
/// <summary> /// connect /// </summary> public UChannel(USocket socket, string host, int port, UService service): base(service, ChannelType.Connect) { this.socket = socket; this.service = service; this.RemoteAddress = host + ":" + port; this.socket.ConnectAsync(host, (ushort)port); this.socket.Received += this.OnRecv; this.socket.Disconnect += () => { this.OnError(this, SocketError.SocketError); }; }
public override async Task <AChannel> AcceptChannel() { USocket socket = await this.poller.AcceptAsync(); UChannel channel = new UChannel(socket, this); this.idChannels[channel.Id] = channel; return(channel); }
public override AChannel GetChannel(string host, int port) { UChannel channel = null; USocket newSocket = new USocket(this.poller); channel = new UChannel(newSocket, host, port, this); newSocket.Disconnect += () => this.OnChannelError(channel.Id, SocketError.SocketError); this.idChannels[channel.Id] = channel; return(channel); }
public void Update() { this.OnEvents(); if (this.Service() < 0) { return; } while (true) { ENetEvent eNetEvent = this.GetEvent(); if (eNetEvent == null) { return; } switch (eNetEvent.Type) { case EventType.Connect: { // 这是一个connect peer if (this.USocketManager.ContainsKey(eNetEvent.Peer)) { USocket uSocket = this.USocketManager[eNetEvent.Peer]; uSocket.OnConnected(eNetEvent); } break; } case EventType.Receive: { USocket uSocket = this.USocketManager[eNetEvent.Peer]; uSocket.OnReceived(eNetEvent); break; } case EventType.Disconnect: { USocket uSocket = this.USocketManager[eNetEvent.Peer]; this.USocketManager.Remove(uSocket.PeerPtr); uSocket.PeerPtr = IntPtr.Zero; uSocket.OnDisconnect(eNetEvent); break; } } } }
private void OnAccepted(ENetEvent eEvent) { if (eEvent.Type == EventType.Disconnect) { this.AcceptTcs.TrySetException(new Exception("socket disconnected in accpet")); } USocket socket = new USocket(eEvent.Peer, this); this.USocketManager.Add(socket.PeerPtr, socket); socket.OnAccepted(); var tcs = this.AcceptTcs; this.AcceptTcs = null; tcs.SetResult(socket); }
public UChannel(USocket socket, string host, int port, UService service) : base(service) { this.socket = socket; this.service = service; this.remoteAddress = host + ":" + port; }
public void Add(IntPtr peerPtr, USocket uSocket) { this.sockets.Add(peerPtr, uSocket); }
public Task<USocket> AcceptAsync() { if (this.AcceptTcs != null) { throw new Exception("do not accept twice!"); } var tcs = new TaskCompletionSource<USocket>(); // 如果有请求连接缓存的包,从缓存中取 if (this.connQueue.Count > 0) { IntPtr ptr = this.connQueue.FirstKey; this.connQueue.Remove(ptr); USocket socket = new USocket(ptr, this); this.USocketManager.Add(ptr, socket); tcs.SetResult(socket); } else { this.AcceptTcs = tcs; } return tcs.Task; }