Пример #1
0
 public void listen()
 {
     try
     {
         byte[] data     = listener.Receive(ref hostEP);
         Packet incoming = Reconstruct(data);
         if (incoming is Packet000)
         {
             //This should never happen. NEVER!
         }
         else if (incoming is Packet001)
         {
             Packet001 msgPacket = (Packet001)incoming;
             string    message   = msgPacket.getMessage();
             Console.ForegroundColor = msgPacket.getForeground();
             //Console.BackgroundColor = msgPacket.getBackground();
             Console.WriteLine(message);
             Console.ResetColor();
             //messageLog = msgPacket.getLog();
         }
         else if (incoming is Packet002)
         {
             Packet002 msgPacket = (Packet002)incoming;
             string    message   = msgPacket.getMessage();
             Console.ForegroundColor = msgPacket.getForeground();
             //Console.BackgroundColor = msgPacket.getBackground();
             Console.WriteLine(message);
             Console.ResetColor();
             //messageLog = msgPacket.getLog();
         }
         else if (incoming is Packet999)
         {
             //This should never happen. NEVER!
         }
         else
         {
             Console.WriteLine("Packet Confusion");
         }
     }
     catch (Exception e)
     {
         Console.WriteLine(e.ToString());
     }
 }
Пример #2
0
 //I made another change. LOOK!
 private void listen()
 {
     try
     {
         byte[] data     = listener.Receive(ref groupEP);
         Packet incoming = Reconstruct(data);
         if (incoming is Packet000)
         {
             Console.Write("Connection: ");
             Packet000 connectPacket = (Packet000)incoming;
             IPAddress connectingIP  = connectPacket.getSenderIP();
             Console.WriteLine(connectPacket.getName());
             Console.WriteLine(connectingIP.ToString() + ": Player has connected.");
             broadcastTo.Add(connectingIP);
         }
         else if (incoming is Packet001)
         {
             Console.Write("Message: ");
             Packet001    msgPacket = (Packet001)incoming;
             string       message   = msgPacket.getMessage();
             IPAddress    sender    = msgPacket.getSenderIP();
             ConsoleColor color     = msgPacket.getForeground();
             Console.WriteLine(msgPacket.getName());
             Console.WriteLine(message);
             messageLog.Insert(0, lastMessage);
             lastMessage = message;
             broadcastExclusively(new Packet001(sender.ToString() + "> " + lastMessage, color), sender);
         }
         else if (incoming is Packet002)
         {
             Console.Write("Message: ");
             Packet002 msgPacket = (Packet002)incoming;
             string    message   = msgPacket.getMessage();
             IPAddress sender    = msgPacket.getSenderIP();
             //Console.WriteLine(msgPacket.getRecipient().ToString());
             IPAddress recipient = msgPacket.getRecipient();
             if (broadcastTo.Any(e => e.ToString() == recipient.ToString()))
             {
                 Console.WriteLine(msgPacket.getName());
                 Console.WriteLine(message);
                 messageLog.Insert(0, lastMessage);
                 lastMessage = message;
                 broadcastPrivately(new Packet002("[" + sender.ToString() + "]> " + lastMessage, "null:servermessage", ConsoleColor.Magenta), recipient);
             }
             else
             {
                 Console.WriteLine(msgPacket.getName());
                 Console.WriteLine(message);
                 messageLog.Insert(0, lastMessage);
                 lastMessage = message;
                 broadcastPrivately(new Packet002("[" + recipient.ToString() + " doesn't exist]", "null:servermessage", ConsoleColor.Red), sender);
             }
         }
         else if (incoming is Packet999)
         {
             Console.Write("Disconnection: ");
             Packet999 disconnectPacket = (Packet999)incoming;
             IPAddress disconnectingIP  = disconnectPacket.getSenderIP();
             Console.WriteLine(disconnectPacket.getName());
             Console.WriteLine(disconnectingIP.ToString() + ": Player has disconnected.");
             broadcastTo.Remove(disconnectingIP);
         }
         else
         {
             Console.WriteLine("Packet Confusion");
         }
     }
     catch (Exception e)
     {
         Console.WriteLine(e.ToString());
     }
 }