take profile photos by android phone (still very buggy)

This commit is contained in:
Alexander Eckart committed 2013-05-06 22:08:59 +02:00
1 parent da843b4f06
commit 6d008cdc06
17 files changed
+339 -48

No files matched your search

+27 -8
View File
@@ -24,6 +24,7 @@ namespace ClientServerLib
private CConnection connection;
private List<SRequest> requests;
private List<SRequest> responses;
private Object mutexRequests = new Object();
private OnConnectionChanged onConnectionChanged;
private OnSend onSend;
private OnReceived onReceived;
@@ -70,7 +71,7 @@ namespace ClientServerLib
{
doConnect = false;
lock (requests)
lock (mutexRequests)
{
requests.Clear();
}
@@ -96,7 +97,7 @@ namespace ClientServerLib
req.Callback = Callback;
req.Response = null;
lock (requests)
lock (mutexRequests)
{
requests.Add(req);
}
@@ -195,15 +196,23 @@ namespace ClientServerLib
}
else
{
lock (requests)
try
{
while (requests.Count > 0)
{
SRequest res = HandleRequest(requests[0]);
responses.Add(res);
requests.RemoveAt(0);
lock (responses)
{
responses.Add(res);
}
lock (mutexRequests)
{
requests.RemoveAt(0);
}
}
}
catch { };
lock (responses)
{
@@ -236,7 +245,16 @@ namespace ClientServerLib
RaiseOnSend(request.Command);
byte[] encrypted = connection.Encrypt(request.Command);
clientStream.Write(encrypted, 0, encrypted.Length);
int sendBytes = 0;
while(sendBytes < encrypted.Length)
{
int bytesToSend = encrypted.Length - sendBytes;
if (bytesToSend > bufferLength)
bytesToSend = bufferLength;
clientStream.Write(encrypted, sendBytes, bytesToSend);
sendBytes += bytesToSend;
}
clientStream.Flush();
byte[] data = new byte[bufferLength];
@@ -257,8 +275,9 @@ namespace ClientServerLib
int messageLength = BitConverter.ToInt32(data, 0);
while (messageLength > bytesRead)
{
Array.Resize<byte>(ref data, data.Length + bufferLength);
bytesRead += clientStream.Read(data, data.Length - bufferLength, bufferLength);
if (data.Length < messageLength + bufferLength)
Array.Resize<byte>(ref data, messageLength + bufferLength);
bytesRead += clientStream.Read(data, bytesRead, bufferLength);
}
encrypted = new byte[bytesRead];
+20 -10
View File
@@ -32,6 +32,7 @@ namespace ClientServerLib
public CConnection(TcpClient client, int ConnectionID)
{
TcpClient = client;
client.NoDelay = true;
keySet = false;
connectionID = ConnectionID;
encoder = new UTF8Encoding();
@@ -125,17 +126,26 @@ namespace ClientServerLib
byte[] encrypted = new byte[messageLength - 24];
Array.Copy(Data, 24, encrypted, 0, encrypted.Length);
using (var decryptor = aes.CreateDecryptor(aes.Key, aes.IV))
using (var memoryStream = new MemoryStream())
using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Write))
try
{
cryptoStream.Write(encrypted, 0, encrypted.Length);
cryptoStream.FlushFinalBlock();
cryptoStream.Close();
byte[] decrypted = memoryStream.ToArray();
byte[] message = new byte[dataLength];
Array.Copy(decrypted, message, dataLength);
return message;
using (var decryptor = aes.CreateDecryptor(aes.Key, aes.IV))
using (var memoryStream = new MemoryStream())
using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(encrypted, 0, encrypted.Length);
cryptoStream.FlushFinalBlock();
cryptoStream.Close();
byte[] decrypted = memoryStream.ToArray();
byte[] message = new byte[dataLength];
Array.Copy(decrypted, message, dataLength);
return message;
}
}
catch
{
return null;
}
}
}
+3 -2
View File
@@ -146,8 +146,9 @@ namespace ClientServerLib
int messageLength = BitConverter.ToInt32(data, 0);
while (messageLength > bytesRead)
{
Array.Resize<byte>(ref data, data.Length + bufferLength);
bytesRead += clientStream.Read(data, data.Length - bufferLength, bufferLength);
if (data.Length < messageLength + bufferLength)
Array.Resize<byte>(ref data, messageLength + bufferLength);
bytesRead += clientStream.Read(data, bytesRead, bufferLength);
}
byte[] message = new byte[bytesRead];
@@ -12,9 +12,9 @@ namespace Vocaluxe.Base.Server
public static class CCommands
{
private static UTF8Encoding encoder = new UTF8Encoding();
public static SHA256Managed SHA256 = new SHA256Managed();
#region Commands
public const int ResponseOK = 1;
public const int ResponseNOK = 2;
@@ -39,6 +39,10 @@ namespace Vocaluxe.Base.Server
public const int ComamndSendMouseMBUpEvent = 241;
public const int CommandSendMouseWheelEvent = 250;
public const int CommandSendAvatarPicture = 500;
public const int CommandSendAvatarPictureJpg = 501;
#endregion Commands
#region General
public static byte[] CreateCommandWithoutParams(int Command)
{
@@ -65,7 +69,36 @@ namespace Vocaluxe.Base.Server
#endregion Keyboard
#region Avatar
public static byte[] CreateCommandSendAvatarPicture(int Width, int Height, byte[] data)
{
SAvatarPicture ap = new SAvatarPicture();
ap.Height = Height;
ap.Width = Width;
ap.data = new byte[data.Length];
Array.Copy(data, ap.data, data.Length);
return Serialize<SAvatarPicture>(CommandSendAvatarPicture, ap);
}
public static bool DecodeCommandSendAvatarPicture(byte[] Message, out SAvatarPicture AvatarPicture)
{
return TryDeserialize<SAvatarPicture>(Message, out AvatarPicture);
}
public static byte[] CreateCommandSendAvatarPictureJpg(byte[] data)
{
SAvatarPicture ap = new SAvatarPicture();
ap.Height = 0;
ap.Width = 0;
ap.data = new byte[data.Length];
Array.Copy(data, ap.data, data.Length);
return Serialize<SAvatarPicture>(CommandSendAvatarPictureJpg, ap);
}
#endregion Avatar
#region Serializing
private static byte[] Serialize<T>(int Command, T obj)
{
byte[] command = BitConverter.GetBytes(Command);
@@ -108,5 +141,6 @@ namespace Vocaluxe.Base.Server
}
}
}
#endregion Serializing
}
}
@@ -6,4 +6,11 @@ namespace Vocaluxe.Base.Server
{
public byte[] SHA256;
}
public struct SAvatarPicture
{
public int Width;
public int Height;
public byte[] data;
}
}