void ServiceMain(int argc, char** argv) { serviceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS; serviceStatus.dwCurrentState = SERVICE_START_PENDING; serviceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN; serviceStatus.dwWin32ExitCode = 0; serviceStatus.dwServiceSpecificExitCode = 0; serviceStatus.dwCheckPoint = 0; serviceStatus.dwWaitHint = 0; serviceStatusHandle = RegisterServiceCtrlHandlerW(serviceName, (LPHANDLER_FUNCTION)ControlHandler); if (serviceStatusHandle == (SERVICE_STATUS_HANDLE)0) return; serviceStatus.dwCurrentState = SERVICE_RUNNING; SetServiceStatus(serviceStatusHandle, &serviceStatus); SocketManager *sock = new SocketManager(); sock->start(); while (serviceStatus.dwCurrentState == SERVICE_RUNNING) { Sleep(1000); } }
int _tmain(int argc, _TCHAR* argv[]) { SocketManager mng = SocketManager(); mng.init(); mng.run(); return 0; }
int main(int argc, char **argv) { #if 1 SocketManager * mng = SocketManager::getInstance(); mng->init(); mng->start(); mng->fini(); #endif return 0; }
/* * Test that socket creation / closing works. */ void Ut_Connection::testSocket() { const char* socketName = "testAccept"; SocketManager sm; sm.initSocket(socketName); m_subject.reset(new MyConnection(sm.findSocket(socketName), false)); m_subject->m_fd = 1000; QVERIFY(m_subject->m_fd > 0); m_subject->close(); QVERIFY(m_subject->m_fd == -1); unlink(socketName); }
void* handleTrame() { SocketManager* receiveSocketManager = new SocketManager(); receiveSocketManager->startListening(name); pthread_t handleTrame_th; sockaddr_in adresse_client_courant; int longueur_adresse_courante; int newSocketDescriptor; for(;;) { longueur_adresse_courante = sizeof(adresse_client_courant); if((newSocketDescriptor = accept(receiveSocketManager->getSocketDescriptor(),(sockaddr*)(&adresse_client_courant),&longueur_adresse_courante))<0) { cout << "unable to accept a new client " << endl; return 1; } if(pthread_create(&handleTrame_th,NULL,singleHandler,(void*)&newSocketDescriptor)) { cout << "unable to create a new thread" << endl; return 1; } } return; }
// Pass-by-value function (not recommended) // void use_SocketManager(SocketManager mgr) { cout << "use_SocketManager(SocketManager)" << endl; mgr.send("Sending data..."); }
int main() { /* IPEndPoint ep(IPAddress("0.0.0.0"),26969); Socket s(AF_INET,SOCK_DGRAM,0); s.Bind(ep); Buffer b(4096); StandardStream ss; while(true) { int br=s.Recv(b); ss << b.SubBuffer(0,br); } //*/ /*IPEndPoint ep(IPAddress("0.0.0.0"),7899); Socket s1(AF_INET,SOCK_STREAM,0); s1.Bind(ep); s1.Listen(10); while(true) { Socket s=s1.Accept(); try{ Buffer b(4096); StandardStream ss; while(true) { int br=s.Recv(b); if(br<=0)break; ss << b.SubBuffer(0,br); } }catch(Exception&ex){ s.Close(); } }*/ StringBuilder motd_sb; Buffer motd; motd_sb << "You are now connected to the chat server.\n"; try { FileStream fs("motd",O_RDONLY); motd_sb.Clear(); while(motd_sb.Append(fs,4096)>0); } catch(Exception& ex){} motd_sb << "Please enter a nickname: "; motd=motd_sb.ToBuffer(); IPEndPoint ep(IPAddress("0.0.0.0"),16969); Socket s1(AF_INET,SOCK_STREAM,0); s1.Bind(ep); s1.Listen(10); SocketManager* m = SocketManager::GetDefault(); struct { SocketManager::Callback cb; } tmp1; tmp1.cb=SocketManager::Callback([&](void* v, SocketManager* m, Socket sock) { Socket s=sock.Accept(); client* zxcv=new client(s); zxcv->send(motd); broadcast("Connected", zxcv, true); clients.insert(zxcv); zxcv->cb=SocketManager::Callback([](void* v, SocketManager* m, Socket sock) { client* zxcv=(client*)v; //try //{ int br; if((br=m->EndRecv(sock))<=0)goto close; if(zxcv->name_init) broadcast(zxcv->b.SubBuffer(0,br), zxcv); else { if(br>50) { zxcv->send("\x1B[41;1;33mNickname can not be longer than 50 chars.\x1B[0;0;0m\nEnter a new one: "); goto aaaaa; } BufferRef tmp=strip_newline(zxcv->b.SubBuffer(0,br)); zxcv->name=string((char*)tmp.Data,tmp.Length); zxcv->name_init=true; broadcast("Set name", zxcv, true); StringBuilder sb; sb << "List of users ("; sb << (int)(clients.size()-1); sb << "): \n"; for(auto it=clients.begin();it!=clients.end();it++) { if((*it)!=zxcv) { if((*it)->name_init) sb << (*it)->name << " ("; sb << (*it)->ep->ToStr(); if((*it)->name_init) sb << ')'; sb << '\n'; } } sb << "You can now chat.\n"; zxcv->send(sb.ToBuffer()); } aaaaa: m->BeginRecv(sock, zxcv->b, zxcv->cb, false); //}catch(Exception& ex){goto close;} return; close: m->Cancel(sock); sock.Close(); clients.erase(zxcv); broadcast("Disconnected", zxcv, true); delete zxcv; }, zxcv); m->BeginRecv(s, zxcv->b, zxcv->cb, false); m->BeginAccept(sock, tmp1.cb); }); m->BeginAccept(s1, tmp1.cb); m->EventLoop(); }