static void Main(string[] args) { // UDP testing HostInformation hostInformation = new HostInformation("DE98F9", "127.0.0.1", "Markus", 0.0f, true); byte[] byteArray = HostInformation.Serialize(hostInformation); HostInformation deserialized = HostInformation.Deserialize(byteArray); NetworkComponents.UDPBroadcaster broadcaster = new NetworkComponents.UDPBroadcaster(); NetworkComponents.UDPReceiver receiver = new NetworkComponents.UDPReceiver(null); broadcaster.Broadcast(hostInformation); // TCP testing //NetworkComponents.TCPConnector hostConnector = new NetworkComponents.TCPConnector(OnMesageReceivedFromClient, true); //while (!(hostConnector.Listen())) //{ //} //hostConnector.Send(hostInformation); //hostConnector.Close(); UserInformation user = new UserInformation("Markus", "127.0.0.1"); byte[] data = UserInformation.Serialize(user); user = UserInformation.Deserialize(data); JoinRequest request = new JoinRequest(user, PasswordFunctions.HashPassword("penis1337")); data = JoinRequest.Serialize(request); request = JoinRequest.Deserialize(data); NetworkComponents.TCPConnector hostConnector = new NetworkComponents.TCPConnector(OnMesageReceivedFromClient, false); while (!(hostConnector.AcceptConnection("127.0.0.1"))) { } hostConnector.Send(hostInformation); hostConnector.Close(); }
// Serialize public static byte[] Serialize(UserInformation message) { MemoryStream ms = new MemoryStream(); CustomSerializer.SerializeString(ms, "IGridforceMessage"); CustomSerializer.SerializeString(ms, "UserInformation"); CustomSerializer.SerializeString(ms, message.name); CustomSerializer.SerializeString(ms, message.ipAddress); return ms.ToArray(); }
// Deserialize public static JoinRequest Deserialize(byte[] data) { int pos = 0; string interfaceName = null; string className = null; try { interfaceName = CustomSerializer.DeserializeString(data, ref pos); className = CustomSerializer.DeserializeString(data, ref pos); } catch (IGridforceMessageDeserializeException) { throw new IGridforceMessageDeserializeException("The data array doesn't represent a JoinRequest instance"); } if (interfaceName != "IGridforceMessage" || className != "JoinRequest") throw new IGridforceMessageDeserializeException("The data array doesn't represent a JoinRequest instance"); string hashedPassword = null; UserInformation userInformation = null; try { hashedPassword = CustomSerializer.DeserializeString(data, ref pos); byte[] userInformationArray = new byte[data.Length - pos]; Array.Copy(data, pos, userInformationArray, 0, data.Length - pos); userInformation = UserInformation.Deserialize(userInformationArray); } catch (IGridforceMessageDeserializeException) { throw new IGridforceMessageDeserializeException("The data array doesn't represent a JoinRequest instance"); } return new JoinRequest(userInformation, ASCIIEncoding.ASCII.GetBytes(hashedPassword)); }
// Constructor public JoinRequest(UserInformation userInformation, byte[] hashedPassword) { this.userInformation = userInformation; this.hashedPassword = hashedPassword; }