public TChannel(TSocket socket, string host, int port, TService service): base(service) { this.socket = socket; this.service = service; this.parser = new PacketParser(this.recvBuffer); this.remoteAddress = host + ":" + port; }
public TChannel(TSocket socket, TService service): base(service) { this.isConnected = true; this.socket = socket; this.service = service; this.parser = new PacketParser(this.recvBuffer); this.remoteAddress = this.socket.RemoteAddress; this.StartRecv(); }
public void ClientSendToServer() { const string hostName = "127.0.0.1"; const ushort port = 8889; using (IService clientService = new TService()) { using (IService serverService = new TService(hostName, 8889)) { Task task1 = Task.Factory.StartNew(() => { while (!this.isClientStop) { clientService.Update(); } }, TaskCreationOptions.LongRunning); Task task2 = Task.Factory.StartNew(() => { while (!this.isServerStop) { serverService.Update(); } }, TaskCreationOptions.LongRunning); // 往server host线程增加事件,accept serverService.Add(() => this.ServerEvent(serverService)); // 往client host线程增加事件,client线程连接server clientService.Add(() => this.ClientEvent(clientService, hostName, port)); this.barrier.SignalAndWait(); serverService.Add(() => { this.isServerStop = true; }); clientService.Add(() => { this.isClientStop = true; }); Task.WaitAll(task1, task2); } } }