public void SendAndRecive() { var testPackageOut = new Core.Connection.Packet(); testPackageOut.Content = JsonConvert.SerializeObject("{\"Status\":0,\"Actions\":[[],[],[],[],[],[],[],[],[],[],[],[],[{\"Parameters\":{\"CreatePosition\":{\"X\":5316345,\"Y\":3354734},\"CreateBuilding\":276},\"Type\":2}],[],[],[],[],[],[],[],[],[],[],[],[]],\"Entities\":[]}"); testPackageOut.MethodType = Core.Connection.MethodType.DoActions; var client = new TcpClient(); client.Connect( Client.Common.Constants.ClientConstants.TCP_SERVER, Client.Common.Constants.ClientConstants.TCP_PORT); var stream = client.GetStream(); testPackageOut.Send(stream); var testPackageIn = Core.Connection.Packet.Receive(stream); client.Close(); Assert.True(!client.Connected); Assert.AreNotEqual(testPackageOut.GetHashCode(), testPackageIn.GetHashCode()); }
public void DoAction() { var user = this.GetLogin(); var testAccount = new Core.Models.Account(user.AccountId); var testDefinition = new Core.Models.Definitions.Definition(276); var testPositionI = new Core.Models.PositionI(this.TestPosition()); var dictParam = new System.Collections.Generic.Dictionary<string, object>(); dictParam[Core.Controllers.Actions.CreateUnit.CREATE_POSITION] = testPositionI; dictParam[Core.Controllers.Actions.CreateUnit.CREATION_TYPE] = (long)testDefinition.SubType; var testAction = new Core.Models.Action(testAccount, Core.Models.Action.ActionType.CreateBuilding, dictParam); var testActions = new Core.Models.Action[] { testAction, }; var testActionRequest = new Core.Connections.DoActionsRequest( user.SessionID, this.TestPosition(), testActions); var testJson = JsonConvert.SerializeObject(testActionRequest); var testPackage = new Packet(); testPackage.Content = testJson; testPackage.MethodType = MethodType.DoActions; var testStream = this.GetStream(); testPackage.Send(testStream); var data = JsonConvert.DeserializeObject<Core.Connections.Response>(Packet.Receive(testStream).Content); Assert.AreEqual(Core.Connections.Response.ReponseStatus.OK, data.Status); }
public void LoadRegion() { var testLoginRequest = new Core.Connections.LoginRequest( new Core.Models.Position(100, 100), "Maria", "Musterfrau"); var testJson = JsonConvert.SerializeObject(testLoginRequest); var testPackage = new Packet(); testPackage.Content = testJson; testPackage.MethodType = MethodType.Login; var testStream = this.GetStream(); testPackage.Send(testStream); var testPackageIn = Packet.Receive(testStream); var loginData = JsonConvert.DeserializeObject<Core.Connections.LoginResponse>(testPackageIn.Content); var testRegion = new Core.Models.RegionPosition[] { new Core.Models.RegionPosition(100, 100), }; var testRequest = new Core.Connections.LoadRegionsRequest( loginData.SessionID, new Core.Models.Position(100, 100), testRegion); testJson = JsonConvert.SerializeObject(testRequest); testPackage = new Packet(); testPackage.Content = testJson; testPackage.MethodType = MethodType.LoadEntities; testStream = this.GetStream(); testPackage.Send(testStream); testPackageIn = Packet.Receive(testStream); var data = JsonConvert.DeserializeObject<Core.Connections.Response>(testPackageIn.Content); Assert.AreEqual(Core.Connections.Response.ReponseStatus.OK, data.Status); }
/// <summary> /// Gets a login instance. /// </summary> /// <returns>The login.</returns> private Core.Connections.LoginResponse GetLogin() { var testStream = this.GetStream(); var testLoginRequest = new Core.Connections.LoginRequest( this.TestPosition(), "Maria", "Musterfrau"); var testJson = JsonConvert.SerializeObject(testLoginRequest); var testPackage = new Packet(); testPackage.Content = testJson; testPackage.MethodType = MethodType.Login; testPackage.Send(testStream); return JsonConvert.DeserializeObject<Core.Connections.LoginResponse>(Packet.Receive(testStream).Content); }
public void Login() { var testLoginRequest = new Core.Connections.LoginRequest( new Core.Models.Position(8108, 15), "Maria", "Musterfrau"); var testJson = JsonConvert.SerializeObject(testLoginRequest); var testPackage = new Packet(); testPackage.Content = testJson; testPackage.MethodType = MethodType.Login; var client = new TcpClient(); client.Connect( Client.Common.Constants.ClientConstants.TCP_SERVER, Client.Common.Constants.ClientConstants.TCP_PORT); Assert.True(client.Connected); var testStream = client.GetStream(); testPackage.Send(testStream); var testPackageIn = Packet.Receive(testStream); client.Close(); Assert.True(!client.Connected); var data = JsonConvert.DeserializeObject<Core.Connections.LoginResponse>(testPackageIn.Content); Assert.AreEqual(Core.Connections.LoginResponse.ReponseStatus.OK, data.Status); Assert.IsNotNull(data.Status); Assert.IsNotNull(data.AccountId); // Test False logins testLoginRequest = new Core.Connections.LoginRequest( new Core.Models.Position(8108, 15), "Maria", "Musterfrauxxx"); testJson = JsonConvert.SerializeObject(testLoginRequest); testPackage = new Packet(); testPackage.Content = testJson; testPackage.MethodType = MethodType.Login; testStream = this.GetStream(); testPackage.Send(testStream); data = JsonConvert.DeserializeObject<Core.Connections.LoginResponse>(Packet.Receive(testStream).Content); Assert.AreNotEqual(Core.Connections.LoginResponse.ReponseStatus.OK, data.Status); }
/// <summary> /// Receives an packet from the specified stream. /// </summary> /// <param name="stream">Stream (TCP Socket Stream e.g.).</param> /// <returns>Packet which was received</returns> public static Packet Receive(Stream stream) { var packetOut = new Packet(); stream.Read(packetOut.m_byteMethodType, 0, 2); byte[] size = new byte[4]; stream.Read(size, 0, size.Length); var buffer = new byte[BitConverter.ToInt32(size, 0)]; stream.Read(buffer, 0, BitConverter.ToInt32(size, 0)); packetOut.ByteContent = buffer; return packetOut; }
/// <summary> /// Receives an packet from the specified stream. /// </summary> /// <param name="stream">Stream (TCP Socket Stream e.g.).</param> /// <returns>Packet which was received</returns> public static async Task<Packet> ReceiveAsync(Stream stream) { var packetOut = new Packet(); await stream.ReadAsync(packetOut.m_byteMethodType, 0, 2); byte[] size = new byte[4]; await stream.ReadAsync(size, 0, size.Length); var buffer = new byte[BitConverter.ToInt32(size, 0)]; await stream.ReadAsync(buffer, 0, BitConverter.ToInt32(size, 0)); packetOut.ByteContent = buffer; return packetOut; }
/// <summary> /// Starts the TCP thread. /// </summary> /// <param name="stateInfo">TCPClient Object who connected.</param> private void StartTcpThread(object stateInfo) { Console.Write("Client Thread opened.\n"); var client = (TcpClient)stateInfo; try { var stream = client.GetStream(); var packetIn = Packet.Receive(stream); if (packetIn.Content.Length <= Config.MAX_CONTENT_SIZE) { var json = string.Empty; switch (packetIn.MethodType) { case MethodType.Login: Console.Write("Client Login.\n"); json = JSONController.Login(packetIn.Content); break; case MethodType.DoActions: Console.Write("Client DoActions.\n"); json = JSONController.DoActions(packetIn.Content); break; case MethodType.LoadEntities: Console.Write("Client LoadRegions.\n"); json = JSONController.LoadRegions(packetIn.Content); break; default: Console.Write("Client Invalid Method?\n"); json = JSONController.Default(packetIn.Content); break; } var packetOut = new Packet(); packetOut.MethodType = packetIn.MethodType; packetOut.Content = json; packetOut.Send(stream); } } catch (Exception exception) { Console.Write("{0}", exception); } client.Close(); }