Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


MJLaukala

Member Since 21 Jan 2011
Offline Last Active Jul 13 2012 01:44 AM

Topics I've Started

Blocking collection return the wrong byte array

10 July 2012 - 05:45 PM

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

public class MessageBuffer : BlockingCollection<Message>
	{
	}

In the class that has Listener() and ReceivedMessageHandler(object messageProcessor)

private MessageBuffer RecievedMessageBuffer;

On Thread1

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();
				}
			}
		}

Message.ReadMessage(NetworkStream stream, iTcpConnectClient client)

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;
		}

On Thread2

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();
				}
			}
		}

PlayerStateMessage.cs

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);
		}
	}

Message.GetVector2FromBuffer(int bufferlocation)
This is where the exception is thrown because this.Buffer is byte[0] when it should be byte[20].

public Vector2 GetVector2FromBuffer(int bufferlocation)
		{
			return new Vector2(
				BitConverter.ToSingle(this.Buffer, Message.HeaderSize + bufferlocation),
				BitConverter.ToSingle(this.Buffer, Message.HeaderSize + bufferlocation + 4));
		}

Multiple TcpClienton one client?

01 June 2012 - 01:21 PM

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 - http://endeavorgame.com/, 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

If statement frustration with UdpClient.Receive

29 May 2012 - 09:18 PM

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
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();
	}
}

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);
public void OnMessageRecieved(byte[] message)
{
	switch ((MessageType)message[0])
	{
		case MessageType.Error:
			break;
		case MessageType.Ping:
			this.PingResponse(message);
			break;
	}
}

[java] need help with unexpected results

31 January 2011 - 11:08 AM

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

PARTNERS