blocking_write.c 1.61 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
2
/* blocking_write.c
 *
Niels Möller's avatar
Niels Möller committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 * $id:$ */

/* lsh, an implementation of the ssh protocol
 *
 * Copyright (C) 1998 Niels Mller
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Niels Möller's avatar
Niels Möller committed
22
23
 */

24
25
#include "blocking_write.h"

26
#include "io.h"
27
28
#include "xalloc.h"

29
30
31
32
33
34
35
36
/* CLASS:
   (class
     (name blocking_write)
     (super abstract_write)
     (vars
       (fd . int)
       (write . (pointer (function int int UINT32 "UINT8 *")))))
*/
37

38
#include "blocking_write.c.x"
39
40
41

static int do_blocking_write(struct abstract_write *w,
			     struct lsh_string *packet)
Niels Möller's avatar
Niels Möller committed
42
{
43
  CAST(blocking_write, closure, w);
44
  int success = closure->write(closure->fd, packet->length, packet->data);
Niels Möller's avatar
Niels Möller committed
45

Niels Möller's avatar
Niels Möller committed
46
  lsh_string_free(packet);
47
48

  return success ? LSH_OK : LSH_FAIL | LSH_DIE; 
Niels Möller's avatar
Niels Möller committed
49
50
}

51
struct abstract_write *make_blocking_write(int fd, int with_nonblocking)
Niels Möller's avatar
Niels Möller committed
52
{
53
  NEW(blocking_write, closure);
Niels Möller's avatar
Niels Möller committed
54

55
  closure->super.write = do_blocking_write;
56
  closure->write = (with_nonblocking ? write_raw_with_poll : write_raw);
Niels Möller's avatar
Niels Möller committed
57
58
  closure->fd = fd;

59
  return &closure->super;
Niels Möller's avatar
Niels Möller committed
60
}