/// <summary> /// Returns the Uuid from a series of bytes /// </summary> /// <param name="bytes"></param> /// <param name="offset"></param> /// <returns></returns> public static string BytesToUuid(AppendableByteArray bytes, int offset) { byte[] uuid = bytes.GetSubarray(offset, UUID_LEN); ASCIIEncoding encoding = new ASCIIEncoding(); return encoding.GetString(uuid); }
/// <summary> /// Sets up the data in the header /// </summary> /// <param name="type"></param> /// <param name="assetUUID"></param> /// <param name="dataSize"></param> private void SetupHeader(RequestType type, string assetUUID, int dataSize) { _data = new AppendableByteArray(HEADER_SIZE + dataSize); _data.Append((byte)type); _data.Append(Util.UuidToAscii(assetUUID)); _data.Append(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(dataSize))); //size }
/// <summary> /// Returns the Uuid from a series of bytes /// </summary> /// <param name="bytes"></param> /// <param name="offset"></param> /// <returns></returns> public static string BytesToUuid(AppendableByteArray bytes, int offset) { byte[] uuid = bytes.GetSubarray(offset, UUID_LEN); ASCIIEncoding encoding = new ASCIIEncoding(); return(encoding.GetString(uuid)); }
/// <summary> /// Initializes a new asset object with the given data /// </summary> /// <param name="dataSize"></param> public Asset(AppendableByteArray data) { _uuid = Util.BytesToUuid(data, 0); _type = data.data[TYPE_TAG_LOC]; _local = data.data[LOCAL_TAG_LOC] == 1; _temporary = data.data[TEMPORARY_TAG_LOC] == 1; _createTime = Util.NTOHL(data.data, CREATE_TIME_TAG_LOC); //now the dynamic sized fields UTF8Encoding encoding = new UTF8Encoding(); //the name field byte nameFieldSize = data.data[NAME_SIZE_TAG_LOC]; if (nameFieldSize > 0) { _name = encoding.GetString(data.data, NAME_SIZE_TAG_LOC + 1, nameFieldSize); } else { _name = String.Empty; } //the description field int descSizeFieldLoc = NAME_SIZE_TAG_LOC + nameFieldSize + 1; byte descFieldSize = data.data[descSizeFieldLoc]; if (descFieldSize > 0) { _description = encoding.GetString(data.data, descSizeFieldLoc + 1, descFieldSize); } else { _description = String.Empty; } //finally, get the location of the data and it's size int dataSizeFieldLoc = descSizeFieldLoc + descFieldSize + 1; int dataSize = Util.NTOHL(data.data, dataSizeFieldLoc); int dataLoc = dataSizeFieldLoc + 4; //create the array now so that it will be shared between all reqestors if (dataSize > 0) { _data = data.GetSubarray(dataLoc, dataSize); } else { _data = new byte[0]; } }
public ServerResponseMsg(Socket conn) { //read the header _header.AppendFromSocket(conn, HEADER_SIZE); int intStatus = (int)_header.data[0]; if (intStatus < (int)Result.FOUND || intStatus > (int)Result.OK) { throw new AssetProtocolError("Invalid result type in server response: " + GetHeaderSummary()); } //read the data int dataSize = Util.NTOHL(_header.data, DATA_SZ_TAG_LOC); if (dataSize > MAX_DATA_SIZE || dataSize < 0) { throw new AssetProtocolError("Returned data was too long in response: " + GetHeaderSummary()); } _data = new AppendableByteArray(dataSize); _data.AppendFromSocket(conn, dataSize); }
/// <summary> /// Constructs a new authentication response /// </summary> /// <param name="challenge">The challenge from the server</param> /// <param name="password">The password for the server</param> public AuthResponse(AuthChallenge challenge, string password) { //convert the password to ascii ASCIIEncoding encoding = new ASCIIEncoding(); byte[] asciiPW = encoding.GetBytes(password); //get data from the challenge byte[] challengeBytes = challenge.Challenge; //add the two ranges together and compute the hash AppendableByteArray authString = new AppendableByteArray(asciiPW.Length + challengeBytes.Length); authString.Append(asciiPW); authString.Append(challengeBytes); SHA1 sha = new SHA1CryptoServiceProvider(); byte[] challengeHash = sha.ComputeHash(authString.data); //copy the results to the raw packet data _rawMessageData.Append(PACKET_IDENTIFIER); _rawMessageData.Append(encoding.GetBytes(Util.HashToHex(challengeHash))); }
public AppendableByteArray Serialize() { UTF8Encoding encoding = new UTF8Encoding(); byte[] nameBytes = encoding.GetBytes(_name); byte[] descBytes = encoding.GetBytes(_description); if (nameBytes.Length > 255) { throw new AssetProtocolError(String.Format("Serialized asset name would be too long after encoding {0} {1}", _name, _uuid)); } if (descBytes.Length > 255) { throw new AssetProtocolError(String.Format("Serialized asset description would be too long after encoding {0} {1}", _description, _uuid)); } //see the packet diagram to understand where the size calculation is coming from AppendableByteArray retArray = new AppendableByteArray(HEADER_SIZE + 1 + nameBytes.Length + 1 + descBytes.Length + 4 + _data.Length); retArray.Append(Util.UuidToAscii(_uuid)); retArray.Append((byte)_type); retArray.Append((byte)(_local ? 1 : 0)); retArray.Append((byte)(_temporary ? 1 : 0)); retArray.Append(Util.HTONL(_createTime)); retArray.Append((byte)nameBytes.Length); retArray.Append(nameBytes); retArray.Append((byte)descBytes.Length); retArray.Append(descBytes); retArray.Append(Util.HTONL(_data.Length)); retArray.Append(_data); return(retArray); }
public AppendableByteArray Serialize() { UTF8Encoding encoding = new UTF8Encoding(); byte[] nameBytes = encoding.GetBytes(_name); byte[] descBytes = encoding.GetBytes(_description); if (nameBytes.Length > 255) { throw new AssetProtocolError(String.Format("Serialized asset name would be too long after encoding {0} {1}", _name, _uuid)); } if (descBytes.Length > 255) { throw new AssetProtocolError(String.Format("Serialized asset description would be too long after encoding {0} {1}", _description, _uuid)); } //see the packet diagram to understand where the size calculation is coming from AppendableByteArray retArray = new AppendableByteArray(HEADER_SIZE + 1 + nameBytes.Length + 1 + descBytes.Length + 4 + _data.Length); retArray.Append(Util.UuidToAscii(_uuid)); retArray.Append((byte)_type); retArray.Append((byte)(_local ? 1 : 0)); retArray.Append((byte)(_temporary ? 1 : 0)); retArray.Append(Util.HTONL(_createTime)); retArray.Append((byte)nameBytes.Length); retArray.Append(nameBytes); retArray.Append((byte)descBytes.Length); retArray.Append(descBytes); retArray.Append(Util.HTONL(_data.Length)); retArray.Append(_data); return retArray; }