Пример #1
0
        //---------------------------------------
        //				RECEIVE
        //---------------------------------------
        //Reading data
        public static void ReadCallbackTCP(IAsyncResult ar)
        {
            //Start with an empty msg
            string content = string.Empty;


            StateObject state     = (StateObject)ar.AsyncState;
            Socket      handle    = state.workSocket;
            int         readBytes = handle.EndReceive(ar);

            Console.WriteLine(readBytes);

            //If there is data to read
            if (readBytes > 0)
            {
                //Get content and print a debug msg
                content = Encoding.ASCII.GetString(state.buffer, 0, state.bufferSize);
                Console.WriteLine("Server received: '{0}', {1} bytes", content, readBytes);
                state.buffer = new byte[1024];


                //Produce content and handle
                MessageTCP msg = new MessageTCP();
                msg.sock = state.workSocket;
                msg.body = content;
                producerConsumerTCP.produce(msg);

                //Loop
                handle.BeginReceive(state.buffer, 0, state.bufferSize, 0, new AsyncCallback(ReadCallbackTCP), state);
            }
            else if (readBytes == 0)
            {
                Console.WriteLine("Socket closed");
            }
        }
 //PRODUCE
 public void produce(MessageTCP message)
 {
     Console.WriteLine("PRODUCER TCP: " + message);
     lock (_lockObject)
     {
         Monitor.Wait(_lockObject);
         _MyQueue.Enqueue(message);
         Monitor.Pulse(_lockObject);
     }
 }
Пример #3
0
        //---------------------------------------
        //				INTERPRET
        //---------------------------------------
        //This runs on a separate thread

        public static void interpretTCP()
        {
            while (true)
            {
                MessageTCP newMsg = producerConsumerTCP.consume();

                if (newMsg != null)
                {
                    Console.WriteLine("Server consumed content of " + newMsg.body);

                    string sub = newMsg.body.Substring(0, 3);

                    //REGISTER to a server
                    if (sub == "REG")
                    {
                        string[] elements = newMsg.body.Split(' ');
                        string   charName = elements[1];
                        RegisterClient(newMsg.sock, charName);

                        //SendTCP(newMsg.body);
                    }

                    //JOIN the race
                    if (sub == "JOI")
                    {
                        string[] elements = newMsg.body.Split(' ');
                        string   charName = elements[1];
                        Join(charName);
                    }

                    //REPEAT the joining proccess
                    if (sub == "REP")
                    {
                        //Send information of all players to all players
                        foreach (string entry in clientDictionary.Keys)
                        {
                            if (clientDictionary[entry].raceId != -1)
                            {
                                SendTCPToRegistered("JOI " + clientDictionary[entry].raceId.ToString() + entry, clientDictionary[entry].sessionID);
                            }
                        }
                    }

                    if (sub == "SCR")
                    {
                        //insert to database
                        string[] elements   = newMsg.body.Split(' ');
                        string   charName   = elements[1];
                        string   score      = elements[2];
                        float    scoreFloat = (float)Convert.ToDouble(score);

                        //Handle score, update database and players
                        ScoreHandle(charName, scoreFloat);
                    }

                    if (sub == "BLT")
                    {
                        string[] elements = newMsg.body.Split(' ');
                        string   charName = elements[1];
                        string   angle    = elements[2];

                        SendTCPToRegistered("BLT " + clientDictionary[charName].raceId + " " + angle + " ", clientDictionary[charName].sessionID);
                    }

                    //Console.WriteLine("Server consumed content of " + newMsg.body);
                    //SendTCP(newMsg.body);
                }
            }
        }