public static async void InitializeDatabase(string filename) { Database.filename = filename; try { using (SqliteConnection db = new SqliteConnection("Filename=" + filename)) { await db.OpenAsync(); string tableCommand = "CREATE TABLE IF NOT " + "EXISTS penguins (ID INTEGER PRIMARY KEY AUTOINCREMENT, " + "Username VARCHAR(24) NULL, " + "Password BINARY(60) NULL, " + "Randkey VARCHAR(48) NULL)"; SqliteCommand createTable = new SqliteCommand(tableCommand, db); await createTable.ExecuteNonQueryAsync(); } } catch (SqliteException ex) { await MServer.LogMessage("WARN", "Database", "Database encountered the following error while establishing a connection: " + ex); Debug.WriteLine(ex); } }
protected virtual async void StreamSocketListener_ConnectionReceived(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args) { Debug.WriteLine("Accepting penguin"); Penguin penguin = new Penguin(this, args.Socket); await MServer.LogMessage("INFO", server_name, "Accepting new penguin"); await AddPenguin(penguin); using (var stream = args.Socket.InputStream.AsStreamForRead()) { while (penguins.ContainsKey(args.Socket)) { byte[] data = new byte[256]; int numBytesRead = await stream.ReadAsync(data, 0, data.Length); if (numBytesRead == 0) { await RemovePenguin(penguin); return; } String message = Encoding.UTF8.GetString(data).Substring(0, numBytesRead); Stack <string> packets = new Stack <string>(message.Split('\0')); packets.Pop(); foreach (string packet in packets) { Debug.WriteLine("[RECV] " + packet); await HandlePacket(penguin, packet); } } } }
public async void StartServer() { try { listener = new StreamSocketListener(); listener.ConnectionReceived += StreamSocketListener_ConnectionReceived; await listener.BindServiceNameAsync(port); await MServer.LogMessage("FINE", server_name, "Server is running on port " + port + " (allow this port in firewall)"); } catch (Exception ex) { SocketErrorStatus webErrorStatus = SocketError.GetStatus(ex.GetBaseException().HResult); await MServer.LogMessage("FATAL", server_name, "Server failed to start. Ensure that another service is not running on port " + port + "."); Debug.WriteLine(server_name + " failed to start."); if (MServer.IsStarted()) { await MServer.StopServers(); } } await LoopServer(); }
public async Task Send(string message) { Debug.WriteLine("[SENT] " + message); await MServer.LogMessage("INFO", parent.server_name, "Sent: " + message); message = message + "\0"; byte[] data = Encoding.UTF8.GetBytes(message); await socket.OutputStream.WriteAsync(data.AsBuffer()); }
protected override async Task RemovePenguin(Penguin penguin) { if (penguins.ContainsKey(penguin.socket)) { await MServer.LogMessage("INFO", server_name, "Removing penguin"); await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { Debug.WriteLine("Removing penguin"); penguins.Remove(penguin.socket); MServer.penguins.Remove(penguin); latestPackets.Remove(penguin); }); } }
private async Task HandlePacket(Penguin penguin, string packet) { await MServer.LogMessage("INFO", server_name, "Received: " + packet); string start = packet.Substring(0, 1); switch (start) { case "<": await StoreXmlPacket(penguin, packet); break; case "%": // await StorePlayPacket(penguin, packet); break; } return; }
public async Task StopServer() { List <Penguin> penguinsToRemove = new List <Penguin>(); foreach (KeyValuePair <StreamSocket, Penguin> penguin in penguins) { penguinsToRemove.Add(penguin.Value); } foreach (Penguin penguin in penguinsToRemove) { Debug.WriteLine("Penguins:"); Debug.WriteLine(penguin); } foreach (Penguin penguin in penguinsToRemove) { await RemovePenguin(penguin); } await MServer.LogMessage("FINE", server_name, "Server " + server_name + " has been stopped."); listener.Dispose(); }
private async Task HandleRequest(StreamSocket socket, string packet) { string[] request = packet.Split(' '); try { if (request[0] == "GET") { switch (request[1]) { case "/": await GenerateBoots(socket, MServer.GetCurrentLoginPort(), MServer.GetCurrentWorldNamesAndPorts()); return; case "/LICENSE": await GenerateBootsLicense(socket); return; case "/register": await RegisterGet(socket); return; } if (!request[1].Contains('.')) { await SendResponse(socket, Encoding.UTF8.GetBytes("mCPPS"), "text/html"); return; } List <String> folders = request[1].Split('/').ToList(); string filename = folders[folders.Count - 1]; string extension = filename.Split('.')[1]; folders.RemoveAt(0); folders.RemoveAt(folders.Count - 1); if (!allowed_extensions.ContainsKey(extension)) { await SendResponse(socket, Encoding.UTF8.GetBytes("mCPPS"), "text/html"); return; } /* Check if file is cached locally. * If it is not, download it from the master cache_server first. * If it is, send it to user from cache. */ try { StorageFile file = await local_folder.GetFileAsync("Www\\" + request[1].Replace("/", "\\")); await ReplyFileData(socket, file, allowed_extensions[extension]); } catch (FileNotFoundException) { try { HttpClient client = new HttpClient(); byte[] buffer = await client.GetByteArrayAsync(cache_server + request[1]); StorageFolder tempfolder = await local_folder.GetFolderAsync("Www"); foreach (string folder in folders) { try { tempfolder = await tempfolder.GetFolderAsync(folder); } catch (Exception) { await tempfolder.CreateFolderAsync(folder); tempfolder = await tempfolder.GetFolderAsync(folder); } } StorageFile file = await tempfolder.CreateFileAsync(filename); using (Stream stream = await file.OpenStreamForWriteAsync()) stream.Write(buffer, 0, buffer.Length); await ReplyFileData(socket, file, allowed_extensions[extension]); } catch (Exception) { await SendResponse(socket, Encoding.UTF8.GetBytes("mCPPS"), "text/html"); } } } else if (request[0] == "POST") { switch (request[1]) { case "/register": await RegisterPost(socket); return; } } } catch (Exception) { await SendResponse(socket, Encoding.UTF8.GetBytes("mCPPS"), "text/html"); } }