/// <summary> /// Writes a disconnect message into de buffer. /// </summary> /// <param name="sender"></param> /// <param name="targetMessage"></param> /// <returns></returns> public static Error.ErrorType DisconnectMessage(NetworkEntity sender, out Message targetMessage) { int bytesToSend = Message.HeaderOfMessageCommand.Length; byte[] rawBuffer = new byte[ServerSettings.ServerBufferSize]; targetMessage = null; byte[] messageHeaderContent = null; if (sender == null) { return(Error.ErrorType.InvalidNetworkEntity); } ///Writing header System.Buffer.BlockCopy(Message.HeaderOfMessageCommand, 0, rawBuffer, 0, Message.HeaderOfMessageCommand.Length); bytesToSend += 4; ///Writing the command. rawBuffer[bytesToSend] = (byte)Message.CommandType.Disconnect; bytesToSend += 1; ///Writing the EndOfMessageCommand. System.Buffer.BlockCopy(Message.EndOfMessageCommand, 0, rawBuffer, bytesToSend, Message.EndOfMessageCommand.Length); bytesToSend += EndOfMessageCommand.Length; ///Writing the message length. messageHeaderContent = System.BitConverter.GetBytes(bytesToSend); System.Buffer.BlockCopy(messageHeaderContent, 0, rawBuffer, Message.HeaderOfMessageCommand.Length, messageHeaderContent.Length); ///Creating the Message targetMessage = new ManagedMessage((CommandType)rawBuffer[Message.HeaderOfMessageCommand.Length + 4], sender); targetMessage.SetBodyMessageNoClone(rawBuffer, (uint)bytesToSend); return(Error.ErrorType.Ok); }
/// <summary> /// Creates an authentication message. **In this moment it is not complete and may change in future updates.** /// </summary> /// <param name="sender"></param> /// <param name="targetMessage"></param> /// <returns></returns> public static Error.ErrorType AuthenticationMessage(NetworkEntity sender, User userInfo, out Message targetMessage) { int bytesToSend = Message.HeaderOfMessageCommand.Length; byte[] rawBuffer = new byte[ServerSettings.ServerBufferSize]; short hashSize; targetMessage = null; byte[] messageHeaderContent = null; byte[] userBuffer = null; string stringBuffer; if (sender == null) { return(Error.ErrorType.InvalidNetworkEntity); } stringBuffer = userInfo.Username; User.EncodeUsernameToBytes(ref stringBuffer, out userBuffer); ///Writing header System.Buffer.BlockCopy(Message.HeaderOfMessageCommand, 0, rawBuffer, 0, Message.HeaderOfMessageCommand.Length); bytesToSend += 4; ///Writing the command. rawBuffer[bytesToSend] = (byte)Message.CommandType.Authentication; bytesToSend += 1; ///Writing the username's byte length. rawBuffer[bytesToSend] = (byte)userBuffer.Length; bytesToSend += 1; ///Writing the username's bytes System.Buffer.BlockCopy(userBuffer, 0, rawBuffer, bytesToSend, userBuffer.Length); bytesToSend += userBuffer.Length; ///Writing the hash's length hashSize = (short)userInfo.Hash.Length; userBuffer = null; userBuffer = System.BitConverter.GetBytes(hashSize); System.Buffer.BlockCopy(userBuffer, 0, rawBuffer, bytesToSend, userBuffer.Length); bytesToSend += userBuffer.Length; ///Writing the user's hash code. System.Buffer.BlockCopy(userInfo.Hash, 0, rawBuffer, bytesToSend, hashSize); bytesToSend += hashSize; ///Writing the EndOfMessage command. System.Buffer.BlockCopy(Message.EndOfMessageCommand, 0, rawBuffer, bytesToSend, Message.EndOfMessageCommand.Length); bytesToSend += EndOfMessageCommand.Length; messageHeaderContent = System.BitConverter.GetBytes(bytesToSend); System.Buffer.BlockCopy(messageHeaderContent, 0, rawBuffer, Message.HeaderOfMessageCommand.Length, messageHeaderContent.Length); targetMessage = new ManagedMessage((CommandType)rawBuffer[Message.HeaderOfMessageCommand.Length + 4], sender); targetMessage.SetBodyMessage(rawBuffer, (uint)bytesToSend); return(Error.ErrorType.Ok); }
public static Error.ErrorType SettingUpChatSystem(NetworkEntity sender, System.Collections.Generic.List <Chat.Group.ChatGroup> availableGroups, out Message targetMessage) { int bytesToSend = Message.HeaderOfMessageCommand.Length; byte[] rawBuffer = new byte[ServerSettings.ServerBufferSize]; short shortBuffer; targetMessage = null; byte[] messageHeaderContent = null; byte[] bytesBuffer = null; if (sender == null) { return(Error.ErrorType.InvalidNetworkEntity); } ///Writing header System.Buffer.BlockCopy(Message.HeaderOfMessageCommand, 0, rawBuffer, 0, Message.HeaderOfMessageCommand.Length); bytesToSend += 4; ///Writing the command. rawBuffer[bytesToSend] = (byte)Message.CommandType.ChatSettingUp; bytesToSend += 1; ///Writing how many chat groups are available. shortBuffer = (short)availableGroups.Count; bytesBuffer = System.BitConverter.GetBytes(shortBuffer); System.Buffer.BlockCopy(bytesBuffer, 0, rawBuffer, bytesToSend, bytesBuffer.Length); bytesToSend += bytesBuffer.Length; for (int i = 0; i < availableGroups.Count; i++) { ///Writing the group Id bytesBuffer = System.BitConverter.GetBytes(availableGroups[i].Id); System.Buffer.BlockCopy(bytesBuffer, 0, rawBuffer, bytesToSend, bytesBuffer.Length); bytesToSend += bytesBuffer.Length; ///Writing the chat name length KSPM.Globals.KSPMGlobals.Globals.StringEncoder.GetBytes(availableGroups[i].Name, out bytesBuffer); rawBuffer[bytesToSend] = (byte)bytesBuffer.Length; bytesToSend++; ///Writing the chat's name. System.Buffer.BlockCopy(bytesBuffer, 0, rawBuffer, bytesToSend, bytesBuffer.Length); bytesToSend += bytesBuffer.Length; } ///Writing the EndOfMessage command. System.Buffer.BlockCopy(Message.EndOfMessageCommand, 0, rawBuffer, bytesToSend, Message.EndOfMessageCommand.Length); bytesToSend += EndOfMessageCommand.Length; messageHeaderContent = System.BitConverter.GetBytes(bytesToSend); System.Buffer.BlockCopy(messageHeaderContent, 0, rawBuffer, Message.HeaderOfMessageCommand.Length, messageHeaderContent.Length); targetMessage = new ManagedMessage((CommandType)rawBuffer[Message.HeaderOfMessageCommand.Length + 4], sender); targetMessage.SetBodyMessage(rawBuffer, (uint)bytesToSend); return(Error.ErrorType.Ok); }
public static Error.ErrorType UDPSettingUpMessage(NetworkEntity sender, out Message targetMessage) { int bytesToSend = Message.HeaderOfMessageCommand.Length; byte[] rawBuffer = new byte[ServerSettings.ServerBufferSize]; int intBuffer; ServerSideClient ssClientReference = (ServerSideClient)sender; targetMessage = null; byte[] messageHeaderContent = null; byte[] byteBuffer; if (sender == null) { return(Error.ErrorType.InvalidNetworkEntity); } ///Writing header System.Buffer.BlockCopy(Message.HeaderOfMessageCommand, 0, rawBuffer, 0, Message.HeaderOfMessageCommand.Length); bytesToSend += 4; ///Writing the Command byte. rawBuffer[bytesToSend] = (byte)Message.CommandType.UDPSettingUp; bytesToSend += 1; ///Writing the port number. intBuffer = ((System.Net.IPEndPoint)ssClientReference.udpCollection.socketReference.LocalEndPoint).Port; byteBuffer = System.BitConverter.GetBytes(intBuffer); System.Buffer.BlockCopy(byteBuffer, 0, rawBuffer, bytesToSend, byteBuffer.Length); bytesToSend += byteBuffer.Length; ///Writintg the paring code. byteBuffer = System.BitConverter.GetBytes(ssClientReference.CreatePairingCode()); System.Buffer.BlockCopy(byteBuffer, 0, rawBuffer, bytesToSend, byteBuffer.Length); bytesToSend += byteBuffer.Length; ///Writint the EndOfMessageCommand. System.Buffer.BlockCopy(Message.EndOfMessageCommand, 0, rawBuffer, bytesToSend, Message.EndOfMessageCommand.Length); bytesToSend += EndOfMessageCommand.Length; messageHeaderContent = System.BitConverter.GetBytes(bytesToSend); System.Buffer.BlockCopy(messageHeaderContent, 0, rawBuffer, Message.HeaderOfMessageCommand.Length, messageHeaderContent.Length); targetMessage = new ManagedMessage((CommandType)rawBuffer[Message.HeaderOfMessageCommand.Length + 4], sender); targetMessage.SetBodyMessage(rawBuffer, (uint)bytesToSend); return(Error.ErrorType.Ok); }
public void SwapReceivedBufferToSend(ManagedMessage otherMessage) { System.Buffer.BlockCopy(otherMessage.OwnerNetworkEntity.ownerNetworkCollection.secondaryRawBuffer, 0, this.OwnerNetworkEntity.ownerNetworkCollection.rawBuffer, 0, (int)otherMessage.messageRawLength); this.messageRawLength = otherMessage.messageRawLength; }
/// <summary> /// Returns a new instance of the same class. /// </summary> /// <returns></returns> public override Message Empty() { ManagedMessage item = new ManagedMessage(CommandType.Null, null); return(item); }