/// <summary> /// accept /// </summary> public TChannel(TSocket socket, TService service) : base(service, ChannelType.Accept) { this.socket = socket; this.parser = new PacketParser(this.recvBuffer); this.RemoteAddress = socket.RemoteAddress; this.OnAccepted(); }
public override AChannel ConnectChannel(string host, int port) { TSocket newSocket = new TSocket(this.poller); TChannel channel = new TChannel(newSocket, host, port, this); this.idChannels[channel.Id] = channel; return channel; }
public override async Task<AChannel> AcceptChannel() { if (this.acceptor == null) { throw new Exception("service construct must use host and port param"); } TSocket socket = new TSocket(this.poller); await this.acceptor.AcceptAsync(socket); TChannel channel = new TChannel(socket, this); this.idChannels[channel.Id] = channel; return channel; }
/// <summary> /// connect /// </summary> public TChannel(TSocket socket, string host, int port, TService service) : base(service, ChannelType.Connect) { this.socket = socket; this.parser = new PacketParser(this.recvBuffer); this.RemoteAddress = host + ":" + port; bool result = this.socket.ConnectAsync(host, port); if (!result) { this.OnConnected(this.Id, SocketError.Success); return; } this.socket.OnConn += e => OnConnected(this.Id, e); }
public Task<bool> AcceptAsync(TSocket accpetSocket) { if (this.socket == null) { throw new Exception($"TSocket已经被Dispose,不能接收连接!"); } var tcs = new TaskCompletionSource<bool>(); this.innArgs.UserToken = tcs; this.innArgs.AcceptSocket = accpetSocket.socket; if (!this.socket.AcceptAsync(this.innArgs)) { OnAcceptComplete(this.innArgs); } return tcs.Task; }
/// <summary> /// 即可做client也可做server /// </summary> /// <param name="host"></param> /// <param name="port"></param> public TService(string host, int port) { this.acceptor = new TSocket(this.poller, host, port); }
public TChannel(TSocket socket, string host, int port, TService service) : base(service) { this.socket = socket; this.parser = new PacketParser(this.recvBuffer); this.remoteAddress = host + ":" + port; }