示例#1
0
        static void handleRequestEnter(InputPacket ip, User u)
        {
            Console.WriteLine("handlerE");
            OutputPacket op1 = new OutputPacket(512, Protocol.RESPONSE_ENTER);

            u.setUserName(ip.readBytes());
            op1.writeUser(u);

            op1.writeInt32(usrList.Count);

            foreach (User otherUsr in usrList)
            {
                op1.writeUser(otherUsr);
            }
            u.sendPacketAsync(op1);

            OutputPacket op2 = new OutputPacket(512, Protocol.BROADCAST_ENTER);

            op2.writeUser(u);
            foreach (User otherUsr in usrList)
            {
                otherUsr.sendPacketAsync(op2);
            }

            usrList.Add(u);
            //Console.WriteLine("HEOS");
        }
示例#2
0
        private void receivePacketComplete(IAsyncResult ar)
        {
            try
            {
                int len = usrSocket.EndReceive(ar);
                if (len == 0)
                {
                    close();
                    return;
                }
                else
                {
                    end += len;
                }
            }
            catch (Exception e)
            {
                close();
                return;
            }

            while (true)
            {
                if (4 > end - cursor)
                {
                    break;
                }
                int packetLen = BitConverter.ToInt32(receiveBuffer, cursor);
                if (packetLen < 0 || packetLen > Global.MAX_INPUT_PACKET_LEN)
                {
                    close();
                    return;
                }
                if (end - cursor < packetLen)
                {
                    break;
                }

                InputPacket ip = new InputPacket(receiveBuffer, cursor, packetLen);
                inputPacketQ.Enqueue(ip);
                cursor += packetLen;
            }
            if (cursor >= Global.MAX_INPUT_PACKET_LEN)
            {
                Buffer.BlockCopy(receiveBuffer, cursor, receiveBuffer, 0, end - cursor);
            }
            try
            {
                usrSocket.BeginReceive(receiveBuffer, end, receiveBuffer.Length - end, SocketFlags.None, receivePacketComplete, null);
            }
            catch (Exception e)
            {
                close();
            }
        }
示例#3
0
        public InputPacket readPacket()
        {
            if (inputPacketQ.Count == 0)
            {
                return(null);
            }
            InputPacket ip = null;

            inputPacketQ.TryDequeue(out ip);

            return(ip);
        }
示例#4
0
        static void handleRequestChangeDestination(InputPacket ip, User u)
        {
            OutputPacket op  = new OutputPacket(512, Protocol.BROADCAST_CHANGE_DESTINATION);
            int          key = u.getKey();

            op.writeInt32(key);
            op.writeVector2(u.posVec);
            Vector2 newDestinaton = ip.readVector2();

            op.writeVector2(newDestinaton);
            u.destinationVec = newDestinaton;

            //Console.WriteLine(key + u.posVec.x + ", " + u.posVec.y + " " + newDestinaton.x + "," + newDestinaton.y);
            foreach (User otherUsr in usrList)
            {
                otherUsr.sendPacketAsync(op);
            }
        }
示例#5
0
 static void ReadPackets()
 {
     for (int i = 0; i < usrList.Count; i++)
     {
         User        u  = usrList[i];
         InputPacket ip = u.readPacket();
         while (ip != null)
         {
             switch (ip.getProtocol())
             {
             case Protocol.REQUEST_CHANGE_DESTINATION:
                 handleRequestChangeDestination(ip, u);
                 break;
             }
             ip = u.readPacket();
         }
     }
 }
示例#6
0
 static void SweepTmpList()
 {
     lock (tempList)
     {
         for (int i = 0; i < tempList.Count; i++)
         {
             User        u  = tempList[i];
             InputPacket ip = u.readPacket();
             if (ip != null)
             {
                 if (ip.getProtocol() == Protocol.REQUEST_ENTER)
                 {
                     handleRequestEnter(ip, u);
                 }
                 else
                 {
                     u.close();
                 }
                 tempList.RemoveAt(i);
                 i--;
             }
         }
     }
 }