コード例 #1
0
ファイル: ChatServer.cs プロジェクト: LudicrousJimblemen/quac
 public static void sendQuacket(Quacket quacket)
 {
     statusChangeMessage(new StatusChangedEventArgs(JsonConvert.SerializeObject(
         quacket
     )));
     try {
         if (quacket.Recipient == null) {
             foreach (var user in users) {
                 try {
                     writer = new StreamWriter(user.Client.GetStream());
                     writer.WriteLine(JsonConvert.SerializeObject(quacket));
                     writer.Flush();
                 } catch (IOException) {
                     System.Diagnostics.Debug.WriteLine("IOException");
                     continue;
                 }
             }
         } /* else {
             //TODO streamwriter is recipvient
             writer.WriteLine(JsonConvert.SerializeObject(quacket));
             writer.Flush();
             writer = null;
         }*/
     } catch (NullReferenceException) {
         System.Diagnostics.Debug.WriteLine("NullReferenceException");
     } catch (InvalidOperationException e) {
         System.Diagnostics.Debug.WriteLine(e.Message);
     }
 }
コード例 #2
0
ファイル: Connection.cs プロジェクト: LudicrousJimblemen/quac
        public void accept()
        {
            System.Diagnostics.Debug.WriteLine("Connection accepted");
            sentData = JsonConvert.DeserializeObject<Quacket>(reader.ReadLine());
            System.Diagnostics.Debug.WriteLine("Read data");

            user = sentData.Sender;
            user.Client = client;

            try {
                System.Diagnostics.Debug.WriteLine("Checking for users...");
                foreach (var otherUser in ChatServer.users) {
                    if (otherUser.Username == user.Username) {
                        System.Diagnostics.Debug.WriteLine("Found matching user.");
                        writer.WriteLine(JsonConvert.SerializeObject(
                            new Quacket(null, true, true, false, true, user, "Someone's already using that username!", null)
                        ));
                        writer.Flush();
                        this.close(false);
                        return;
                    }
                }
            } catch (NullReferenceException) {
                System.Diagnostics.Debug.WriteLine("NullReferenceException");
            }

            ChatServer.addUser(user);
            System.Diagnostics.Debug.WriteLine("Added user.");

            while (ChatServer.listening) {
                try {
                    sentData = JsonConvert.DeserializeObject<Quacket>(reader.ReadLine());
                    sentData.SentUsers = ChatServer.users;
                    if (sentData.IsDisconnectionMessage) {
                        this.close();
                    } else {
                        ChatServer.sendQuacket(sentData);
                    }
                } catch (ObjectDisposedException) {
                    System.Diagnostics.Debug.WriteLine("ObjectDisposedException");
                    break;
                } catch (ArgumentNullException) {
                    System.Diagnostics.Debug.WriteLine("ArgumentNullException");
                    break;
                } catch (IOException) {
                    this.close();
                    System.Diagnostics.Debug.WriteLine("IOException");
                    break;
                }
            }
        }