File size: 982 Bytes
c7436b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*

 * user_interface.h

 *

 *  Created on: May 16, 2015

 *      Author: Gideon

 */

#ifndef SOCKET_STREAM_H_
#define SOCKET_STREAM_H_

#define OUT_BUFFER_SIZE 2048

#include "stream.h"

class SocketStream : public Stream
{
	int  actual_socket;
	int  purge();
	int  transmit(const char *buffer, int n);

	int buf_size;
	int buf_remaining;
	int skip;
	char *buf_pos;
	char *buf_start;
public:
	SocketStream(int sock) {
		actual_socket = sock;
		buf_size = OUT_BUFFER_SIZE;
		buf_start = new char[buf_size];
		buf_pos = buf_start;
		buf_remaining = buf_size;
		skip = 0;
	}
	~SocketStream() {
		delete[] buf_start;
	}

	void close();

	// stream interface
	int write(const char *buffer, int n);
	int get_char(void);
	bool is_alive(void) { return actual_socket >= 0; }
    void charout(int c)
    {
    	char cc = (char)c;
    	write(&cc, 1);
    }

    void sync(void)
    {
    	purge();
    }
};

#endif /* SOCKET_STREAM_H_ */