You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@thrift.apache.org by Hannes Baldursson <ha...@gmail.com> on 2009/03/27 07:48:33 UTC

C# threaded server

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

Re: C# threaded server

Posted by Franis Sirkovic <fr...@gmail.com>.
Hi.

We have implemented Init() method in our interface (as Jonathan suggests).
This is the method client calls to register itself. There is a static field
in the class that has ThreadStaticAttribute. We initialize this field in our
Init method. It will hold all data that is specific to one client. Also, you
can use same solution in some other languages (Python, Java).

There is another problem in thrift library.  There is no indication of
broken connection in a server. I would like to have some kind of closed()
method in processor. So, you have to rely on GC to clean-up the data related
to the client (TLS will be released when thread is destroyed).

Also, you have to take care if you plan to use different languages. There
are some differences between thrift libraries.

Best wishes, Franis Sirkovic.


On Fri, Mar 27, 2009 at 7:19 PM, Jonathan Marcus <ja...@jobstick.com>wrote:

> 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 <franis.sirkovic@gmail.com
>> >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 <hannson@gmail.com
>>>
>>>> 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
>>>>
>>>>
>>
>

Re: C# threaded server

Posted by Jonathan Marcus <ja...@jobstick.com>.
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 <fr...@gmail.com>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 <hannson@gmail.com
>>> 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
>>>
> 


Re: C# threaded server

Posted by Hannes Baldursson <ha...@gmail.com>.
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 <fr...@gmail.com>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 <hannson@gmail.com
> >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
> >
>

Re: C# threaded server

Posted by Franis Sirkovic <fr...@gmail.com>.
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 <ha...@gmail.com>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
>