示例#1
0
 private void HandleFriendRequest(FriendRequest request)
 {
     try
     {
         var targetAccount = connectedAccounts.Find(x => x.Username == request.TargetUsername && x.PublicId == request.TargetPublicId);
         if (targetAccount != null)
         {
             request.TargetId = targetAccount.Id;
             var receiverNwStream = targetAccount.tcpClient.GetStream();
             JsonSerializerProvider.SerializeBinary(receiverNwStream, request);
             Console.WriteLine("Friend Request from {0}#{1} to {2}#{3}  | public key: {4}", request.SenderUsername, request.SenderPublicId,
                               request.TargetUsername, request.TargetPublicId, System.Text.Encoding.UTF8.GetString(request.senderPublicKey));
         }
     }
     catch (Exception e)
     {
         Console.WriteLine("Exception: " + e.Message);
     }
 }
示例#2
0
        private void InterpretIncomingMessage(NetworkStream nwStream)
        {
            object received = JsonSerializerProvider.DeserializeBinary(nwStream);

            if (received is Message message)
            {
                Console.OutputEncoding = System.Text.Encoding.UTF8;
                Console.WriteLine("Message from {0} to {1}, {2}: {3}", message.SenderUniqueId, message.ReceiverUniqueId, message.Timestamp,
                                  System.Text.Encoding.UTF8.GetString(message.EncryptedMessage));
                try
                {
                    var receiver = connectedAccounts.FirstOrDefault(x => x.Id == message.ReceiverUniqueId);
                    if (receiver != null)
                    {
                        var receiverNwStream = receiver.tcpClient.GetStream();
                        // Writes serialized binary data into the stream
                        JsonSerializerProvider.SerializeBinary(receiverNwStream, message);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Exception: " + e.Message);
                }
            }
            else if (received is FriendRequest request)
            {
                HandleFriendRequest(request);
            }
            else if (received is FriendResponse response)
            {
                HandleFriendResponse(response);
            }
            else
            {
                Console.WriteLine("Received: " + received);
            }
        }