Working on sync and async
This commit is contained in:
		
							parent
							
								
									65c2cdbbaf
								
							
						
					
					
						commit
						e0feb8b2da
					
				| @ -25,6 +25,8 @@ class client; | ||||
|  * Server klasa za TCP/IP soket | ||||
|  * Instanca se incijalizira kada pokrećemo server | ||||
| */ | ||||
| // template<typename... Args>
 | ||||
| 
 | ||||
| 
 | ||||
| class server { | ||||
|     public: | ||||
| @ -32,16 +34,17 @@ class server { | ||||
|     struct sockaddr_in addr; | ||||
|     SSL_CTX* securefds = NULL; | ||||
| 
 | ||||
|     server (const ushort port, const uint limit = 1000, SSL_CTX* _securefds = NULL); | ||||
|     server (const ushort port, const uint queue = 1000, SSL_CTX* _securefds = NULL); | ||||
|     ~server (); | ||||
| 
 | ||||
|     // one klijent
 | ||||
|     client* cli = NULL; | ||||
|     void accept(const uint timeout = 100);    | ||||
|     // template<typename... Args>
 | ||||
|     void sync(const uint timeout = 100, void (*func)(Args ...vars));    | ||||
| 
 | ||||
|     vector<thread> thr; | ||||
|     vector<client*> clis; | ||||
|     void asyncli(const uint limit, void (*handlecli)(client*) , const uint timeout = 100); | ||||
|     // vector<thread> thr;
 | ||||
|     // vector<client*> clis;
 | ||||
|     // void async(const uint limit, void (*handlecli)(Args ...args) , const uint timeout = 100);
 | ||||
| 
 | ||||
| }; | ||||
| 
 | ||||
|  | ||||
| @ -4,9 +4,8 @@ | ||||
|  * Kontrustruktor varijable tipa server, prima port i limit za ograničenje liste klijenata na čekanju | ||||
| */ | ||||
| 
 | ||||
| server::server (const ushort port, const uint limit, SSL_CTX* _securefds) { | ||||
|     securefds = _securefds;         // dodati parametar red čekanja queue koji će se koristiti kao limit u socketu
 | ||||
|                                     // a stavrni limit ćemo korisitit kao broj threadova
 | ||||
| server::server (const ushort port, const uint queue, SSL_CTX* _securefds) { | ||||
|     securefds = _securefds;          | ||||
| 
 | ||||
|     addr.sin_family = AF_INET; | ||||
|     addr.sin_addr.s_addr = INADDR_ANY; | ||||
| @ -27,38 +26,43 @@ server::server (const ushort port, const uint limit, SSL_CTX* _securefds) { | ||||
|         throw string("[ERROR] Unable to bind socket "); | ||||
|     } | ||||
| 
 | ||||
|     if (listen(sock, limit) < 0) { | ||||
|     if (listen(sock, queue) < 0) { | ||||
|         throw string("[ERROR] It is not possible to set the allowed number of waiting clients "); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| void server::accept(const uint timeout) { | ||||
|     if (cli != NULL) { | ||||
|         cli->~client(); | ||||
|         cli = NULL; | ||||
|     } | ||||
|     cli = new client(this, timeout, securefds); | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| void server::asyncli(const uint limit, void (*handlecli)(client*), const uint timeout) { | ||||
| template<typename... Args> | ||||
| void server::sync(const uint timeout, void (*func)(Args ...vars)) { | ||||
|     do { | ||||
|         for (uint i=0; i<limit; i++) { | ||||
|             clis.push_back(new client(this, timeout, securefds)); | ||||
|             thr.push_back(thread(handlecli, clis[clis.size()-1])); | ||||
|         if (cli != NULL) { | ||||
|             cli->~client(); | ||||
|             cli = NULL; | ||||
|         } | ||||
| 
 | ||||
|         for (uint i=0; i<limit; i++) { | ||||
|             thr[i].join(); | ||||
|             clis[i]->~client(); | ||||
|         } | ||||
|         thr.clear(); | ||||
|         clis.clear(); | ||||
| 
 | ||||
|         cli = new client(this, timeout, securefds); | ||||
|         // callback
 | ||||
|         func(); | ||||
|     } while (true); | ||||
| } | ||||
| 
 | ||||
| // template<typename... Args>
 | ||||
| // void server::async(const uint limit, void (*handlecli)(Args ...args), const uint timeout) {
 | ||||
| //     do {
 | ||||
| //         for (uint i=0; i<limit; i++) {
 | ||||
| //             clis.push_back(new client(this, timeout, securefds));
 | ||||
| //             thr.push_back(thread(handlecli, args...));
 | ||||
| //         }
 | ||||
| 
 | ||||
| //         for (uint i=0; i<limit; i++) {
 | ||||
| //             thr[i].join();
 | ||||
| //             clis[i]->~client();
 | ||||
| //         }
 | ||||
| //         thr.clear();
 | ||||
| //         clis.clear();
 | ||||
| 
 | ||||
| //     } while (true);
 | ||||
| // }
 | ||||
| 
 | ||||
| 
 | ||||
| /**
 | ||||
|  * Destruktor varijable tipa server | ||||
|  | ||||
| @ -41,7 +41,13 @@ int main() { | ||||
| 
 | ||||
| 
 | ||||
|         server myserver(5000, 100); | ||||
|         myserver.asyncli(8, [](client *cli) { | ||||
|         // myserver.async(8, [](client *cli) {
 | ||||
|         //     cout << "Klijent " << cli->ipv4 << endl;
 | ||||
|         //     string fromclient = cli->pull();
 | ||||
|         //     cout << "S klijenta " << fromclient << endl;
 | ||||
|         //     cli->push(fromclient);
 | ||||
|         // }, 200);
 | ||||
|         myserver.sync(8, [](client *cli) { | ||||
|             cout << "Klijent " << cli->ipv4 << endl; | ||||
|             string fromclient = cli->pull(); | ||||
|             cout << "S klijenta " << fromclient << endl; | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user