public UChannel(USocket socket, UService service): base(service) { this.isConnected = true; this.socket = socket; this.service = service; this.remoteAddress = this.socket.RemoteAddress; }
public async Task <AChannel> GetChannel() { USocket socket = await this.poller.AcceptAsync(); UChannel channel = new UChannel(socket, this); this.channels[channel.RemoteAddress] = channel; this.idChannels[channel.Id] = channel; return(channel); }
public UPoller(string hostName, ushort port) { this.acceptor = new USocket(IntPtr.Zero, this); UAddress address = new UAddress(hostName, port); ENetAddress nativeAddress = address.Struct; this.host = NativeMethods.ENetHostCreate(ref nativeAddress, NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID, 0, 0, 0); if (this.host == IntPtr.Zero) { throw new UException("Host creation call failed."); } NativeMethods.ENetHostCompressWithRangeCoder(this.host); }
public AChannel GetChannel(string host, int port) { UChannel channel = null; if (this.channels.TryGetValue(host + ":" + port, out channel)) { return(channel); } USocket newSocket = new USocket(this.poller); channel = new UChannel(newSocket, host, port, this); this.channels[channel.RemoteAddress] = channel; this.idChannels[channel.Id] = channel; this.SocketConnectAsync(channel); return(channel); }
private void Dispose(bool disposing) { if (this.socket == null) { return; } this.onDispose(this); if (disposing) { this.socket.Dispose(); } this.service.Remove(this); this.socket = null; }
public Task <USocket> AcceptAsync() { if (this.uSocketManager.ContainsKey(IntPtr.Zero)) { throw new UException("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.TrySetResult(socket); } else { this.uSocketManager.Add(this.acceptor.PeerPtr, this.acceptor); this.acceptor.Connected = eEvent => { if (eEvent.Type == EventType.Disconnect) { tcs.TrySetException(new UException("socket disconnected in accpet")); } this.uSocketManager.Remove(IntPtr.Zero); USocket socket = new USocket(eEvent.Peer, this); this.uSocketManager.Add(socket.PeerPtr, socket); tcs.TrySetResult(socket); }; } return(tcs.Task); }
public UChannel(USocket socket, string host, int port, UService service): base(service) { this.socket = socket; this.service = service; this.remoteAddress = host + ":" + port; }
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.uSocketManager.ContainsKey(IntPtr.Zero)) { throw new UException("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.TrySetResult(socket); } else { this.uSocketManager.Add(this.acceptor.PeerPtr, this.acceptor); this.acceptor.Connected = eEvent => { if (eEvent.Type == EventType.Disconnect) { tcs.TrySetException(new UException("socket disconnected in accpet")); } this.uSocketManager.Remove(IntPtr.Zero); USocket socket = new USocket(eEvent.Peer, this); this.uSocketManager.Add(socket.PeerPtr, socket); tcs.TrySetResult(socket); }; } return tcs.Task; }
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; } // 这是accept peer if (this.uSocketManager.ContainsKey(IntPtr.Zero)) { USocket uSocket = this.uSocketManager[IntPtr.Zero]; uSocket.OnConnected(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: { // 如果链接还在缓存中,则删除 if (this.connQueue.Remove(eNetEvent.Peer)) { break; } // 链接已经被应用层接收 USocket uSocket = this.uSocketManager[eNetEvent.Peer]; this.uSocketManager.Remove(eNetEvent.Peer); // 等待的task将抛出异常 if (uSocket.Connected != null) { uSocket.OnConnected(eNetEvent); break; } if (uSocket.Received != null) { uSocket.OnReceived(eNetEvent); break; } if (uSocket.Disconnect != null) { uSocket.OnDisconnect(eNetEvent); break; } break; } } } }
public AChannel GetChannel(string host, int port) { UChannel channel = null; if (this.channels.TryGetValue(host + ":" + port, out channel)) { return channel; } USocket newSocket = new USocket(this.poller); channel = new UChannel(newSocket, host, port, this); this.channels[channel.RemoteAddress] = channel; this.idChannels[channel.Id] = channel; this.SocketConnectAsync(channel); return channel; }