示例#1
1
		public void WorkerProcessMain(string argument, string passwordBase64)
		{
			int port = int.Parse(argument, CultureInfo.InvariantCulture);
			
			client = new TcpClient();
			client.Connect(new IPEndPoint(IPAddress.Loopback, port));
			Stream stream = client.GetStream();
			receiver = new PacketReceiver();
			sender = new PacketSender(stream);
			shutdownEvent = new ManualResetEvent(false);
			receiver.ConnectionLost += OnConnectionLost;
			receiver.PacketReceived += OnPacketReceived;
			sender.WriteFailed += OnConnectionLost;
			
			// send password
			sender.Send(Convert.FromBase64String(passwordBase64));
			
			receiver.StartReceive(stream);
			while (!shutdownEvent.WaitOne(SendKeepAliveInterval, false)) {
				Program.Log("Sending keep-alive packet");
				sender.Send(new byte[0]);
			}
			
			Program.Log("Closing client (end of WorkerProcessMain)");
			client.Close();
			shutdownEvent.Close();
		}
		void OnAcceptTcpClient(IAsyncResult ar)
		{
			clientAuthenticated = false;
			SetTimeout();
			try {
				client = listener.EndAcceptTcpClient(ar);
			} catch (SocketException) {
				// error connecting
			}
			listener.Stop();
			listener = null;
			
			if (client == null) {
				OnConnectionLost(null, null);
			} else {
				Stream stream = client.GetStream();
				receiver = new PacketReceiver();
				sender = new PacketSender(stream);
				receiver.ConnectionLost += OnConnectionLost;
				receiver.PacketReceived += OnPacketReceived;
				
				receiver.StartReceive(stream);
				OnReady();
			}
		}