xref: /haiku/src/add-ons/print/transports/shared/SocketStream.cpp (revision 8449c783969cae2c4d4197feb75bf4d08bba8de2)
1926f1247SMichael Pfeiffer // Sun, 18 Jun 2000
2926f1247SMichael Pfeiffer // Y.Takagi
3926f1247SMichael Pfeiffer 
4*8449c783SMichael Lotz #if defined(__HAIKU__) || defined(HAIKU_TARGET_PLATFORM_BONE)
5926f1247SMichael Pfeiffer #	include <sys/socket.h>
6926f1247SMichael Pfeiffer #else
7926f1247SMichael Pfeiffer #	include <net/socket.h>
8926f1247SMichael Pfeiffer #endif
9926f1247SMichael Pfeiffer 
10926f1247SMichael Pfeiffer #ifdef _DEBUG
11926f1247SMichael Pfeiffer #include <iostream>
12926f1247SMichael Pfeiffer #include <fstream>
13926f1247SMichael Pfeiffer #endif
14926f1247SMichael Pfeiffer 
15926f1247SMichael Pfeiffer #include "Socket.h"
16926f1247SMichael Pfeiffer #include "SocketStream.h"
17926f1247SMichael Pfeiffer 
18926f1247SMichael Pfeiffer /*-----------------------------------------------------------------*/
19926f1247SMichael Pfeiffer 
socketstreambuf(Socket * sock,streamsize n)20926f1247SMichael Pfeiffer socketstreambuf::socketstreambuf(Socket *sock, streamsize n)
21926f1247SMichael Pfeiffer 	: streambuf(), __sock(sock), __alsize(n), __pu(NULL), __po(NULL)
22926f1247SMichael Pfeiffer {
23926f1247SMichael Pfeiffer 	setg(0, 0, 0);
24926f1247SMichael Pfeiffer 	setp(0, 0);
25926f1247SMichael Pfeiffer }
26926f1247SMichael Pfeiffer 
~socketstreambuf()27926f1247SMichael Pfeiffer socketstreambuf::~socketstreambuf()
28926f1247SMichael Pfeiffer {
29926f1247SMichael Pfeiffer 	if (__pu)
30926f1247SMichael Pfeiffer 		delete [] __pu;
31926f1247SMichael Pfeiffer 	if (__po)
32926f1247SMichael Pfeiffer 		delete [] __po;
33926f1247SMichael Pfeiffer }
34926f1247SMichael Pfeiffer 
underflow()35926f1247SMichael Pfeiffer int socketstreambuf::underflow()
36926f1247SMichael Pfeiffer {
37926f1247SMichael Pfeiffer //	cout << "***** underflow" << endl;
38926f1247SMichael Pfeiffer 
39926f1247SMichael Pfeiffer 	int bytes;
40926f1247SMichael Pfeiffer 
41926f1247SMichael Pfeiffer 	if (__pu == NULL) {
42926f1247SMichael Pfeiffer 		__pu = new char[__alsize];
43926f1247SMichael Pfeiffer 	}
44926f1247SMichael Pfeiffer 
45926f1247SMichael Pfeiffer 	bytes = __sock->read(__pu, __alsize);
46926f1247SMichael Pfeiffer 	if (bytes > 0) {
47926f1247SMichael Pfeiffer #ifdef _DEBUG
48926f1247SMichael Pfeiffer 		ofstream ofs("recv.log", ios::binary | ios::app);
49926f1247SMichael Pfeiffer 		ofs.write(__pu, bytes);
50926f1247SMichael Pfeiffer #endif
51926f1247SMichael Pfeiffer 		setg(__pu, __pu, __pu + bytes);
52926f1247SMichael Pfeiffer 		return *gptr();
53926f1247SMichael Pfeiffer 	}
54926f1247SMichael Pfeiffer 
55926f1247SMichael Pfeiffer 	return EOF;
56926f1247SMichael Pfeiffer }
57926f1247SMichael Pfeiffer 
overflow(int c)58926f1247SMichael Pfeiffer int socketstreambuf::overflow(int c)
59926f1247SMichael Pfeiffer {
60926f1247SMichael Pfeiffer //	cout << "***** overflow" << endl;
61926f1247SMichael Pfeiffer 
62926f1247SMichael Pfeiffer 	if (__po == NULL) {
63926f1247SMichael Pfeiffer 		__po = new char[__alsize];
64926f1247SMichael Pfeiffer 		setp(__po, __po + __alsize);
65926f1247SMichael Pfeiffer 	} else if (sync() != 0) {
66926f1247SMichael Pfeiffer 		return EOF;
67926f1247SMichael Pfeiffer 	}
68926f1247SMichael Pfeiffer 	return sputc(c);
69926f1247SMichael Pfeiffer }
70926f1247SMichael Pfeiffer 
sync()71926f1247SMichael Pfeiffer int socketstreambuf::sync()
72926f1247SMichael Pfeiffer {
73926f1247SMichael Pfeiffer //	cout << "***** sync" << endl;
74926f1247SMichael Pfeiffer 
75926f1247SMichael Pfeiffer 	if (__po) {
76926f1247SMichael Pfeiffer 		int length = pptr() - pbase();
77926f1247SMichael Pfeiffer 		if (length > 0) {
78926f1247SMichael Pfeiffer 			const char *buffer = pbase();
79926f1247SMichael Pfeiffer 			int bytes;
80926f1247SMichael Pfeiffer 			while (length > 0) {
81926f1247SMichael Pfeiffer 				bytes = __sock->write(buffer, length);
82926f1247SMichael Pfeiffer 				if (bytes <= 0) {
83926f1247SMichael Pfeiffer 					return EOF;
84926f1247SMichael Pfeiffer 				}
85926f1247SMichael Pfeiffer #ifdef _DEBUG
86926f1247SMichael Pfeiffer 				ofstream ofs("send.log", ios::binary | ios::app);
87926f1247SMichael Pfeiffer 				ofs.write(buffer, bytes);
88926f1247SMichael Pfeiffer #endif
89926f1247SMichael Pfeiffer 				length -= bytes;
90926f1247SMichael Pfeiffer 				buffer += bytes;
91926f1247SMichael Pfeiffer 			}
92926f1247SMichael Pfeiffer 			pbump(pbase() - pptr());
93926f1247SMichael Pfeiffer 		}
94926f1247SMichael Pfeiffer 	}
95926f1247SMichael Pfeiffer 
96926f1247SMichael Pfeiffer 	return 0;
97926f1247SMichael Pfeiffer }
98926f1247SMichael Pfeiffer 
99926f1247SMichael Pfeiffer /* -------------------------------------------------------------- */
100926f1247SMichael Pfeiffer 
socketstreambase(Socket * sock,streamsize n)101926f1247SMichael Pfeiffer socketstreambase::socketstreambase(Socket *sock, streamsize n)
102926f1247SMichael Pfeiffer 	: buf(sock, n)
103926f1247SMichael Pfeiffer {
104926f1247SMichael Pfeiffer 	ios::init(&this->buf);
105926f1247SMichael Pfeiffer }
106926f1247SMichael Pfeiffer 
107926f1247SMichael Pfeiffer /*-----------------------------------------------------------------*/
108926f1247SMichael Pfeiffer 
isocketstream(Socket * sock,streamsize n)109926f1247SMichael Pfeiffer isocketstream::isocketstream(Socket *sock, streamsize n)
110926f1247SMichael Pfeiffer 	: socketstreambase(sock, n), istream(socketstreambase::rdbuf())
111926f1247SMichael Pfeiffer {
112926f1247SMichael Pfeiffer }
113926f1247SMichael Pfeiffer 
~isocketstream()114926f1247SMichael Pfeiffer isocketstream::~isocketstream()
115926f1247SMichael Pfeiffer {
116926f1247SMichael Pfeiffer }
117926f1247SMichael Pfeiffer 
118926f1247SMichael Pfeiffer /*-----------------------------------------------------------------*/
119926f1247SMichael Pfeiffer 
osocketstream(Socket * sock,streamsize n)120926f1247SMichael Pfeiffer osocketstream::osocketstream(Socket *sock, streamsize n)
121926f1247SMichael Pfeiffer 	: socketstreambase(sock, n), ostream(socketstreambase::rdbuf())
122926f1247SMichael Pfeiffer {
123926f1247SMichael Pfeiffer }
124926f1247SMichael Pfeiffer 
~osocketstream()125926f1247SMichael Pfeiffer osocketstream::~osocketstream()
126926f1247SMichael Pfeiffer {
127926f1247SMichael Pfeiffer 	flush();
128926f1247SMichael Pfeiffer }
129