private void ProcessRequest(CameraRequest request) { //start asking the camera for a new image byte[] data = connection.MakeRequest(PropertyRequest(request)); //extract image data string imageName; byte[] imageData; ByteManipulation.SeperateData(out imageName, data, out imageData); if (imageName == "" || imageData.Length <= 0) { Console.WriteLine("No Image data recieved!!"); Console.WriteLine("Debug data:"); Console.WriteLine("\tThread Camera: " + config.Config.Id); Console.WriteLine("\tImage set id: " + ImageSetName); return; } using (FileStream fileStream = new FileStream(SavePath + imageName, FileMode.CreateNew)) { for (int i = 0; i < imageData.Length; i++) { fileStream.WriteByte(imageData[i]); } } }
public void BasicFileSerialisation() { string filePath = Path.GetPathRoot(Directory.GetCurrentDirectory()) + "scanimage" + Path.DirectorySeparatorChar + "test.jpg"; if (!File.Exists(filePath)) { byte[] data = new byte[100]; new Random().NextBytes(data); byte[] eom = Encoding.ASCII.GetBytes(Constants.EndOfMessage); Array.Copy(eom, 0, data, data.Length - eom.Length, eom.Length); File.WriteAllBytes(filePath, data); } Assert.IsTrue(File.Exists(filePath)); byte[] bytes = ByteHelpers.FileToBytes(filePath); Assert.IsTrue(ByteManipulation.SearchEndOfMessage(bytes, bytes.Length)); Assert.IsTrue(ByteManipulation.SearchEndOfMessageIndex(bytes, bytes.Length) == bytes.Length - Constants.EndOfMessage.Length - 1); File.Delete(filePath); }
public void SeperateDataHalfSeperated() { string name = "String"; byte[] data = { 12, 34, 211, 179 }; //create byte array for seperation string almostSeperator = ""; for (int i = 0; i < Constants.MessageSeperator.Length - 1; i++) { almostSeperator += Constants.MessageSeperator[i]; } name += almostSeperator; byte[] byteName = Encoding.ASCII.GetBytes(name + Constants.MessageSeperator); byte[] testData = new byte[byteName.Length + data.Length]; byteName.CopyTo(testData, 0); data.CopyTo(testData, byteName.Length); //return data string returnName; byte[] returnBytes; Assert.IsTrue(ByteManipulation.SeperateData(out returnName, testData, out returnBytes)); Assert.IsTrue(returnName.Equals(name)); Assert.IsTrue(data.Length == returnBytes.Length); for (int i = 0; i < data.Length; i++) { Assert.IsTrue(data[i] == returnBytes[i]); } }
public void SearchEndOfMessageNoData() { byte[] testData = new byte[0]; Assert.False(ByteManipulation.SearchEndOfMessage(testData, testData.Length)); Assert.True(ByteManipulation.SearchEndOfMessageIndex(testData, testData.Length) == -1); }
public void SeperateDataTestCorrect() { string name = "This is a name"; byte[] data = { 0, 23, 243, 231, 23, 234, 234, 23, 4, 4, 4, 0, 0, 234 }; //create byte array for seperation byte[] byteName = Encoding.ASCII.GetBytes(name + Constants.MessageSeperator); byte[] testData = new byte[byteName.Length + data.Length]; byteName.CopyTo(testData, 0); data.CopyTo(testData, byteName.Length); //return data string returnName; byte[] returnBytes; Assert.IsTrue(ByteManipulation.SeperateData(out returnName, testData, out returnBytes)); Assert.IsTrue(returnName.Equals(name)); Assert.IsTrue(data.Length == returnBytes.Length); for (int i = 0; i < data.Length; i++) { Assert.IsTrue(data[i] == returnBytes[i]); } }
public byte[] MakeRequest(byte[] requestData) { byte[] bytes = new byte[Constants.ByteArraySize], buffer = new byte[bufferSize]; if (!socket.Connected) { throw new Exception("Socket needs to be connnected"); } try { // Encode the data string into a byte array. socket.Send(requestData); //grab the bytes bytes = new byte[Constants.ByteArraySize]; int totalData = 0; do { int bytesRec = socket.Receive(buffer); Array.Copy(buffer, 0, bytes, totalData, bytesRec); totalData += bytesRec; } while (!ByteManipulation.SearchEndOfMessage(bytes, totalData)); return(Helpers.Networking.TrimExcessByteData(bytes, totalData - 1)); } catch (Exception e) { Console.WriteLine(e); throw e; } }
private byte[] ManipData(byte[] origData, byte[] reordering) { byte[] newData = new byte[origData.Length]; for (int x = 0; x < origData.Length; x++) { newData[x] = ByteManipulation.ReorderBits(origData[x], reordering); } return(newData); }
public void SearchEndOfMessageFalse() { byte[] garbageData = { 12, 233, 67, 255, 186, 243, 14, 15, 241, 178 }; byte[] endOfMessage = Encoding.ASCII.GetBytes(Constants.EndOfMessage.Substring(1)); byte[] testData = new byte[garbageData.Length + endOfMessage.Length]; garbageData.CopyTo(testData, 0); endOfMessage.CopyTo(testData, garbageData.Length); Assert.False(ByteManipulation.SearchEndOfMessage(testData, testData.Length)); Assert.True(ByteManipulation.SearchEndOfMessageIndex(testData, testData.Length) == -1); }
public void SeperateDataTestIncorrect() { string name = "This is a really long string that needs to be parsed one by one"; byte[] data = { 0, 23, 243, 231 }; //create byte array for seperation byte[] byteName = Encoding.ASCII.GetBytes(name + Constants.EndOfMessage); byte[] testData = new byte[byteName.Length + data.Length]; byteName.CopyTo(testData, 0); data.CopyTo(testData, byteName.Length); //return data string returnName; byte[] returnBytes; Assert.IsFalse(ByteManipulation.SeperateData(out returnName, testData, out returnBytes)); Assert.IsTrue(returnName == ""); Assert.IsTrue(returnBytes.Length == 0); }
public void ReadFile() { Random rand = new Random(); int size = rand.Next(514875, 1048576); byte[] fakeData = new byte[size]; rand.NextBytes(fakeData); string path = "C:\\scanimage" + Path.DirectorySeparatorChar + "test.txt"; if (File.Exists(path)) { File.Delete(path); } using (FileStream file = new FileStream(path, FileMode.CreateNew)) { for (int i = 0; i < fakeData.Length; i++) { file.WriteByte(fakeData[i]); } } byte[] methodData = ByteHelpers.FileToBytes(path), fileData = new byte[size]; string name = ""; Assert.True(ByteManipulation.SeperateData(out name, methodData, out methodData, Constants.MessageSeperator)); Array.Copy(methodData, fileData, methodData.Length - Constants.EndOfMessage.Length); Assert.True(name == Path.DirectorySeparatorChar + "test.txt"); Assert.True(fileData.Length == fakeData.Length); for (int i = 0; i < fakeData.Length; i++) { Assert.True(fakeData[i] == methodData[i]); } File.Delete(path); }
public void LongData(int qty) { socket.Connected = true; socket.FailCount = 1; socket.byteCount = 0; socket.maxSend = 0; byte[] raw, socketData; do { raw = new byte[qty]; new Random().NextBytes(raw); byte[] EOM = Encoding.ASCII.GetBytes(Constants.EndOfMessage); socketData = new byte[raw.Length + EOM.Length]; Array.Copy(raw, socketData, raw.Length); Array.Copy(EOM, 0, socketData, raw.Length, EOM.Length); } while (ByteManipulation.SearchEndOfMessageIndex(socketData, socketData.Length) != raw.Length - 1); socket.ReturnData = socketData; byte[] netData = net.MakeRequest(new byte[] { 22, 88, 45 }); Assert.AreEqual(socket.ReturnData, netData); }
private byte getRealBankNo(int sequentialBankNo, byte[] romBankNoReordering) { return(ByteManipulation.ReorderBits((byte)sequentialBankNo, romBankNoReordering, true)); }
private void InitializeClientConnection(object data) { CyberFuck.Logger.Log("Network", "initializing new client"); Connection conn = (Connection)data; NetworkStream stream = conn.stream; try { //get the player name byte[] length = { 0 }; stream.Read(length, 0, 1); byte[] buffer = new byte[length[0]]; stream.Read(buffer, 0, length[0]); char[] chars = new char[length[0]]; System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder(); int charLen = d.GetChars(buffer, 0, length[0], chars, 0); string name = new System.String(chars); //send the world bitmap to the client: Tile[,] worldMap = world.Map.tileMap; BinaryFormatter formatter = new BinaryFormatter(); using (MemoryStream memmory = new MemoryStream()) { formatter.Serialize(memmory, worldMap); long size = memmory.Length; Console.WriteLine(size); byte[] bsize = BitConverter.GetBytes(size); byte[] image = memmory.ToArray(); stream.Write(bsize, 0, bsize.Length); stream.Write(image, 0, image.Length); } //send entities data (not players): byte[][] entitiesBytes = new byte[world.Entities.Count][]; List <IEntity> entities = world.Entities; for (int i = 0; i < entities.Count; i++) { entitiesBytes[i] = new EntityData(entities[i]).Encode(); } byte[] entitiesData = ByteManipulation.ConcatByteArrays(entitiesBytes); // send the length of the entities data, send the number of entities and then send the entities themselves byte[] entitiesMsg = ByteManipulation.ConcatByteArrays(BitConverter.GetBytes(entitiesData.Length), BitConverter.GetBytes(entities.Count), entitiesData); stream.Write(entitiesMsg, 0, entitiesMsg.Length); //send all players data: List <Player> players = world.Players.Values.ToList(); byte[][] playersBytes = new byte[world.Players.Count][]; for (int i = 0; i < players.Count; i++) { byte[] byData = System.Text.Encoding.ASCII.GetBytes(players[i].Name); var l = new byte[] { (byte)byData.Length }; playersBytes[i] = ByteManipulation.ConcatByteArrays(new PlayerData(players[i]).Encode(), l, byData); } byte[] playersData = ByteManipulation.ConcatByteArrays(playersBytes); // send the length of the players data, send the number of players and then send the players themselves byte[] playersMsg = ByteManipulation.ConcatByteArrays(BitConverter.GetBytes(playersData.Length), BitConverter.GetBytes(players.Count), playersData); stream.Write(playersMsg, 0, playersMsg.Length); // after aproval of the client, send it its position // the client will send 0xFFFFFFFF to approve the connection CyberFuck.Logger.Log("Network", "waiting for client's approval..."); byte[] approval = new byte[4]; stream.Read(approval, 0, 4); if (!approval.All((b) => b == 0xFF)) { CyberFuck.Logger.Log("Network", "Client connection failed - bad approval message"); return; } // create a player for the client and // send the client its position Player clientPlayer = new Player(world, conn.id, name); byte[] clientsPlayerBytes = new PlayerData(clientPlayer).Encode(); stream.Write(BitConverter.GetBytes(clientsPlayerBytes.Length), 0, sizeof(int)); stream.Write(clientsPlayerBytes, 0, clientsPlayerBytes.Length); CyberFuck.Logger.Log("Network", "player with id " + conn.id + " added"); world.LoadPlayer(clientPlayer); conn.State = ConnectionState.Connected; } catch (Exception e) { conn.Close(); } }