Big endian in non blocking socket (cpp)

119 views
Skip to first unread message

Pipiska INCorporatius

unread,
May 24, 2015, 9:28:50 AM5/24/15
to haxe...@googlegroups.com
Hi mates! I'm trying to make Haxe game server on cpp platform. I would be glad if someone answer me a couple of questions:

1) When you use non-blocking sockets you wait for incoming data regulary using Socket.select() function but it returns you only an array of Sockets, and in game server every socket must be linked with it's own player class. So every time I have data to read I have to sort out all my clients in search for matching socket and it could be difficult if there are many connections at a time, 200 for example. In C++ I used to solve this problem by calling recv() for every socket and checking if there is any data to read, but in Haxe it will cause an Eof exception. Is there any other way to handle this I may be missing?

2)There is a function:

haxe.io.bytesData.getUInt32()

which reads 32bit uinteger from buffer as it says in the description, in low endian encoding. Is there a way to read it in big endian somehow? The only function I can use in socket class is Socket.input.readBytes() so after I call it I have to read all data from haxe.io.bytesData structure.

Juraj Kirchheim

unread,
May 24, 2015, 10:18:49 AM5/24/15
to haxe...@googlegroups.com
1) Use the `custom` field to help sorting things out. The easiest thing would be to just store an ID: http://api.haxe.org/sys/net/Socket.html#custom


Best,
Juraj

--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.

Pipiska INCorporatius

unread,
May 24, 2015, 11:02:26 AM5/24/15
to haxe...@googlegroups.com
back2dos0

2)I know that socket class has Input member, but it make no difference whether you set it on bigEndian or not because you use only input.readBytes() function and
what it  does  it  copy a number of bytes to haxe.io.Bytes class from which you read them calling readInt32(). So bigEndian property of a socket class
 would work only when you use Socket.input.readInt32() but not when you call it from haxe.io.Bytes class. And Bytes class does not have this property.

Pipiska INCorporatius

unread,
May 24, 2015, 11:05:44 AM5/24/15
to haxe...@googlegroups.com
I forgot to mention that your first point with ID is actually very helpful and thank you man that helped me much.

John Plsek

unread,
May 24, 2015, 11:27:27 AM5/24/15
to haxe...@googlegroups.com
an ID is useful, but you can do so much more with custom, see std/cpp/net/ThreadServer.hx for an example

On 25 May 2015 at 01:05, Pipiska INCorporatius <vbi...@mail.ru> wrote:
I forgot to mention that your first point with ID is actually very helpful and thank you man that helped me much.

--

Pipiska INCorporatius

unread,
May 24, 2015, 11:47:38 AM5/24/15
to haxe...@googlegroups.com
ThreadServer.hx uses blocking sockets and threads, it is not what I want

Juraj Kirchheim

unread,
May 24, 2015, 12:14:33 PM5/24/15
to haxe...@googlegroups.com
Ok, I understand your problem now. Just create a `BytesInput` over those exact bytes:

```
var input = new BytesInput(bytes);
input.bigEndian = true;

function readBE32(offset:Int) } { 
   input.position = offset;
   return input.readInt32();
}
```

Generally you might find life easier using inputs rather than bytes. But you can do everything by hand of course ;)

Best,
Juraj

Pipiska INCorporatius

unread,
May 24, 2015, 12:53:50 PM5/24/15
to haxe...@googlegroups.com
Using inputs would be easier no doubt but function Socket.input.readBytes() takes Bytes class as an input parameter. And using your method I have to create a new instance of BytesInput class every time I have data incoming, it's sad.

Juraj Kirchheim

unread,
May 24, 2015, 1:22:56 PM5/24/15
to haxe...@googlegroups.com
I still don't get why you don't just call `socket.input.readInt32()` and be done with it, but I guess that will take time to explain.

Yes, using my proposal you will have to create one BytesInput per Bytes instance. That's a very reasonable overhead. But if you must, you can of course just read that 32 bit integer by hand ;)

Best,
Juraj

On Sun, May 24, 2015 at 6:53 PM, Pipiska INCorporatius <vbi...@mail.ru> wrote:
Using inputs would be easier no doubt but function Socket.input.readBytes() takes Bytes class as an input parameter. And using your method I have to create a new instance of BytesInput class every time I have data incoming, it's sad.

--

Pipiska INCorporatius

unread,
May 24, 2015, 1:36:46 PM5/24/15
to haxe...@googlegroups.com
Explaining:

socket.input.readInt32() would cause an error on non-blocking socket because there is no way to figure out how much data I have to accquire (like Socket.bytesAvailable in AS3) and if there is only one or two bytes to read, not four, it will cause an error e:Wouldblock or Eof. The only function for reading data from nonblocking socket in Haxe is readBytes(), it won't cause an error it just return you number of bytes you have read. It's hard to explain especially if you know English like I do (very well).

John Plsek

unread,
May 24, 2015, 6:22:58 PM5/24/15
to haxe...@googlegroups.com

way to completely miss the point, go ahead store your little id in custom, then use some sort of lookup table when you need to use it.

On 25/05/2015 1:47 AM, "Pipiska INCorporatius" <vbi...@mail.ru> wrote:
ThreadServer.hx uses blocking sockets and threads, it is not what I want

--

Pipiska INCorporatius

unread,
May 24, 2015, 7:25:45 PM5/24/15
to haxe...@googlegroups.com
Don't be too proud of that technological terror you have proposed. The ability to store an ID in custom field is insignificant next to the power of storing a pointer on entire client class though.

John Plsek

unread,
May 24, 2015, 11:31:23 PM5/24/15
to haxe...@googlegroups.com
exactly my point

On 25 May 2015 at 09:25, Pipiska INCorporatius <vbi...@mail.ru> wrote:
Don't be too proud of that technological terror you have proposed. The ability to store an ID in custom field is insignificant next to the power of storing a pointer on entire client class though.

--

MJ

unread,
May 27, 2015, 2:02:25 PM5/27/15
to haxe...@googlegroups.com
HI Pipiska INCorporatius, 
 I also have been thinking to work on Haxe game server ( for cpp target) . My previous experience include some games using flash based servers ( or they were for flash in the begining) such as Electroserver and Smartfox. Is this project only for you or for some other company ? If you want , you can join forces for someting.

Pipiska INCorporatius

unread,
Jun 1, 2015, 8:29:04 AM6/1/15
to haxe...@googlegroups.com
    Hi MJ. I am making a project for myself with educational purposes. I have some expirience making servers for flash games on c++, and now, when I have discovered a new brand language Haxe, I am making an attempt to write client and server both using it. If you have some interesting projects to propose I am glad to join.
Reply all
Reply to author
Forward
0 new messages