//PRODUCE
 public void produce(MessageUDP message)
 {
     Console.WriteLine("PRODUCER UDP: " + message);
     lock (_lockObject)
     {
         Monitor.Wait(_lockObject);
         _MyQueue.Enqueue(message);
         Monitor.Pulse(_lockObject);
     }
 }
Пример #2
0
        //---------------------------------------
        //				RECEIVE
        //---------------------------------------

        public static void ReceiveCallbackUDP(IAsyncResult ar)
        {
            //Start receiving
            Console.WriteLine("Receiving UDP...");

            //Get a state from the AR
            UdpState stateUdp = ((UdpState)(ar.AsyncState));

            //Get main parametres
            UdpClient uClient = stateUdp.udpClient;
            Socket    sock    = stateUdp.workingSocket;


            //Issue?
            EndPoint ePoint = stateUdp.endPoint;
            //EndPoint ePoint = sock.RemoteEndPoint;

            //Read data into a buffer
            int receiveBytes = sock.EndReceiveFrom(ar, ref ePoint);

            stateUdp.endPoint = (IPEndPoint)ePoint;

            string receiveString = Encoding.ASCII.GetString(((UdpState)ar.AsyncState).buffer);

            Console.WriteLine("UDP received: {0}", receiveString);

            //Clear buffer
            ((UdpState)ar.AsyncState).buffer = new byte[1024];

            //Produce content and handle
            MessageUDP msg = new MessageUDP();

            msg.sock     = sock;
            msg.body     = receiveString;
            msg.endPoint = stateUdp.endPoint;
            producerConsumerUDP.produce(msg);


            // Begin receive
            sock.BeginReceiveFrom(stateUdp.buffer, 0, stateUdp.bufferSize, 0, ref ePoint, new AsyncCallback(ReceiveCallbackUDP), stateUdp);
        }
Пример #3
0
        //---------------------------------------
        //				  INERPRET
        //---------------------------------------
        public static void interpretUDP()
        {
            while (true)
            {
                //Consume msg
                MessageUDP newMsg = producerConsumerUDP.consume();

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

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

                    //RECEIVED REG for registration
                    if (sub == "REG")
                    {
                        string[] elements = newMsg.body.Split(' ');
                        string   charName = elements[1];

                        RegisterUdp(newMsg.endPoint, newMsg.sock, charName);
                        SendUDP(newMsg.body, clientDictionary[charName].sessionID);
                    }

                    //RECEIVED POS for position update
                    if (sub == "POS")
                    {
                        string[] elements = newMsg.body.Split(' ');

                        string name      = elements[3];
                        string newString = elements[0] + elements[1] + " " + elements[2] + " " + clientDictionary[name].raceId;

                        Console.WriteLine(newString);
                        SendUDP(newString, clientDictionary[name].sessionID);
                    }

                    //SendUDP(newMsg.body);
                }
            }
        }