MJLaukala

Members
  • Content count

    12
  • Joined

  • Last visited

Community Reputation

110 Neutral

About MJLaukala

  • Rank
    Member
  1. I am having an issue where an object with a byte[20] is being passed into a BlockingCollection on one thread and another thread returning the object with a byte[0] using BlockingCollection.Take(). I think this is a threading issue but I do not know where or why this is happening considering that BlockingCollection is a concurrent collection. Sometimes on thread2, myclass2.mybytes equals byte[0]. Any information on how to fix this is greatly appreciated. MessageBuffer.cs [CODE] public class MessageBuffer : BlockingCollection<Message> { }[/CODE] In the class that has Listener() and ReceivedMessageHandler(object messageProcessor) [CODE]private MessageBuffer RecievedMessageBuffer;[/CODE] On Thread1 [CODE] private void Listener() { while (this.IsListening) { try { Message message = Message.ReadMessage(this.Stream, this); if (message != null) { this.RecievedMessageBuffer.Add(message); } } catch (IOException ex) { if (!this.Client.Connected) { this.OnDisconnected(); } else { Logger.LogException(ex.ToString()); this.OnDisconnected(); } } catch (Exception ex) { Logger.LogException(ex.ToString()); this.OnDisconnected(); } } }[/CODE] Message.ReadMessage(NetworkStream stream, iTcpConnectClient client) [CODE] public static Message ReadMessage(NetworkStream stream, iTcpConnectClient client) { int ClassType = -1; Message message = null; try { ClassType = stream.ReadByte(); if (ClassType == -1) { return null; } if (!Message.IDTOCLASS.ContainsKey((byte)ClassType)) { throw new IOException("Class type not found"); } message = Message.GetNewMessage((byte)ClassType); message.Client = client; message.ReadData(stream); if (message.Buffer.Length < message.MessageSize + Message.HeaderSize) { return null; } } catch (IOException ex) { Logger.LogException(ex.ToString()); throw ex; } catch (Exception ex) { Logger.LogException(ex.ToString()); //throw ex; } return message; }[/CODE] On Thread2 [CODE] private void ReceivedMessageHandler(object messageProcessor) { if (messageProcessor != null) { while (this.IsListening) { Message message = this.RecievedMessageBuffer.Take(); message.Reconstruct(); message.HandleMessage(messageProcessor); } } else { while (this.IsListening) { Message message = this.RecievedMessageBuffer.Take(); message.Reconstruct(); message.HandleMessage(); } } }[/CODE] PlayerStateMessage.cs [CODE] public class PlayerStateMessage : Message { public GameObject PlayerState; public override int MessageSize { get { return 12; } } public PlayerStateMessage() : base() { this.PlayerState = new GameObject(); } public PlayerStateMessage(GameObject playerState) { this.PlayerState = playerState; } public override void Reconstruct() { this.PlayerState.Poisiton = this.GetVector2FromBuffer(0); this.PlayerState.Rotation = this.GetFloatFromBuffer(8); base.Reconstruct(); } public override void Deconstruct() { this.CreateBuffer(); this.AddToBuffer(this.PlayerState.Poisiton, 0); this.AddToBuffer(this.PlayerState.Rotation, 8); base.Deconstruct(); } public override void HandleMessage(object messageProcessor) { ((MessageProcessor)messageProcessor).ProcessPlayerStateMessage(this); } }[/CODE] Message.GetVector2FromBuffer(int bufferlocation) This is where the exception is thrown because this.Buffer is byte[0] when it should be byte[20]. [CODE] public Vector2 GetVector2FromBuffer(int bufferlocation) { return new Vector2( BitConverter.ToSingle(this.Buffer, Message.HeaderSize + bufferlocation), BitConverter.ToSingle(this.Buffer, Message.HeaderSize + bufferlocation + 4)); }[/CODE]
  2. Does putting 2 different TcpClienton one client make sense if you are using different ports? Can the both receive data simultaneously? I've been working on a game called Endeavor - [url="http://endeavorgame.com/"]http://endeavorgame.com/[/url], There are some messages that are very large and would take some time to complete the data transfer. Probably only a matter of a couple hundred milliseconds but I don't want those messages to interrupt the flow of the game. I was thinking about sending constant data which is usually represented by large chunks of data through one TcpClient and the data that changes often which isn't a lot of data at all through another. Thinking about it, if I just implement a simple prediction algorithm, I should be fine with one TcpClient and no real noticeable problems. Still, if you know the answer to my original question, I would still really like to know. -Thanks
  3. yes, I am going to use a ThreadPool. I was just doing a test run when I came across this error. I've been doing some research into passing arrays as parameters. Because they are reference type variables passed by value, any changes made to the original value or parameterized value will change the in memory value. My next thoughts were to send the array to another function and then start the thread there but because we're still dealing with a reference type, we will have the same issue. What options do I have at this point that might work better than message.CopyTo()? I feel like creating a new array every time a message is received is going to be pretty resource intensive as well. It would work fine for testing purposes but I would prefer a system that is optimized for speed.
  4. I have an if statement to catch empty messages that are sent from the server via UDP. Nothing is done with these messages. This part of my program is not complete but that's not what concerns me. What does concern my is the fact that some how, empty messages are being send to a function that will throw an error if an empty message is received. Please tell me what I am doing wrong. I've been baffled by this for hours. Obviously, this code it listening for messages [CODE] while (this.IsListening) { message = this.UDPClient.Receive(ref sourceIPE); if (sourceIPE.Address.Equals(this.IPE.Address) && message.Length > Message.HeaderSize) { ThreadStart Starter = delegate { this.HandleMessage(message); }; Thread handlerThread = new Thread(Starter); handlerThread.Start(); } } [/CODE] this code handles the received messages. "switch ((MessageType)message[0])" throws and error every now and then because message.Length is 0. I followed that stack back to ThreadStart Starter = delegate { this.HandleMessage(message); }; This is the only place in my code that calls OnMessageRecieved(byte[] message); [CODE] public void OnMessageRecieved(byte[] message) { switch ((MessageType)message[0]) { case MessageType.Error: break; case MessageType.Ping: this.PingResponse(message); break; } } [/CODE]
  5. [quote name='Danny02' timestamp='1296672963' post='4768655'] mm, you know that java have no pointers? All calls with objects are call by reference and all primitive calls are call by value [/quote] Maybe not explicitly but they are there.If they weren't, then this code would work on an update. [code] this._PreviousKeyState = this._CurrentKeyState; this._CurrentKeyState = this.keyboard; [/code] I found that if you write [code] this._PreviousKeyState = this._CurrentKeyState; this._CurrentKeyState = this.keyboard; [/code] in the constructor only, that both [color="#1C2837"][size="2"]this._PreviousKeyState and [/size][/color][color="#1C2837"][size="2"]this._CurrentKeyState will always equal [/size][/color][color="#1C2837"][size="2"]this.keyboard even when [/size][/color][color="#1C2837"][size="2"]this.keyboard's value is changed and [/size][/color][color="#1C2837"][size="2"]this._PreviousKeyState and [/size][/color][color="#1C2837"][size="2"]this._CurrentKeyState was never told to equal [/size][/color][color="#1C2837"][size="2"]this.keyboard after the statement is the constructor.[/size][/color]
  6. I figured out why it's not working! The problem is pointers. Now I just need to figure out how to solve the problem. I'll look for a topic on not using pointers and if I can't find one, I'll make a new topic. Yeah, I understand now, why my code didn't work. Pointers are no fun to work with but it's ok. I think I can make this work to my advantage!!
  7. [quote name='Red27' timestamp='1296610619' post='4768263'] TBH, I've no idea why you wouldn't simply implement the standard KeyListener and then use then keyPressed() and keyReleased() methods. Store the boolean status of your key presses (true for pressed, false for released) and then action accordingly. No disrespect, but your methid seems overly complicated and considerably extra work for no real benefit. [/quote] I'm building a highly reusable input manager. What you suggest is essentially what I am doing. The only difference is that I am using enums for the key states instead of booleans.
  8. [quote name='Danny02' timestamp='1296609855' post='4768253'] [quote name='MJLaukala' timestamp='1296591151' post='4768103'] [code] protected KeyboardState getState() { KeyboardState state = new KeyboardState(); state = this; return state; } [/code] [/quote] Sens, this doesn't make any. some thoughts, favor composition over inheritance, use inheritance as sparse as possible(interfaces nearly always better). A Keyboard Object should have a KeyStatus object and a KeyStatus object should have many Key objects. Beside that, throw all your code away and just use the standart java libs. [/quote] I changed [code] protected KeyboardState getState() { KeyboardState state = new KeyboardState(); state = this; return state; } [/code] to [code] protected KeyboardState getState() { return this; } [/code] Yeah, that was kinda extra code that was not needed. I use interfaces where I don't need classes. I am building a reusable Game Engine. The problem is not weather I am using interfaces or classes. the problem is that for some unknown reason, my code doesn't work and i'm not getting any compile errors or runtime errors.
  9. [quote name='Angex' timestamp='1296589054' post='4768081'] Can I ask what class library "Keyboard" & "KeyboardState" are from? (Maybe they're your own implementation ?). I don't recognise them. To identify the problem we need to know what "this._keyboard.getState()" actually returns.(As Wooh explained). [/quote] They are my own implementation. I am fairly new to Java. I am a C# programmer and I wanted to try my hand at something new. Keyboard extends KeyboardState. Keyboard.getState() returns KeyboardState as such: [code] protected KeyboardState getState() { KeyboardState state = new KeyboardState(); state = this; return state; } [/code] KeyboardState extends a class called Keys which contains a variable(class Key) for each key. Key holds a private Enum called KeyState that is accessed by a getter and setter method. KeyState holds 2 states; Pressed and Released.
  10. [quote name='Wooh' timestamp='1296494203' post='4767552'] because the code says so [code]this._PreviousKeyState = this._CurrentKeyState;[/code] and if this._keyboard.getState() always returns the same KeyboardState they will stay the same. [/quote] The thing is though, this._keyboard.getState() isn't always the same. All my keys work. I need this code to work so I can actually select the menu item that I want to select. Because this code is not working properly, I connot tell my selection method to change the selection just once per key press.
  11. public class InputManager { private Keyboard _keyboard = new Keyboard(); private Mouse _mouse = new Mouse(); private KeyboardState _CurrentKeyState = new KeyboardState(); private MouseState _CurrentMouseState = new MouseState(); private KeyboardState _PreviousKeyState = new KeyboardState(); private MouseState _PreviousMouseState = new MouseState(); public KeyboardState CurrentKeyState() { return this._CurrentKeyState; } public MouseState CurrentMouseState() { return this._CurrentMouseState; } public KeyboardState PreviousKeyState() { return this._PreviousKeyState; } public MouseState PreviousMouseState() { return this._PreviousMouseState; }............ public void Update() { this._PreviousKeyState = this._CurrentKeyState; this._PreviousMouseState = this._CurrentMouseState; this._CurrentKeyState = this._keyboard.getState(); this._CurrentMouseState = this._mouse; if (this._CurrentKeyState._0.getKeyState() == KeyState.Pressed && this._PreviousKeyState._0.getKeyState() == KeyState.Released) { System.out.println("hello"); } } } [/code] Can someone please tell me why "this._PreviousKeyState" is always equal to "this._CurrentKeyState"? ps. Keyboard extends KeyboardState
  12. Java programmer here. Actually started about 2 weeks ago. It was really easy to get a grasp on after programming with C# for the past 2 years. So happy they have a similar syntax. I was getting pretty tired of the .net framework when I found out that I would need a license to sell my game if I used it, I said F that. Minecraft is what made me want to use Java. I looked into C++ but decided that it's more work than I want to tackle at the moment. I figure i could get a simple game or 2 out with Java and then take a look at C++. I have an incredible idea that has been inspired from Minecraft. I'm actually about a week away from completing my first applet game.