封装Socket,将回调push到主线程处理
Inheritance: IDisposable
Ejemplo n.º 1
0
		/// <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();
		}
Ejemplo n.º 2
0
		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;
		}
Ejemplo n.º 3
0
		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;
		}
Ejemplo n.º 4
0
		/// <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);
		}
Ejemplo n.º 5
0
		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;
		}
Ejemplo n.º 6
0
		/// <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);
		}
Ejemplo n.º 7
0
 /// <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);
 }
Ejemplo n.º 8
0
 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;
 }