WCF-Server inital commit (Wenserver without usermanagement and with some pages)

This commit is contained in:
lukeIam committed 2013-12-13 17:34:07 +01:00
1 parent 2f6002e932
commit 28da5b776f
31 files changed
+1371 -438

No files matched your search

+30 -12
View File
@@ -1,18 +1,18 @@
#region license
// This file is part of Vocaluxe.
#region license
// This file is part of Vocaluxe.
//
// Vocaluxe is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Vocaluxe is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Vocaluxe is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Vocaluxe is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Vocaluxe. If not, see <http://www.gnu.org/licenses/>.
// You should have received a copy of the GNU General Public License
// along with Vocaluxe. If not, see <http://www.gnu.org/licenses/>.
#endregion
using System;
@@ -305,6 +305,16 @@ namespace Vocaluxe.Base
return list.ToArray();
}
public static CProfile GetProfile(int profileID)
{
if (!IsProfileIDValid(profileID))
{
return null;
}
return _Profiles[profileID];
}
public static IEnumerable<CAvatar> GetAvatars()
{
if (_Avatars.Count == 0)
@@ -480,6 +490,14 @@ namespace Vocaluxe.Base
return _Profiles[profileID].Avatar.ID;
}
public static CAvatar GetAvatar(int profileID)
{
if (!IsProfileIDValid(profileID))
return null;
return _Profiles[profileID].Avatar;
}
public static int GetProfileID(int num)
{
return _Profiles[num].ID;
+13 -12
View File
@@ -1,18 +1,18 @@
#region license
// This file is part of Vocaluxe.
#region license
// This file is part of Vocaluxe.
//
// Vocaluxe is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Vocaluxe is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Vocaluxe is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Vocaluxe is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Vocaluxe. If not, see <http://www.gnu.org/licenses/>.
// You should have received a copy of the GNU General Public License
// along with Vocaluxe. If not, see <http://www.gnu.org/licenses/>.
#endregion
using System;
@@ -102,6 +102,7 @@ namespace Vocaluxe.Base
#endif
Path.Combine(DataPath, FolderProfiles)
};
public static string FolderPhotos = "Photos";
public const string FolderSongs = "Songs";
public const string FolderSounds = "Sounds";
public const string FolderLanguages = "Languages";
+10 -10
View File
@@ -1,4 +1,4 @@
#region license
#region license
// This file is part of Vocaluxe.
//
// Vocaluxe is free software: you can redistribute it and/or modify
@@ -138,7 +138,7 @@ namespace Vocaluxe.Base.Server
stream.Write(cmd, 0, cmd.Length);
byte[] data;
string json = JsonConvert.SerializeObject(obj);
string json = JsonConvert.SerializeObject(obj);
data = _Encoder.GetBytes(json);
stream.Write(data, 0, data.Length);
return stream.ToArray();
@@ -156,16 +156,16 @@ namespace Vocaluxe.Base.Server
var data = new byte[message.Length - 4];
Array.Copy(message, 4, data, 0, data.Length);
try
{
try
{
obj = JsonConvert.DeserializeObject<T>(_Encoder.GetString(data));
return true;
}
catch
{
return false;
return true;
}
catch
{
return false;
}
}
#endregion Serializing
}
}
}
+265 -132
View File
@@ -1,18 +1,18 @@
#region license
// This file is part of Vocaluxe.
#region license
// This file is part of Vocaluxe.
//
// Vocaluxe is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Vocaluxe is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Vocaluxe is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Vocaluxe is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Vocaluxe. If not, see <http://www.gnu.org/licenses/>.
// You should have received a copy of the GNU General Public License
// along with Vocaluxe. If not, see <http://www.gnu.org/licenses/>.
#endregion
using System;
@@ -20,7 +20,9 @@ using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Linq;
using System.IO;
using System.Text;
using System.Windows.Forms;
using ClientServerLib;
using Vocaluxe.Lib.Input;
@@ -40,8 +42,23 @@ namespace Vocaluxe.Base.Server
public static void Init()
{
_Clients = new Dictionary<int, CClientHandler>();
String pw = (CConfig.ServerEncryption == EOffOn.TR_CONFIG_ON) ? CConfig.ServerPassword : String.Empty;
_Server = new CServer(RequestHandler, CConfig.ServerPort, pw);
if (CConfig.ServerEncryption == EOffOn.TR_CONFIG_ON)
{
_Server = new CServer(CConfig.ServerPort);
//_Server = new CServer(RequestHandler, CConfig.ServerPort, CConfig.ServerPassword);
}
else
{
_Server = new CServer(CConfig.ServerPort);
//_Server = new CServer(RequestHandler, CConfig.ServerPort, String.Empty);
}
CServer.SendKeyEvent = sendKeyEvent;
CServer.GetProfileData = getProfileData;
CServer.SendProfileData = sendProfileData;
CServer.GetProfileList = getProfileList;
CServer.SendPhoto = sendPhoto;
CServer.GetSiteFile = getSiteFile;
_Discover = new CDiscover(CConfig.ServerPort, CCommands.BroadcastKeyword);
Controller.Init();
@@ -63,147 +80,263 @@ namespace Vocaluxe.Base.Server
_Clients = new Dictionary<int, CClientHandler>();
}
private static byte[] RequestHandler(int connectionID, byte[] message)
private static bool sendKeyEvent(string key)
{
byte[] answer = CCommands.CreateCommandWithoutParams(CCommands.ResponseNOK);
if (message == null)
return answer;
if (message.Length < 4)
return answer;
bool loggedIn;
lock (_Clients)
bool result = false;
if (key != null && key != "")
{
if (!_Clients.ContainsKey(connectionID))
switch (key.ToLower())
{
var client = new CClientHandler(connectionID);
_Clients.Add(connectionID, client);
//just a small fix: There is no real login at the moment, so set it to true.
_Clients[connectionID].LoggedIn = true;
}
loggedIn = _Clients[connectionID].LoggedIn;
}
if (!loggedIn)
return CCommands.CreateCommandWithoutParams(CCommands.ResponseNOK);
int command = BitConverter.ToInt32(message, 0);
switch (command)
{
case CCommands.CommandSendKeyUp:
answer = CCommands.CreateCommandWithoutParams(CCommands.ResponseOK);
Controller.AddKeyEvent(new SKeyEvent(ESender.Keyboard, false, false, false, false, Char.MinValue, Keys.Up));
break;
case CCommands.CommandSendKeyDown:
answer = CCommands.CreateCommandWithoutParams(CCommands.ResponseOK);
Controller.AddKeyEvent(new SKeyEvent(ESender.Keyboard, false, false, false, false, Char.MinValue, Keys.Down));
break;
case CCommands.CommandSendKeyLeft:
answer = CCommands.CreateCommandWithoutParams(CCommands.ResponseOK);
Controller.AddKeyEvent(new SKeyEvent(ESender.Keyboard, false, false, false, false, Char.MinValue, Keys.Left));
break;
case CCommands.CommandSendKeyRight:
answer = CCommands.CreateCommandWithoutParams(CCommands.ResponseOK);
Controller.AddKeyEvent(new SKeyEvent(ESender.Keyboard, false, false, false, false, Char.MinValue, Keys.Right));
break;
case CCommands.CommandSendAvatarPicture:
SAvatarPicture avatarPicture;
if (CCommands.DecodeCommandSendAvatarPicture(message, out avatarPicture))
{
using (var bmp = new Bitmap(avatarPicture.Width, avatarPicture.Height))
case "up":
Controller.AddKeyEvent(new SKeyEvent(ESender.Keyboard, false, false, false, false, Char.MinValue, Keys.Up));
result = true;
break;
case "down":
Controller.AddKeyEvent(new SKeyEvent(ESender.Keyboard, false, false, false, false, Char.MinValue, Keys.Down));
result = true;
break;
case "left":
Controller.AddKeyEvent(new SKeyEvent(ESender.Keyboard, false, false, false, false, Char.MinValue, Keys.Left));
result = true;
break;
case "right":
Controller.AddKeyEvent(new SKeyEvent(ESender.Keyboard, false, false, false, false, Char.MinValue, Keys.Right));
result = true;
break;
case "escape":
Controller.AddKeyEvent(new SKeyEvent(ESender.Keyboard, false, false, false, false, Char.MinValue, Keys.Escape));
result = true;
break;
case "return":
Controller.AddKeyEvent(new SKeyEvent(ESender.Keyboard, false, false, false, false, Char.MinValue, Keys.Return));
result = true;
break;
case "tab":
Controller.AddKeyEvent(new SKeyEvent(ESender.Keyboard, false, false, false, false, Char.MinValue, Keys.Tab));
result = true;
break;
default:
foreach (char c in key.ToCharArray())
{
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
Marshal.Copy(avatarPicture.Data, 0, bmpData.Scan0, avatarPicture.Data.Length);
bmp.UnlockBits(bmpData);
const string filename = "snapshot";
int i = 0;
while (File.Exists(Path.Combine(CSettings.DataPath, CSettings.FolderProfiles, filename + i + ".png")))
i++;
bmp.Save(Path.Combine(CSettings.DataPath, CSettings.FolderProfiles, filename + i + ".png"), ImageFormat.Png);
}
answer = CCommands.CreateCommandWithoutParams(CCommands.ResponseOK);
}
break;
case CCommands.CommandSendAvatarPictureJpg:
SAvatarPicture avatarPictureJpg;
if (CCommands.DecodeCommandSendAvatarPicture(message, out avatarPictureJpg))
{
if (_AddAvatar(avatarPictureJpg.Data) != String.Empty)
answer = CCommands.CreateCommandWithoutParams(CCommands.ResponseOK);
}
break;
case CCommands.CommandSendProfile:
SProfile profile;
if (CCommands.DecodeCommandSendProfile(message, out profile))
{
try
{
string avatarFilename = _AddAvatar(profile.Avatar.Data);
if (avatarFilename != String.Empty)
bool shift = true;
char keyChar;
if (char.ToUpper(c) != c)
{
var p = new CProfile
{
Active = EOffOn.TR_CONFIG_ON,
AvatarFileName = avatarFilename,
Difficulty = (EGameDifficulty)profile.Difficulty,
GuestProfile = EOffOn.TR_CONFIG_OFF,
PlayerName = profile.PlayerName
};
CProfiles.AddProfile(p);
answer = CCommands.CreateCommandWithoutParams(CCommands.ResponseOK);
keyChar = char.ToUpper(c);
shift = false;
}
else
{
keyChar = c;
}
if ((keyChar >= 0x30 && keyChar <= 0x39) || (keyChar >= 0x41 && keyChar <= 0x5A))
{
Keys keys = (Keys)keyChar;
if (shift)
{
keys |= Keys.Shift;
}
Controller.AddKeyEvent(new SKeyEvent(ESender.Keyboard, false, false, false, false, Char.MinValue, keys));
result = true;
}
}
catch (Exception) {}
}
break;
break;
}
}
return answer;
return result;
}
private static string _AddAvatar(byte[] jpgData)
#region profile
private static ProfileData getProfileData(int profileId)
{
CProfile profile = CProfiles.GetProfile(profileId);
if (profile == null)
{
return new ProfileData();
}
return CreateProfileData(profile); ;
}
private static bool sendProfileData(ProfileData profile)
{
CProfile newProfile;
CProfile existingProfile = CProfiles.GetProfile(profile.ProfileId);
if (existingProfile != null)
{
newProfile = new CProfile
{
Active = existingProfile.Active,
AvatarFileName = existingProfile.AvatarFileName,
Avatar = existingProfile.Avatar,
Difficulty = existingProfile.Difficulty,
GuestProfile = existingProfile.GuestProfile,
PlayerName = existingProfile.PlayerName
};
}
else
{
newProfile = new CProfile
{
Active = EOffOn.TR_CONFIG_ON,
GuestProfile = EOffOn.TR_CONFIG_OFF
};
}
if (profile.Avatar != null)
{
newProfile.Avatar = _AddAvatar(profile.Avatar);
}
else if (newProfile.Avatar == null)
{
//TODO:standardAvatar
}
if (profile.PlayerName != null && profile.PlayerName != "")
{
newProfile.PlayerName = profile.PlayerName;
}
else if (newProfile.PlayerName != null && newProfile.PlayerName != "")
{
newProfile.PlayerName = "DummyName";
}
if (profile.Difficulty >= 0 && profile.Difficulty <= 2)
{
newProfile.Difficulty = (EGameDifficulty)profile.Difficulty;
}
if (existingProfile != null)
{
CProfiles.EditProfile(newProfile);
}
else
{
CProfiles.AddProfile(newProfile);
}
return true;
}
private static ProfileData[] getProfileList()
{
List<ProfileData> result = new List<ProfileData>(CProfiles.NumProfiles);
foreach (CProfile profile in CProfiles.GetProfiles())
{
result.Add(CreateProfileData(profile));
}
return result.ToArray();
}
private static ProfileData CreateProfileData(CProfile profile)
{
ProfileData profileData = new ProfileData();
profileData.IsEditable = true; //TODO: set correctly
profileData.PlayerName = profile.PlayerName;
profileData.Type = (int)profile.GuestProfile;
profileData.Difficulty = (int)profile.Difficulty;
CAvatar avatar = profile.Avatar;
if (avatar != null)
{
if (File.Exists(avatar.FileName))
{
Image avatarImage = Image.FromFile(avatar.FileName);
//TODO: Convert??? and Compress?
profileData.Avatar = new Base64Image(avatarImage, avatarImage.RawFormat);
}
}
return profileData;
}
private static CAvatar _AddAvatar(Base64Image avatarData)
{
string result = String.Empty;
try
{
string filename = Path.Combine(CSettings.DataPath, CSettings.FolderProfiles, "snapshot");
int i = 0;
while (File.Exists(filename + i + ".jpg"))
i++;
filename = filename + i + ".jpg";
FileStream fs = File.Create(filename);
fs.Write(jpgData, 0, jpgData.Length);
fs.Flush();
fs.Close();
string filename = _SaveImage(avatarData, "snapshot", CSettings.FolderProfiles);
var avatar = new CAvatar(-1);
if (avatar.LoadFromFile(filename))
{
CProfiles.AddAvatar(avatar);
result = filename;
return avatar;
}
else
{
return null;
}
}
catch {}
catch
{
return null;
}
}
return result;
#endregion
#region photo
private static bool sendPhoto(PhotoData photoData)
{
if (photoData.Photo == null)
{
return false;
}
string name = DateTime.Now.ToString("yyyy-MM-DD-hh-mm-ss");
string filePath = _SaveImage(photoData.Photo, name, CSettings.FolderPhotos);
return (filePath != null && filePath != "");
}
#endregion
#region website
private static byte[] getSiteFile(string filename)
{
string path = "Website/" + filename;
path = path.Replace("..", "");
if (!File.Exists(path))
{
return null;
}
/*string content = File.ReadAllText(path);
content = content.Replace("%SERVER%", System.Net.Dns.GetHostName() + ":" + CConfig.ServerPort);
return Encoding.UTF8.GetBytes(content);*/
return File.ReadAllBytes(path);
}
#endregion
private static string _SaveImage(Base64Image imageDate, string name, string folder)
{
Image avatarImage = imageDate.getImage();
string extension = imageDate.getImageType();
string filename = Path.Combine(folder, name);
int i = 0;
while (File.Exists(filename + "_" + i + "." + extension))
{
i++;
}
filename = filename + "_" + i + "." + extension;
avatarImage.Save(filename);
return filename;
}
}
}
+2 -2
View File
@@ -22,7 +22,7 @@ namespace Vocaluxe.Base.Server
public byte[] Sha256;
}
public struct SAvatarPicture
public struct SPicture
{
public int Width;
public int Height;
@@ -31,7 +31,7 @@ namespace Vocaluxe.Base.Server
public struct SProfile
{
public SAvatarPicture Avatar;
public SPicture Avatar;
public string PlayerName;
public int Difficulty;
}