   transfering socket between threads and other (for browser mmo)
  Hello. I'm writing (for fun, actually) a browser mmo on c++ server (linux/windows crossplatform), flash client, so use TCP  and got some simple yet ungooglable questions. I'm not a native english speaker, so excuse me and send grammar corrections in private please =)How do you add a socket to a select's set in a thread where select waits infinitely?Or more precisely - how do you suggest to transfer socket from login thread (that do accept()) to world thread? Or from one location thread to another? It looks like not a bad strategy to have something like one select per location. I'll write overall architecture at the end, maybe i'm just doing it wrong %)Waiting for infinite select to unlock is not looking good (yes, generally it will be fast but in the worst case it will be the timeout time of the lost connection user, like 10sec). The best looking solution as I see now is to have "awaker" connections between login thread and each of the connection listening threads, that will unlock select. Or maybe infinite is not that good - just make select sleep for 0.1 sec and all will be fine. But still think there's gotta be a better way.Other question - is how to test silent disconnect?You know, when two computers (yes, I have only two :() are connected directly and you are disabling connection or unplugging cable Windows sends disconnect event to sockets that were connected at that interface (or maybe it just looks like it, whatever). In the real world there's no immediate event (afaik only after TCP timeout that is huge, remembering a number of like 2 minutes but may be mistaken).Is it a bad idea to have one mutex/critical section per user? Expecting like 5000 users (or maybe even 5000 maximum). Looks like that can make synchronisation abit easier.Also maybe offtopic for the forum, but quite close question: there's a pattern where some threads are used as workers that do jobs assigned by other threads. The question is - how do you make worker idle, when there are no jobs? For now using sleep(100) after fail of cheking for existing jobs. Maybe I should look closely on semaphores?Overall server architecture: one login thread that do accept(), reads login data from user, retrieves via "worker" thread if it is correct and extra user info from mysql database and pushes user to world. Actually it pushes user to one of the world's locations that each have one listening thread with infinite select(), and one update thread (that calculates sent data, updates world (like 10 frames per second) and send delta world state to all it's users). One (or maybe like 2-3) "worker" threads that can do long heavy job asynchronously, all database requests, for example. Wanna have some simple 2d physics (will use box2d engine, at least at the beginning), so 10 frames/sec. Have windows/linux network and threads cross platform wrapper from "MUD Game Programming" book.   
  For web usage using TCP, asynchronous design would be a better choice. For C++ there are two libraries, ACE and boost's asio that also take care of thread pooling. They also take care of timing, timeouts and connection limits.Quote:just make select sleep for 0.1 secselect, IOCP and other APIs provide a way to pass timeout value. Instead of sleep, use that.Quote:Is it a bad idea to have one mutex/critical section per user?It is one per shared resource, the one accessed by multiple threads. Different shared resources not protected by same mutex are considered independent. So mutex per user isn't really useful unless you choose Agent-based design, where each user runs in its own pseudo-thread. Both libraries mention above provide a way to achieve that, but with only minimal number of threads.Quote:there's a pattern where some threads are used as workers that do jobs assigned by other threads. Proactor (asio) and Reactor (ACE).Quote:One (or maybe like 2-3) "worker" threads that can do long heavy job asynchronously, all database requests, for example.Most databases provide asynchronous API, MySQL is one that doesn't and needs to use threads or a slightly hackish manual way. So it depends on database you use.Quote:Other question - is how to test silent disconnect?TCP peer can disapear without you noticing. Track the traffic. If you don't receive anything for reasonable time (can be from 1 second and up to an hour, whatever suits you), disconnect them. Disconnect on socket error is trivial.Quote:how do you make worker idleSome form of synchronization primitive. Events, boost::condition, semaphores, some lockless trickery. It depends on OS. Boost's is portable, but can be absurdly expensive on XP (IIRC). pthreads is another option.   ;  Great thanks, main things are clear now.Question about disconnect was - how to reproduce such disconnect for testing purposes, not how to deal with it %) Calling close() on client's side - sends disconnect event, disabling interface/unplugging cable while having direct connection emulates such event. Wonder if using firewall helps, but some magic closeWithoutNotice would be much better.   ;  Unplugging the cable is the best way to test it, although switching off your router is another good one. You could also try killing the remote program from task manager so that the process has no chance to send any data to facilitate a graceful close, but the operating system might still send TCP-level notification anyway. (I am speculating on this point.)   