Brief description of class still missing. More...
#include <TcpServerStream.h>
Public Member Functions | |
virtual void | event (short type) |
virtual short | eventTypes () |
bool | listen (uint16_t port, int maxConnections) |
void | send (const char *bytes, int byteCount) |
TcpServerStream () | |
Protected Member Functions | |
virtual void | childDeleted (Stream *client) |
const std::list < TcpClientStream * > & | clients () const |
virtual TcpClientStream * | createClientStream (int fileDescriptor, const Address &peer)=0 |
Brief description of class still missing.
Full description of class still missing
Description of constructor still missing
References GpCoreTools::CoreApplication::exit(), GpCoreTools::Stream::fileDescriptor(), GpCoreTools::Stream::setFileDescriptor(), TRACE, and GpCoreTools::Log::write().
{ TRACE; setFileDescriptor(socket(AF_INET, SOCK_STREAM, 0)); if(fileDescriptor()<=0) { Log::write(1, "tcp server socket %s\n", strerror(errno)); CoreApplication::exit(2); } int optval=1; if(setsockopt(fileDescriptor(),SOL_SOCKET,SO_REUSEADDR,&optval,sizeof(optval)) < 0) { Log::write(0, "tcp server reuseaddr: [%i] %s\n", errno, strerror(errno)); CoreApplication::exit(2); } }
void GpCoreTools::TcpServerStream::childDeleted | ( | Stream * | client | ) | [protected, virtual] |
Reimplemented from GpCoreTools::Stream.
References TRACE.
{ TRACE; std::list<TcpClientStream *>::iterator it; for(it=_clients.begin();it!=_clients.end();it++) { if(*it==client) { break; } } if(it!=_clients.end()) { _clients.erase(it); } }
const std::list<TcpClientStream *>& GpCoreTools::TcpServerStream::clients | ( | ) | const [inline, protected] |
Referenced by GpsServer::broadcast(), and NetServer::sendNeighbors().
{return _clients;}
virtual TcpClientStream* GpCoreTools::TcpServerStream::createClientStream | ( | int | fileDescriptor, |
const Address & | peer | ||
) | [protected, pure virtual] |
Implemented in GpsServer, CubeTcpServer, MasterServer, CrystalfontzServer, TimeMasterServer, NetServer, LinkServer, and SystemServer.
Referenced by event().
void GpCoreTools::TcpServerStream::event | ( | short | type | ) | [virtual] |
Implements GpCoreTools::Stream.
References GpCoreTools::EventLoop::addStream(), createClientStream(), GpCoreTools::Stream::fileDescriptor(), GpCoreTools::EventLoop::instance(), GpCoreTools::Stream::setParent(), TRACE, and GpCoreTools::Log::write().
{ TRACE; if(type & POLLIN) { sockaddr_in peerAddr; socklen_t peerAddrLen=sizeof(sockaddr_in); int peerFd=accept(fileDescriptor(), (sockaddr *)&peerAddr, &peerAddrLen); if(peerFd>0) { Log::write(2, "connection from %s:%hu (socket %i)\n",inet_ntoa(peerAddr.sin_addr), ntohs(peerAddr.sin_port), peerFd); TcpClientStream * s=createClientStream(peerFd, peerAddr.sin_addr.s_addr); EventLoop::instance()->addStream(s); s->setParent(this); _clients.push_back(s); } else { Log::write(1, "%s\n", strerror(errno)); } } }
virtual short GpCoreTools::TcpServerStream::eventTypes | ( | ) | [inline, virtual] |
Implements GpCoreTools::Stream.
{return POLLIN;}
bool GpCoreTools::TcpServerStream::listen | ( | uint16_t | port, |
int | maxConnections | ||
) |
References GpCoreTools::Stream::fileDescriptor(), TRACE, and GpCoreTools::Log::write().
Referenced by main().
{ TRACE; if(fileDescriptor()<=0) return false; sockaddr_in addr; addr.sin_family=AF_INET; addr.sin_port=htons(port); addr.sin_addr.s_addr=htonl(INADDR_ANY); if(bind(fileDescriptor(), (sockaddr *)&addr, sizeof(sockaddr_in))<0) { Log::write(1, "%s\n", strerror(errno)); return false; } if(::listen(fileDescriptor(), maxConnections)<0) { Log::write(1, "%s\n", strerror(errno)); return false; } else { return true; } }
void GpCoreTools::TcpServerStream::send | ( | const char * | bytes, |
int | byteCount | ||
) |
Send the same packet to all connected clients
References TRACE.
Referenced by OkPayload::get(), ErrorPayload::get(), HardVersionPayload::get(), SoftVersionPayload::get(), BatteryPayload::get(), TemperaturePayload::get(), CubeTcpServer::sendState(), FrequencyPayload::sendTCP(), GainsPayload::sendTCP(), and TimeModePayload::sendTCP().
{ TRACE; std::list<TcpClientStream *>::iterator it; for(it=_clients.begin();it!=_clients.end();it++) { (*it)->buffer()->send(bytes, byteCount); } }