Hi,
I believe that when your clients make Thrift requests, they will not
necessarily get the same thread each time. Your hangmanHandler should
have a few functions:
* New HangmanGame instance. This would create an instance, and
return a unique ID for the client to use on subsequent requests. The
handler would store the instance in a Dictionary<id, HangmanGame> for
later retrieval.
* Guess letters (or other game-related functions). In addition to
game-related parameters, you would also pass the Game-Id, which would
let the HangmanHandler find the appropriate HangmanGame to take the
action on.
Basically, the Thrift Server is just going to take requests - you need
to take care of creating/modifying games on your own (like what Franis said)
Regards,
Jonathan Marcus
Hannes Baldursson wrote:
Thanks for your reply Franis. Rewriting the library sounds inefficient and
would most likely create a maintenance hell so I think TLS is the way to go.
Could you perhaps give me some pointers to get me started?
Hannes Baldursson
On Fri, Mar 27, 2009 at 08:49, Franis Sirkovic <[email protected]>wrote:
Hi.
I am thrift user too (just to warn you). Thrift library will never create
new instance of HangmanGame. I have the same problem too. I see 2
solutions.
First, you can write your own library that will create new processor for
each client. Or, you can use thread local storage to store data related to
different clients. I use TLS because I didn't want to rewrite the library.
Best wishes, Franis Sirkovic.
On Fri, Mar 27, 2009 at 7:48 AM, Hannes Baldursson <[email protected]
wrote:
Hi all,
I'm trying to create a threaded server for a simple hangman game written
in
C#. It's working fine for a single client but multiple clients seem to
share
the game instance instead of each having a unique one.
The code I use to start the server is:
bool useBufferedSockets = false;
int port = 9090;
// Processor
* HangmanHandler hangmanHandler = new HangmanHandler();*
HangmanRPC.Processor hangmanProcessor = new
HangmanRPC.Processor(hangmanHandler);
// Transport
TServerSocket tServerSocket = new TServerSocket(port,
0,
useBufferedSockets);
TServer serverEngine;
// Simple Server
//serverEngine = new TSimpleServer(hangmanProcessor,
tServerSocket);
// ThreadPool Server
// serverEngine = new TThreadPoolServer(testProcessor,
tServerSocket);
// Threaded Server
serverEngine = new TThreadedServer(hangmanProcessor,
tServerSocket);
hangmanHandler.server = serverEngine;
// Run it
Console.WriteLine("Starting the server on port " + port
+
(useBufferedSockets ? " with buffered socket" : "") + "...");
serverEngine.Serve();
HangmanRPC is the service generated by the thrift compiler.
HangmanHandler is the implementation that responds to the RPC commands
and
contains an object of HangmanGame which is the code for the game
instance.
The problem is that the HangmanGame object is shared between multiple
clients. What is the ideal place for HangmanGame so it creates a unique
instance for each client?
Thanks in advance,
Hannes Baldursson