Esempio n. 1
0
    public static void ProcessClientRequest(IAsyncResult ar)
    {
        // Retrieve the state object and the handler socket
        // from the asynchronous state object.
        StateObject state   = (StateObject)ar.AsyncState;
        Socket      handler = state.workSocket;
        string      input;
        IPEndPoint  remoteIpEndPoint = handler.RemoteEndPoint as IPEndPoint;

        try
        {
            String content = String.Empty;
            int    ptr     = 0;

            //Getting the current thread id
            int utid = GetCurrentThreadId();

            foreach (ProcessThread pt in Process.GetCurrentProcess().Threads)
            {
                //When the incoming thread id matches the process thread id
                if (utid == pt.Id)
                {
                    //Mutex lock starts here, allows only one thread to enter
                    ft.WaitOne();

                    ptr = (int)Math.Pow(2, (currProcessor - 1));

                    //Sets affinity to current thread, so as to process one thread per processor
                    pt.ProcessorAffinity = (IntPtr)ptr;
                    currProcessor++;

                    //Gets current processor number where the thread is running
                    int coreid = GetCurrentProcessorNumber();

                    Console.WriteLine("The current thread id {0} processing the client {1}:{2} runs in the processor: {3} ",
                                      utid, remoteIpEndPoint.Address, remoteIpEndPoint.Port, coreid);

                    //If all the cores are assigned with one thread per core, then resetting the curprocessor value to one,
                    //so as to form a round robin allocation
                    if (currProcessor > Environment.ProcessorCount)
                    {
                        currProcessor = 1;
                    }

                    ft.ReleaseMutex();
                    //Mutex lock ends here

                    break;
                }
            }

            // Read data from the client socket.
            int bytesRead = handler.EndReceive(ar);

            if (bytesRead > 0)
            {
                // There  might be more data, so store the data received so far.
                state.sb.Append(Encoding.ASCII.GetString(
                                    state.buffer, 0, bytesRead));

                // Check for end-of-file tag. If it is not there, read more data.
                content = state.sb.ToString();
                if (content.IndexOf("<EOF>") > -1)
                {
                    input = content.Remove(content.IndexOf("<EOF>"));

                    //Displaying the data received from the client
                    Console.WriteLine("Data Received: {0}  in the Thread Id: {1} at {2} from the Client located at IPaddress:{3} and port {4}",
                                      input, GetCurrentThreadId(), DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss.ff"), remoteIpEndPoint.Address, remoteIpEndPoint.Port);

                    //Mutex starts to handle critical code
                    mt.WaitOne();

                    //Function call to KeyvalueDictionary
                    string output = KeyValueStore.KeyValueDictionary(input);

                    mt.ReleaseMutex(); //Mutex Ends here

                    // Echo the data back to the client.
                    Send(handler, output);
                }
                else
                {
                    // Not all data received. Get more.
                    handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                                         new AsyncCallback(ProcessClientRequest), state);
                }
            }
        }
        catch (SocketException se)
        {
            Console.WriteLine("The Client connected from IP address: " +
                              remoteIpEndPoint.Address + " on port number: " + remoteIpEndPoint.Port +
                              " got disconnected at the time stamp: " + DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss.ff"));
            handler.Shutdown(SocketShutdown.Both);
            handler.Close();
        }
    }