public void OpenPort() { var pipeServer = new NamedPipeServerStream(PipeConst.XOCLOCK_PIPE_NAME, PipeDirection.InOut); _log.Debug("Waiting for client connection..."); pipeServer.WaitForConnection(); int threadId = Thread.CurrentThread.ManagedThreadId; _log.Debug("Client connected on thread[{0}].", threadId); try { // Read the request from the client. Once the client has // written to the pipe its security token will be available. StreamString ss = new StreamString(pipeServer); // Verify our identity to the connected client using a // string that the client anticipates. ss.WriteString(PipeConst.XOCLOCK_SERVER_ID); string command = ss.ReadString(); // Display the name of the user we are impersonating. _log.Debug("received command: '{0}' on thread[{1}] as user: {2}.", command, threadId, pipeServer.GetImpersonationUserName()); DispatchToModel(command); } catch (IOException e) { _log.Debug("ERROR: {0}", e.Message); } pipeServer.Close(); pipeServer.Dispose(); }
public bool ConnectToServer() { _pipeClient = new NamedPipeClientStream(PipeConst.LOCAL_SERVER_NAME, PipeConst.XOCLOCK_PIPE_NAME, PipeDirection.InOut, PipeOptions.None, TokenImpersonationLevel.Impersonation); int timeout = 1000; try { _log.Debug("Connecting to server...\n"); _pipeClient.Connect(timeout); _stream = new StreamString(_pipeClient); } catch (Exception) { return(false); } if (_stream.ReadString() == PipeConst.XOCLOCK_SERVER_ID) { IsConnected = true; return(true); } else { _log.Debug("Server could not be verified."); return(false); } }