void NetworkThread() { while (_running == 1) { var endPoint = new IPEndPoint(IPAddress.Loopback, 4040); RemoteSide remoteSide = null; while (_running == 1 && remoteSide == null) { if ((remoteSide = TryConnect(endPoint)) == null) { Thread.Sleep(1000); } } Interlocked.Exchange(ref _connected, 1); while (_running == 1 && remoteSide.Connected) { StatePortion state; while ((state = remoteSide.ReadMessage()) != null) { _incomingState.Enqueue(state); } Command cmd; while ((cmd = _outgoingCommands.Dequeue()) != null) { remoteSide.WriteMessage(cmd); } Thread.Sleep(1); } Interlocked.Exchange(ref _connected, 0); } ; }
public void SetUp() { tempDir = new DirectoryInfo(Path.Combine(Path.GetTempPath(), "protobuf-net-gh24-" + Convert.ToBase64String(BitConverter.GetBytes(DateTime.UtcNow.Ticks)).Replace('/', '-').Substring(0, 11))); tempDir.Create(); var subdir = tempDir.CreateSubdirectory("temp"); assemblyPath = new FileInfo(Path.Combine(subdir.FullName, ASSEMBLY_NAME + ".dll")); CopyAssembly(assemblyPath); var setup = new AppDomainSetup { ApplicationBase = tempDir.FullName, // setting this to true allows both tests to pass DisallowApplicationBaseProbing = false }; domain = AppDomain.CreateDomain("Gh24", null, setup); sponsor = new ClientSponsor(); remoteSide = (RemoteSide)domain.CreateInstanceFromAndUnwrap(typeof(RemoteSide).Assembly.Location, typeof(RemoteSide).FullName); sponsor.Register(remoteSide); var references = new[] { typeof(Serializer).Assembly }; var assemblyPaths = references.ToDictionary(a => a.GetName(), a => a.Location); assemblyPaths[new AssemblyName(ASSEMBLY_NAME)] = assemblyPath.FullName; remoteSide.Initialize(assemblyPaths); }
static RemoteSide TryConnect(IPEndPoint endPoint) { var client = new TcpClient(); try { client.Connect(endPoint); var remote = new RemoteSide(client, StatePortion.Deserialize); Debug.LogFormat("Connected to {0}", remote.RemoteEndPoint); client = null; // RemoteSide now owns TcpClient (see finally) return(remote); } catch (SocketException ex) { if (ex.SocketErrorCode != SocketError.ConnectionRefused) { throw ex; } } finally { if (client != null) { client.Close(); } } return(null); }