srv_pipe: reorganize code so that related functions are close to each other