2 #include <asm/ioctls.h>
6 #include <lib/network/socket.h>
10 if (writebuffer.empty())
12 int wasconnected=(mystate==Connection) || (mystate==Closing);
23 rsn->setRequested(rsn->getRequested()|eSocketNotifier::Write);
27 void eSocket::enableRead()
30 rsn->setRequested(rsn->getRequested()|eSocketNotifier::Read);
33 void eSocket::disableRead()
36 rsn->setRequested(rsn->getRequested()&~eSocketNotifier::Read);
39 void eSocket::inject(const char *data, int len)
41 readbuffer.write(data, len);
42 if (mystate == Connection)
46 std::string eSocket::readLine()
48 int size=readbuffer.searchchr('\n');
51 size++; // ich will auch das \n
54 readbuffer.read(buffer, size);
55 return std::string(buffer);
58 bool eSocket::canReadLine()
60 return readbuffer.searchchr('\n') != -1;
63 int eSocket::bytesAvailable()
65 return readbuffer.size();
68 int eSocket::readBlock(char *data, unsigned int maxlen)
70 return readbuffer.read(data, maxlen);
73 int eSocket::bytesToWrite()
75 return writebuffer.size();
83 int eSocket::setSocket(int s, int iss, eMainloop *ml)
87 fcntl(socketdesc, F_SETFL, O_NONBLOCK);
88 last_break = 0xFFFFFFFF;
92 rsn=new eSocketNotifier(ml, getDescriptor(),
93 eSocketNotifier::Read|eSocketNotifier::Hungup);
94 CONNECT(rsn->activated, eSocket::notifier);
98 void eSocket::notifier(int what)
100 if ((what & eSocketNotifier::Read) && (mystate == Connection))
104 if (ioctl(getDescriptor(), FIONREAD, &bytesavail)<0)
105 eDebug("FIONREAD failed.\n");
110 if (!bytesavail) // does the REMOTE END has closed the connection? (no Hungup here!)
116 } else // when operating on terminals, check for break
118 // where is this struct defined?
119 struct async_icount {
120 unsigned long cts, dsr, rng, dcd, tx, rx;
121 unsigned long frame, parity, overrun, brk;
122 unsigned long buf_overrun;
125 if (!ioctl(getDescriptor(), TIOCGICOUNT, &icount))
127 if (last_break == 0xFFFFFFFF)
128 last_break = icount.brk;
129 else if (last_break != icount.brk)
131 last_break = icount.brk;
132 readbuffer.fromfile(getDescriptor(), bytesavail);
135 rsn->setRequested(rsn->getRequested()&~eSocketNotifier::Write);
136 write(getDescriptor(), "BREAK!", 6);
143 if ((r=readbuffer.fromfile(getDescriptor(), bytesavail)) != bytesavail)
145 eDebug("fromfile failed!");
148 } else if (what & eSocketNotifier::Write)
150 if ((mystate == Connection) || (mystate == Closing))
152 if (!writebuffer.empty())
154 bytesWritten_(writebuffer.tofile(getDescriptor(), 65536));
155 if (writebuffer.empty())
157 rsn->setRequested(rsn->getRequested()&~eSocketNotifier::Write);
158 if (mystate == Closing)
160 close(); // warning, we might get destroyed after close.
165 eDebug("got ready to write, but nothin in buffer. strange.");
166 if (mystate == Closing)
168 } else if (mystate == Connecting)
171 rsn->setRequested(rsn->getRequested()&~eSocketNotifier::Write);
174 socklen_t size=sizeof(res);
175 ::getsockopt(getDescriptor(), SOL_SOCKET, SO_ERROR, &res, &size);
184 } else if (what & eSocketNotifier::Hungup)
186 if (mystate == Connection)
190 } else if (mystate == Connecting)
193 socklen_t size=sizeof(res);
194 ::getsockopt(getDescriptor(), SOL_SOCKET, SO_ERROR, &res, &size);
201 int eSocket::writeBlock(const char *data, unsigned int len)
204 if (issocket && writebuffer.empty())
206 int tw=::send(getDescriptor(), data, len, MSG_NOSIGNAL);
207 if ((tw < 0) && (errno != EWOULDBLOCK))
216 writebuffer.write(data, len);
218 if (!writebuffer.empty())
219 rsn->setRequested(rsn->getRequested()|eSocketNotifier::Write);
223 int eSocket::getDescriptor()
228 int eSocket::connectToHost(std::string hostname, int port)
230 struct hostent *server;
237 server=gethostbyname(hostname.c_str());
240 eDebug("can't resolve %s", hostname.c_str());
244 bzero( (char*)&serv_addr, sizeof(serv_addr));
245 serv_addr.sin_family=AF_INET;
246 bcopy( (char*)server->h_addr,
247 (char*)&serv_addr.sin_addr.s_addr,
249 serv_addr.sin_port=htons(port);
250 res=::connect(socketdesc, (const sockaddr*)&serv_addr, sizeof(serv_addr));
251 if ((res < 0) && (errno != EINPROGRESS))
253 eDebug("can't connect to host: %s", hostname.c_str());
258 if (res < 0) // EINPROGRESS
260 rsn->setRequested(rsn->getRequested()|eSocketNotifier::Write);
270 eSocket::eSocket(eMainloop *ml): readbuffer(32768), writebuffer(32768), rsn(0)
273 int s=socket(AF_INET, SOCK_STREAM, 0);
275 eDebug("[SOCKET]: initalized socket %d", socketdesc);
281 eSocket::eSocket(int socket, int issocket, eMainloop *ml): readbuffer(32768), writebuffer(32768), rsn(0)
283 setSocket(socket, issocket, ml);