Re: Wrong pointer calculation without casting on struct

2015-02-05 Thread tcak via Digitalmars-d-learn

On Friday, 6 February 2015 at 03:59:51 UTC, tcak wrote:

I am on 64-bit Linux.

I defined a struct that it 8 bytes in total.

align(1) struct MessageBase{
align(1):
ushort qc;
ushort wc;
ushort id;
ushort contentLength;
void[0] content;
}


I defined a function in this struct that tries to set a pointer 
to contentLength field.


writeln( Without: , (this + id.offsetof) );
writeln( With   : , (cast(size_t)this + id.offsetof) );

Results:
Without: 774F5030
With   : 140737342558228

0x774F5030 = 140737342558256


As it is seen, there is 28 bytes of difference between them. 
What is this behaviour exactly?


By the way,

writeln(Base Normal: , this);
writeln(Base Cast  : , cast(size_t)this);

Result:
Base Normal: 774F5010
Base Cast  : 140737342558224

0x774F5010 = 140737342558224

These are same. So, the issue is about addition process.


ubyte array to uint?

2015-02-05 Thread Gan via Digitalmars-d-learn

Is there a simple way of conversion? Something like:
uint length = to!uint(buffer[0 .. 4]);

Right now I have:
uint length = *cast(uint*)buffer[0 .. 4].ptr;

Which I'm not entirely sure is the correct way to do that.


Re: Trying to make a TCP server, client connects and disconnects immediately

2015-02-05 Thread Gan via Digitalmars-d-learn

On Friday, 6 February 2015 at 01:36:17 UTC, Mike Parker wrote:

On 2/6/2015 9:50 AM, Gan wrote:
On Friday, 6 February 2015 at 00:35:12 UTC, Adam D. Ruppe 
wrote:

On Friday, 6 February 2015 at 00:31:37 UTC, Gan wrote:
Or am I misunderstanding the receive function? Does it send 
whole

messages or just message chunks?


It sends as much as it can when you call it. So if there's 
only 12
bytes available when you send it with a 4096 buffer, it will 
fill the
first twelve bytes (and return 12) so you can slice it 
buffer[0 ..

returnedValue] to get the data.

If there's more available than the buffer will hold, that 
data will be
held on to until next time you call receive. If 5000 bytes 
come off
the network, it will return 4096 the first time, then next 
time

through the loop, it will return the remaining 904.


How can you distinguish between different packets that get 
sent? Won't

they all be merged to a giant blob of data?


You need to give each of your packets a header. At a minimum 
you'll want a message ID and message length. When a message 
comes in, you use the length field to determine where one 
packet ends and the next one begins.


Oh sweet. Though if one message length is off by even 1 byte, 
then all future messages get corrupted?


Re: Wrong pointer calculation without casting on struct

2015-02-05 Thread Baz via Digitalmars-d-learn

On Friday, 6 February 2015 at 04:10:08 UTC, tcak wrote:

On Friday, 6 February 2015 at 03:59:51 UTC, tcak wrote:

I am on 64-bit Linux.

I defined a struct that it 8 bytes in total.

align(1) struct MessageBase{
align(1):
ushort qc;
ushort wc;
ushort id;
ushort contentLength;
void[0] content;
}


I defined a function in this struct that tries to set a 
pointer to contentLength field.


writeln( Without: , (this + id.offsetof) );
writeln( With   : , (cast(size_t)this + id.offsetof) );

Results:
Without: 774F5030
With   : 140737342558228

0x774F5030 = 140737342558256


As it is seen, there is 28 bytes of difference between them. 
What is this behaviour exactly?


By the way,

writeln(Base Normal: , this);
writeln(Base Cast  : , cast(size_t)this);

Result:
Base Normal: 774F5010
Base Cast  : 140737342558224

0x774F5010 = 140737342558224

These are same. So, the issue is about addition process.


I've tried this;

import std.stdio;

struct MessageBase{
align(1):
ushort qc;
ushort wc;
ushort id;
ushort contentLength;
void[0] content;

void a(){
writefln(%.8X,(cast(void*)this + id.offsetof));
writefln(%.8X,(cast(size_t)this + id.offsetof));
}
}


void main()
{
MessageBase mb;
mb.a;
}

and got the same value for both. Your problem is:

typeof(this).stringof is MessageBase* so with ptr arithmetic 
when you add 1 you actually shift by 1 * MessageBase.sizeof.


typeof(cast(void*)this) is void* so with ptr arithmetic you 
shift by a certain number of bytes.


Re: Trying to make a TCP server, client connects and disconnects immediately

2015-02-05 Thread Andre Kostur via Digitalmars-d-learn

On 2015-02-05, 8:17 PM, Gan wrote:

On Friday, 6 February 2015 at 01:36:17 UTC, Mike Parker wrote:

On 2/6/2015 9:50 AM, Gan wrote:

On Friday, 6 February 2015 at 00:35:12 UTC, Adam D. Ruppe wrote:

On Friday, 6 February 2015 at 00:31:37 UTC, Gan wrote:

Or am I misunderstanding the receive function? Does it send whole
messages or just message chunks?


It sends as much as it can when you call it. So if there's only 12
bytes available when you send it with a 4096 buffer, it will fill the
first twelve bytes (and return 12) so you can slice it buffer[0 ..
returnedValue] to get the data.

If there's more available than the buffer will hold, that data will be
held on to until next time you call receive. If 5000 bytes come off
the network, it will return 4096 the first time, then next time
through the loop, it will return the remaining 904.


How can you distinguish between different packets that get sent? Won't
they all be merged to a giant blob of data?


You need to give each of your packets a header. At a minimum you'll
want a message ID and message length. When a message comes in, you use
the length field to determine where one packet ends and the next one
begins.


Oh sweet. Though if one message length is off by even 1 byte, then all
future messages get corrupted?


Yep.  You're using a reliable stream-based protocol.  If you want 
discrete messages, you can use UDP.  Though it is an unreliable 
packet-oriented protocol.  Don't foul up your message lengths


Wrong pointer calculation without casting on struct

2015-02-05 Thread tcak via Digitalmars-d-learn

I am on 64-bit Linux.

I defined a struct that it 8 bytes in total.

align(1) struct MessageBase{
align(1):
ushort qc;
ushort wc;
ushort id;
ushort contentLength;
void[0] content;
}


I defined a function in this struct that tries to set a pointer 
to contentLength field.


writeln( Without: , (this + id.offsetof) );
writeln( With   : , (cast(size_t)this + id.offsetof) );

Results:
Without: 774F5030
With   : 140737342558228

0x774F5030 = 140737342558256


As it is seen, there is 28 bytes of difference between them. What 
is this behaviour exactly?


Re: ubyte array to uint?

2015-02-05 Thread weaselcat via Digitalmars-d-learn

On Friday, 6 February 2015 at 05:18:45 UTC, Gan wrote:

Is there a simple way of conversion? Something like:
uint length = to!uint(buffer[0 .. 4]);

Right now I have:
uint length = *cast(uint*)buffer[0 .. 4].ptr;

Which I'm not entirely sure is the correct way to do that.


Hi,

check out std.bitmanip.read

http://dlang.org/phobos/std_bitmanip.html#.read


Re: D + Solaris

2015-02-05 Thread Suliman via Digitalmars-d-learn

ldc supports solaris/x86


but druntime/Phobos support will most likely be lacking what 
does it's mean? It is not fully work or what?




Re: Do you have a better way to remove element from a array?

2015-02-05 Thread Tobias Pankrath via Digitalmars-d-learn

On Thursday, 5 February 2015 at 13:25:37 UTC, FrankLike wrote:

Now I can remove element from a array:

module removeOne;
import std.stdio;
import std.array;
import std.algorithm;

void main()
{
   int[] aa =[1,2,3,4,5];

 aa = aa[0..2] ~aa[3..$];
 writeln(aa); //ok
  remove(aa,1);
 writeln(aa);//get error result
}

You will found the error result,why?

Thank you.


Works as designed: 
http://dlang.org/phobos/std_algorithm.html#.remove




Do you have a better way to remove element from a array?

2015-02-05 Thread FrankLike via Digitalmars-d-learn

Now I can remove element from a array:

module removeOne;
import std.stdio;
import std.array;
import std.algorithm;

void main()
{
   int[] aa =[1,2,3,4,5];

 aa = aa[0..2] ~aa[3..$];
 writeln(aa); //ok
  remove(aa,1);
 writeln(aa);//get error result
}

You will found the error result,why?

Thank you.


Re: Syntax for checking if an element exists in a list

2015-02-05 Thread Nicholas Wilson via Digitalmars-d-learn
On Thursday, 5 February 2015 at 13:31:21 UTC, Nicholas Wilson 
wrote:

On Thursday, 5 February 2015 at 12:31:31 UTC, Stéphane wrote:

Syntax for checking if an element exists in a list

Hello,

  I would like to know if there is a better (easier to wite,
easier to read, easier to understand) way to check if an 
element

(string) is
in a list of strings.

  Here are the possibilities I see today, as someone who is new
to D:

1) using a switch

char [] myopt=get_an_option();

switch(myopt) {
case opt1, opt2, opt3:
  do_A();
  break;
case opt4:
  do_B();
  break;
default:
  do_C();
}


Note that D does NOT have default fall through. i.e.
 switch(myopt)
{
case opt1, opt2, opt3:
do_A();
break;
case opt4:
do_B();
break;
default:
do_C();
 }
is equivalent to
switch(myopt)
{
case opt1, opt2, opt3:
do_A();
case opt4:
do_B();
default:
do_C();
 }
to enable fall through use goto case; / goto case n;  to fall 
through once (to the next case) and to case n (where n is the 
case label identifier i.e. 5 or foo  or mylabel: )

switch(myopt)
{
case opt1, opt2, opt3:
do_A();
goto case;
case opt4:
do_B();
default:
do_C();
 }


PPS: Where should I post, when I have such questions and 
problems

as the one in my previous paragraph? I did not find any meta
forum.


learn is the correct  place for such questions and in general 
for questions about how to do something or enquire as to why 
something is not working or not working the way you think it 
should.


Announcement is for announcements.
debuggers and ide are for debuggers and ide.

digitalmars.D is for discussion about the language the 
libraries and links to interesting stuff (for some value of 
stuff)


the D.gnu and D.ldc are for discussions/queries about gdc and 
ldc (the gcc and llvm backend compilers)


addendum
case n:
...
break;
exists only for the ease of transliteration of C(++) where the 
break statement is required.

How ever
break label;
is still useful and used in D. usually used to break from 
multiple in a single statement.


Re: What am I doing Wrong (OpenGL SDL)

2015-02-05 Thread Mike Parker via Digitalmars-d-learn

On 2/5/2015 4:53 PM, Entity325 wrote:

On Thursday, 5 February 2015 at 07:23:15 UTC, drug wrote:

Look at this
https://github.com/drug007/geoviewer/blob/master/src/sdlapp.d
I used here SDL and OpenGL and it worked. Ctor of SDLApp creates SDL
window with OpenGL context, may be it helps.


Tested your code. Symbols still not being loaded, though it might be
wise to borrow some of your organizational conventions.

Just for kicks I had a friend try to run the executable I produced, and
she said she got exactly the same output I did, so it's definitely
something that gets compiled into the program. A bug report has been
posted on Github, including the full source code(which I probably did
completely wrong) and a screenshot of the output.


I've already replied on github [1], but for anyone else following this 
thread -- the symbols actually are being loaded. The ones for 
DerelictGL3 anyway. It's a different problem entirely.


https://github.com/DerelictOrg/DerelictGL3/issues/29


Re: Do you have a better way to remove element from a array?

2015-02-05 Thread Tobias Pankrath via Digitalmars-d-learn

On Thursday, 5 February 2015 at 13:55:59 UTC, FrankLike wrote:
On Thursday, 5 February 2015 at 13:29:30 UTC, Tobias Pankrath 
wrote:


Works as designed: 
http://dlang.org/phobos/std_algorithm.html#.remove


Thank you.
aa = remove(aa,1);//ok

but how to remove one item?
such as aa.remove(2) ?


I don't get your question.


Re: Syntax for checking if an element exists in a list

2015-02-05 Thread Tobias Pankrath via Digitalmars-d-learn

import std.algorithm;

int main(string[] options)
{
// true if the first option given to this program is 
either foo, bar, or baz.

if(options[1].canFind(foo, bar, baz))
return 0;
return 1;
}


Re: how can I get a reference of array?

2015-02-05 Thread FG via Digitalmars-d-learn

On 2015-02-05 at 09:58, bearophile wrote:

zhmt:


Will arr.ptr change in the future?

As the array add more members , it need more memroy, then remalloc may be 
called, the pointer maybe change, then the stored pointer will be invalid.

Will this happen?


Yes, it can happen.


Therefore, don't use arr.ptr directly, but always access it via arr or a.arr.

class A { public int[] * arr; }
int[] arr;
A a = new A;
a.arr = arr;
... // lots of adding to arr
... // and yet still, a.arr == arr

Even when the array in the memory gets reallocated by adding to arr, causing 
arr.length and arr.ptr to be updated, the arr struct itself will remain in the 
same spot, so pointers to it, including a.arr, are valid. (Unless arr goes out 
of scope before a, in which case you would be in deep trouble.)


Re: Syntax for checking if an element exists in a list

2015-02-05 Thread Nicholas Wilson via Digitalmars-d-learn

On Thursday, 5 February 2015 at 12:31:31 UTC, Stéphane wrote:

Syntax for checking if an element exists in a list

Hello,

   I would like to know if there is a better (easier to wite,
easier to read, easier to understand) way to check if an element
(string) is
in a list of strings.

   Here are the possibilities I see today, as someone who is new
to D:

1) using a switch

char [] myopt=get_an_option();

switch(myopt) {
case opt1, opt2, opt3:
   do_A();
   break;
case opt4:
   do_B();
   break;
default:
   do_C();
}


Note that D does NOT have default fall through. i.e.
 switch(myopt)
{
case opt1, opt2, opt3:
do_A();
break;
case opt4:
do_B();
break;
default:
do_C();
 }
is equivalent to
switch(myopt)
{
case opt1, opt2, opt3:
do_A();
case opt4:
do_B();
default:
do_C();
 }
to enable fall through use goto case; / goto case n;  to fall 
through once (to the next case) and to case n (where n is the 
case label identifier i.e. 5 or foo  or mylabel: )

switch(myopt)
{
case opt1, opt2, opt3:
do_A();
goto case;
case opt4:
do_B();
default:
do_C();
 }


PPS: Where should I post, when I have such questions and 
problems

as the one in my previous paragraph? I did not find any meta
forum.


learn is the correct  place for such questions and in general for 
questions about how to do something or enquire as to why 
something is not working or not working the way you think it 
should.


Announcement is for announcements.
debuggers and ide are for debuggers and ide.

digitalmars.D is for discussion about the language the libraries 
and links to interesting stuff (for some value of stuff)


the D.gnu and D.ldc are for discussions/queries about gdc and ldc 
(the gcc and llvm backend compilers)


Re: Do you have a better way to remove element from a array?

2015-02-05 Thread FrankLike via Digitalmars-d-learn
On Thursday, 5 February 2015 at 13:29:30 UTC, Tobias Pankrath 
wrote:


Works as designed: 
http://dlang.org/phobos/std_algorithm.html#.remove


Thank you.
aa = remove(aa,1);//ok

but how to remove one item?
such as aa.remove(2) ?


Re: Do you have a better way to remove element from a array?

2015-02-05 Thread bearophile via Digitalmars-d-learn

Tobias Pankrath:

Works as designed: 
http://dlang.org/phobos/std_algorithm.html#.remove


Unfortunately it's one of the worst designed functions of Phobos:
https://issues.dlang.org/show_bug.cgi?id=10959

Bye,
bearophile


Re: Syntax for checking if an element exists in a list

2015-02-05 Thread Stéphane
On Thursday, 5 February 2015 at 12:35:03 UTC, Tobias Pankrath 
wrote:

import std.algorithm;

if(options[1].canFind(foo, bar, baz))


 This looks quite OK. Thank you, I did not know about that 
possibility.


Re: D + Solaris

2015-02-05 Thread John Colvin via Digitalmars-d-learn

On Thursday, 5 February 2015 at 14:51:58 UTC, suliman wrote:
My next job will be related with prorgamming under Solaris. I 
dont know version but suppose it will be something pretty out 
of date.


Once i had seen topic about D and Solaris, but i want to ask if 
D solaris version is ready for production or it is alpha? Does 
anybody have experience with it?


ldc supports solaris/x86


Re: Do you have a better way to remove element from a array?

2015-02-05 Thread bachmeier via Digitalmars-d-learn

On Thursday, 5 February 2015 at 14:09:10 UTC, bearophile wrote:

Tobias Pankrath:

Works as designed: 
http://dlang.org/phobos/std_algorithm.html#.remove


Unfortunately it's one of the worst designed functions of 
Phobos:

https://issues.dlang.org/show_bug.cgi?id=10959

Bye,
bearophile


It seems your argument is that remove is poorly designed because 
it's not destructive. Or am I missing your argument?


Re: how can I get a reference of array?

2015-02-05 Thread zhmt via Digitalmars-d-learn

The behavior of array is more or less unpredictable.

If it is always a reference like class , it will be more 
predictable.


Re: how can I get a reference of array?

2015-02-05 Thread zhmt via Digitalmars-d-learn
Sorry, I misunderstand the meaning of array pointer, it is not 
equals to arr.ptr.


The array pointer meets my need perfectly .

Ignore my replies above, Sorry!!!



Re: What am I doing Wrong (OpenGL SDL)

2015-02-05 Thread drug via Digitalmars-d-learn

On 05.02.2015 10:53, Entity325 wrote:

On Thursday, 5 February 2015 at 07:23:15 UTC, drug wrote:

Look at this
https://github.com/drug007/geoviewer/blob/master/src/sdlapp.d
I used here SDL and OpenGL and it worked. Ctor of SDLApp creates SDL
window with OpenGL context, may be it helps.


Tested your code. Symbols still not being loaded, though it might be
wise to borrow some of your organizational conventions.

Just for kicks I had a friend try to run the executable I produced, and
she said she got exactly the same output I did, so it's definitely
something that gets compiled into the program. A bug report has been
posted on Github, including the full source code(which I probably did
completely wrong) and a screenshot of the output.


Hmm, just checked it and it works. Change dependencies in package.json 
on this:

dependencies: {
gl3n: ==1.0.0,
glamour: ==1.0.1,
derelict-gl3: ==1.0.12,
derelict-fi: ==1.9.0,
},
and install besides SDL2 developer version of freeimage and libcurl. 
Unfortunately data format of server has changed and there won't be 
properly rendered map, but correct OpenGL context will be created undoubtly.


Re: how can I get a reference of array?

2015-02-05 Thread zhmt via Digitalmars-d-learn

Will arr.ptr change in the future?

As the array add more members , it need more memroy, then 
remalloc may be called, the pointer maybe change, then the stored 
pointer will be invalid.


Will this happen?




Re: how can I get a reference of array?

2015-02-05 Thread bearophile via Digitalmars-d-learn

zhmt:


Will arr.ptr change in the future?

As the array add more members , it need more memroy, then 
remalloc may be called, the pointer maybe change, then the 
stored pointer will be invalid.


Will this happen?


Yes, it can happen.

Bye,
bearophile


D + Solaris

2015-02-05 Thread suliman via Digitalmars-d-learn
My next job will be related with prorgamming under Solaris. I 
dont know version but suppose it will be something pretty out of 
date.


Once i had seen topic about D and Solaris, but i want to ask if D 
solaris version is ready for production or it is alpha? Does 
anybody have experience with it?


Re: Syntax for checking if an element exists in a list

2015-02-05 Thread anonymous via Digitalmars-d-learn
On Thursday, 5 February 2015 at 13:31:21 UTC, Nicholas Wilson 
wrote:

Note that D does NOT have default fall through. i.e.
 switch(myopt)
{
case opt1, opt2, opt3:
do_A();
break;
case opt4:
do_B();
break;
default:
do_C();
 }
is equivalent to
switch(myopt)
{
case opt1, opt2, opt3:
do_A();
case opt4:
do_B();
default:
do_C();
 }


No, that's not right.

The docs [1] say that [a] ScopeStatementList must either be 
empty, or be ended with a ContinueStatement, BreakStatement, 
ReturnStatement, GotoStatement, ThrowStatement or assert(0) 
expression unless this is the last case.


That's different from what you said. `case opt1: doA(); case 
4: do_B();` is supposed to be forbidden. It's not supposed to 
have an implicit break.


And apparently, dmd is not there (yet?). Implicit fall-through is 
only a warning at the moment (which have to be switched on). It's 
not an error.


[1] http://dlang.org/statement.html#switch-statement


Re: Do you have a better way to remove element from a array?

2015-02-05 Thread FrankLike via Digitalmars-d-learn

On Thursday, 5 February 2015 at 14:09:10 UTC, bearophile wrote:

Tobias Pankrath:

Works as designed: 
http://dlang.org/phobos/std_algorithm.html#.remove


Unfortunately it's one of the worst designed functions of 
Phobos:

https://issues.dlang.org/show_bug.cgi?id=10959

Bye,
bearophile


Yes,A.remove(item) or A.removeAt(index)
They is better than now.


Re: Do you have a better way to remove element from a array?

2015-02-05 Thread FrankLike via Digitalmars-d-learn

On Thursday, 5 February 2015 at 14:09:10 UTC, bearophile wrote:

Yes,A.remove(item) or A.removeAt(index)

They are better than now.


Re: Syntax for checking if an element exists in a list

2015-02-05 Thread Stéphane
On Thursday, 5 February 2015 at 13:31:21 UTC, Nicholas Wilson 
wrote:

Note that D does NOT have default fall through. i.e.


  Yes, but I thought I read that we always had to explicitely 
specify one ending statement, as goto, continue... or 
break; which, in many basic cases, means having to add break 
at the end of each case. One motivation was to force to make it 
look like C (I cannot say I agree with this purpose, but I think 
I read it somewhere).


PPS: Where should I post, when I have such questions and 
problems

as the one in my previous paragraph? I did not find any meta
forum.
learn is the correct  place for such questions and in general 
for questions about how to do something or enquire as to why 
something is not working or not working the way you think it 
should.


  I was talking about my problem with the mailing-lists (the one 
in my PS); I am not sure you are talking about it.


strange alias behavior

2015-02-05 Thread Vlad Levenfeld via Digitalmars-d-learn

I'm expecting in this code http://dpaste.dzfl.pl/ee0d3cd31734
either line 8 should not compile, or lines 12 and 14 should have 
a totally different output. What's going on?


Re: strange alias behavior

2015-02-05 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, February 05, 2015 23:08:13 Vlad Levenfeld via Digitalmars-d-learn 
wrote:
 I'm expecting in this code http://dpaste.dzfl.pl/ee0d3cd31734
 either line 8 should not compile, or lines 12 and 14 should have
 a totally different output. What's going on?

It looks like

alias b = that.obj;

is actually aliasing this.obj instead of that.obj for some reason, which is
definitely a compiler bug. Please report it: https://issues.dlang.org

- Jonathan M Davis



Trying to make a TCP server, client connects and disconnects immediately

2015-02-05 Thread Gan via Digitalmars-d-learn
I managed to get the client to connect but immediately on the 
server side, this happens:


long length = player.playerSocket.receive(buf);
if (length == 0) { //No longer connected
player.playerSocket.shutdown(SocketShutdown.BOTH);
player.playerSocket.close();
}

The player's socket receives a 0 length which means the 
connection is dead. Why does that happen?


Here's my full server-side networking code:

module server.networkingandio.networkingcontroller;
import server.server;
import server.player.player;

import std.socket;
import std.stdio;
import core.thread;
import std.range;
import std.traits;
import std.typecons;
import std.typetuple;
import std.algorithm;
import std.concurrency;

class NetworkingController
{
Server server;

Player[] players;

TcpSocket socket;

this(Server s)
{
server = s;
players = new Player[](0);
writeln(Server active on port );

socket = new TcpSocket();
socket.bind(new InternetAddress());
socket.listen(100);
socket.blocking = true;

spawn(handleNewConnectionsThread, cast(shared)socket);
}
void logic() {
//Check for new sockets
bool receivedNewSocket = true;
while (receivedNewSocket) {
receivedNewSocket = receiveTimeout(0.msecs,
(shared NewConnectionMsg message) {
NewConnectionMsg msg = 
cast(NewConnectionMsg)message;
newConnection(msg.socket);
},
(shared ReceiveDataMsg message) {
	//Convert data to MessageReader, send to server message 
handler

}
);
}

//Check for dead sockets
for (int i = 0; i  players.length; i++) {
Player p = players[i];
if (p.playerSocket.isAlive == false) {
players = players.remove(i);
i--;
server.removeConnection(p);
}
}
//Check for socket messages
}
void newConnection(Socket conn) {
Player p = new Player(conn, this);
players ~= p;
server.newConnection(p);
//Set up listener for player socket
spawn(handleReceiveDataThread, cast(shared)p);
}
}
class NewConnectionMsg {
Socket socket;
this(Socket socket) {
this.socket = socket;
}
}
class ReceiveDataMsg {
Player player;
ubyte[] data;
this(Player player, ubyte[] data) {
this.player = player;
this.data = data;
}
}

void handleNewConnectionsThread(shared TcpSocket s) {
Socket socket = cast(Socket)s;
while(socket.isAlive) {
Socket playerSocket = socket.accept();
if (playerSocket !is null  playerSocket.isAlive == true) {
playerSocket.blocking = true;
ownerTid.send(cast(shared) new 
NewConnectionMsg(playerSocket));
}
}
scope(exit)socket.close();
}

void handleReceiveDataThread(shared Player p) {
Player player = cast(Player)p;
while(player.playerSocket.isAlive) {
ubyte[] buf = new ubyte[](0);
long length = player.playerSocket.receive(buf);
if (length == 0) { //No longer connected
player.playerSocket.shutdown(SocketShutdown.BOTH);
player.playerSocket.close();
} else {
ownerTid.send(cast(shared) new ReceiveDataMsg(player, 
buf));
}
}
scope(exit)player.playerSocket.close();
}


Re: Trying to make a TCP server, client connects and disconnects immediately

2015-02-05 Thread Gan via Digitalmars-d-learn

On Friday, 6 February 2015 at 00:24:54 UTC, Adam D. Ruppe wrote:

On Friday, 6 February 2015 at 00:15:15 UTC, Gan wrote:

ubyte[] buf = new ubyte[](0);


This is your problem: receive fills a preexisting buffer, and 
you allocated zero bytes for it to fill, so it can't give you 
anything.


Give it a bigger buffer, I like to use 4096, and it will work 
better.


Will there ever be a message bigger than 4096 bytes?


Re: Trying to make a TCP server, client connects and disconnects immediately

2015-02-05 Thread Gan via Digitalmars-d-learn

On Friday, 6 February 2015 at 00:28:00 UTC, Gan wrote:

On Friday, 6 February 2015 at 00:24:54 UTC, Adam D. Ruppe wrote:

On Friday, 6 February 2015 at 00:15:15 UTC, Gan wrote:

ubyte[] buf = new ubyte[](0);


This is your problem: receive fills a preexisting buffer, and 
you allocated zero bytes for it to fill, so it can't give you 
anything.


Give it a bigger buffer, I like to use 4096, and it will work 
better.


Will there ever be a message bigger than 4096 bytes?


Or am I misunderstanding the receive function? Does it send whole 
messages or just message chunks?


Re: Trying to make a TCP server, client connects and disconnects immediately

2015-02-05 Thread Adam D. Ruppe via Digitalmars-d-learn

On Friday, 6 February 2015 at 00:31:37 UTC, Gan wrote:
Or am I misunderstanding the receive function? Does it send 
whole messages or just message chunks?


It sends as much as it can when you call it. So if there's only 
12 bytes available when you send it with a 4096 buffer, it will 
fill the first twelve bytes (and return 12) so you can slice it 
buffer[0 .. returnedValue] to get the data.


If there's more available than the buffer will hold, that data 
will be held on to until next time you call receive. If 5000 
bytes come off the network, it will return 4096 the first time, 
then next time through the loop, it will return the remaining 904.


Re: Trying to make a TCP server, client connects and disconnects immediately

2015-02-05 Thread Adam D. Ruppe via Digitalmars-d-learn

On Friday, 6 February 2015 at 00:15:15 UTC, Gan wrote:

ubyte[] buf = new ubyte[](0);


This is your problem: receive fills a preexisting buffer, and you 
allocated zero bytes for it to fill, so it can't give you 
anything.


Give it a bigger buffer, I like to use 4096, and it will work 
better.


Re: Do you have a better way to remove element from a array?

2015-02-05 Thread bearophile via Digitalmars-d-learn

bachmeier:

It seems your argument is that remove is poorly designed 
because it's not destructive. Or am I missing your argument?


It has to be a void function (or perhaps bettter it can return 
true/false if it has removed the item, so it becomes @nogc and 
nothrow).

And it has to remove the first item equal to the given one.
You can then add a second function that removes at a given index 
(like removeAt).


Bye,
bearophile


Re: Trying to make a TCP server, client connects and disconnects immediately

2015-02-05 Thread Adam D. Ruppe via Digitalmars-d-learn

On Friday, 6 February 2015 at 00:50:43 UTC, Gan wrote:
How can you distinguish between different packets that get 
sent? Won't they all be merged to a giant blob of data?


Yeah, that's normal in TCP though, because it is a stream 
oriented protocol. You could add stuff to your data indicating 
message length though.


Re: Trying to make a TCP server, client connects and disconnects immediately

2015-02-05 Thread Gan via Digitalmars-d-learn

On Friday, 6 February 2015 at 00:35:12 UTC, Adam D. Ruppe wrote:

On Friday, 6 February 2015 at 00:31:37 UTC, Gan wrote:
Or am I misunderstanding the receive function? Does it send 
whole messages or just message chunks?


It sends as much as it can when you call it. So if there's only 
12 bytes available when you send it with a 4096 buffer, it will 
fill the first twelve bytes (and return 12) so you can slice it 
buffer[0 .. returnedValue] to get the data.


If there's more available than the buffer will hold, that data 
will be held on to until next time you call receive. If 5000 
bytes come off the network, it will return 4096 the first time, 
then next time through the loop, it will return the remaining 
904.


How can you distinguish between different packets that get sent? 
Won't they all be merged to a giant blob of data?


Re: Trying to make a TCP server, client connects and disconnects immediately

2015-02-05 Thread Mike Parker via Digitalmars-d-learn

On 2/6/2015 9:50 AM, Gan wrote:

On Friday, 6 February 2015 at 00:35:12 UTC, Adam D. Ruppe wrote:

On Friday, 6 February 2015 at 00:31:37 UTC, Gan wrote:

Or am I misunderstanding the receive function? Does it send whole
messages or just message chunks?


It sends as much as it can when you call it. So if there's only 12
bytes available when you send it with a 4096 buffer, it will fill the
first twelve bytes (and return 12) so you can slice it buffer[0 ..
returnedValue] to get the data.

If there's more available than the buffer will hold, that data will be
held on to until next time you call receive. If 5000 bytes come off
the network, it will return 4096 the first time, then next time
through the loop, it will return the remaining 904.


How can you distinguish between different packets that get sent? Won't
they all be merged to a giant blob of data?


You need to give each of your packets a header. At a minimum you'll want 
a message ID and message length. When a message comes in, you use the 
length field to determine where one packet ends and the next one begins.


Re: how can I get a reference of array?

2015-02-05 Thread zhmt via Digitalmars-d-learn

On Thursday, 5 February 2015 at 08:58:47 UTC, bearophile wrote:

zhmt:


Will arr.ptr change in the future?

As the array add more members , it need more memroy, then 
remalloc may be called, the pointer maybe change, then the 
stored pointer will be invalid.


Will this happen?


Yes, it can happen.

Bye,
bearophile


Thx. I understand the difference between array pointer and 
arr.ptr, they are not the same thing.


Re: how can I get a reference of array?

2015-02-05 Thread Jakob Ovrum via Digitalmars-d-learn

On Thursday, 5 February 2015 at 06:58:09 UTC, Ali Çehreli wrote:

On 02/04/2015 10:42 PM, zhmt wrote:

Here is a simple code snippet:


With this approach, the allocation of `arr` itself is often 
clumsy and error-prone. In this case it is important to note that 
`arr` is allocated on the stack and thus has scoped lifetime.


Consider using std.container.array.Array instead.


Re: how can I get a reference of array?

2015-02-05 Thread Christof Schardt via Digitalmars-d-learn


Ali Çehreli acehr...@yahoo.com schrieb im Newsbeitrag 
news:mav4a1$l3a$1...@digitalmars.com...

On 02/04/2015 10:42 PM, zhmt wrote:
The following article is very informative:

  http://dlang.org/d-array-article.html


Nice and important article.

But: How could I have looked this up this myself?
What should be the official path to this link?

I tried BooksArticles and Community = More Links
and could not find it.




Syntax for checking if an element exists in a list

2015-02-05 Thread Stéphane

Syntax for checking if an element exists in a list

Hello,

   I would like to know if there is a better (easier to wite,
easier to read, easier to understand) way to check if an element
(string) is
in a list of strings.

   Here are the possibilities I see today, as someone who is new
to D:

1) using a switch

char [] myopt=get_an_option();

switch(myopt) {
case opt1, opt2, opt3:
   do_A();
   break;
case opt4:
   do_B();
   break;
default:
   do_C();
}

   A switch/case is nice when there are many cases, but when there
are only 1 or 2, the syntax is heavy (because of the breaks)
and not
so nice (the if/else toggle is much more visible in my eyes
than the case/break/default).


2) InExpression

   It only works with hashes (with hashes keys, more exactly).

char [] myopt=get_an_option();

if(myopt in [opt1:1, opt2:1, opt3:1]) {
   do_A();
} else if(myopt==opt4) {
   do_B();
} else {
   do_C();
}

   The fact that the hash needs values means is not very nice to
read and write those useless (in this use case) dummy init values
:1 and
it brings not only visual confusion but also confusion about the
intention, especially if the hash is declared beforehand:

int[string] main_set_of_options=[opt1:1, opt2:1, opt3:1];

char [] myopt=get_an_option();

if(myopt in main_set_of_options) {
   do_A;
} else if(myopt==opt4) {
   do_B();
} else {
   do_C();
}

   because it is impossible to know whether these values are dummy
or carry a useful information.


   So, in fact, I would like to know if there is a way to use
something sweet like the in of other modern languages.
   It would be enough if the present in would work with array
values instead of only with hash keys.
   Or if there was a way to initialise a hash with only keys and
no value (or a default value). For example, with different
delimiters or
with some
special sigil before the hash initialisation, like (the
delimiters and sigil are random, not a real suggestion):

if(myopt in /opt1, opt2, opt3/) { ... }

if(myopt in $[opt1, opt2, opt3]) { ... }

   But perhaps one of these solutions, or some other, already
works and I am just unaware of it. Please enlighten me :-)


Goodbye,
   Stéphane.

PS: I tried posting this message to the mailing-list, but I got
digitalmars-d-learn@puremagic.com: delivery to host
puremagic.com[173.45.241.208] timed out. It was the same when I
tried to confirm my subscription to the list, I had to use the
weblink to achieve the confirmation. Does anyone posts through
the mailing list or do you all use the newsgroup or the web forum?

PPS: Where should I post, when I have such questions and problems
as the one in my previous paragraph? I did not find any meta
forum.


Re: how can I get a reference of array?

2015-02-05 Thread zhmt via Digitalmars-d-learn

On Thursday, 5 February 2015 at 10:12:47 UTC,  wrote:

On Thursday, 5 February 2015 at 06:58:09 UTC, Ali Çehreli wrote:

On 02/04/2015 10:42 PM, zhmt wrote:

Here is a simple code snippet:


With this approach, the allocation of `arr` itself is often 
clumsy and error-prone. In this case it is important to note 
that `arr` is allocated on the stack and thus has scoped 
lifetime.


Consider using std.container.array.Array instead.


@Jakob Ovrum

Thx for your advice.

I am still learning dlang, I will pay attention to memory 
management in the future.


Re: What am I doing Wrong (OpenGL SDL)

2015-02-05 Thread Entity325 via Digitalmars-d-learn
Aldacron and I have determined that I'm doing something weird 
with the imports between gl.d and gl3.d, the functions I'm trying 
to access are deprecated so the problem is less that they aren't 
loading and more that I can see them at all.


Re: Do you have a better way to remove element from a array?

2015-02-05 Thread FG via Digitalmars-d-learn

On 2015-02-05 at 17:25, bearophile wrote:

It has to be a void function (or perhaps bettter it can return true/false if it 
has removed the item, so it becomes @nogc and nothrow).
And it has to remove the first item equal to the given one.
You can then add a second function that removes at a given index (like 
removeAt).


I was never a fan of STL's erase-remove idiom, although the decoupling of 
algorithms and containers is in general a great idea. However, in D algorithms 
can be smarter, because they operate on ranges instead of iterators. I don't 
see why remove has to follow the C++ example. Therefore I have to ask:

Is there any reason why `remove` doesn't take the range by reference and 
`popBack` as many elements as were removed?


Re: how can I get a reference of array?

2015-02-05 Thread Ali Çehreli via Digitalmars-d-learn

On 02/05/2015 01:49 AM, Christof Schardt wrote:


Ali Çehreli acehr...@yahoo.com schrieb im Newsbeitrag
news:mav4a1$l3a$1...@digitalmars.com...

On 02/04/2015 10:42 PM, zhmt wrote:
The following article is very informative:

  http://dlang.org/d-array-article.html


Nice and important article.

But: How could I have looked this up this myself?
What should be the official path to this link?

I tried BooksArticles and Community = More Links
and could not find it.




The link used to be there. Please file a documentation bug presumably 
conveniently through the Improve this page link on dlang.org.


Thank you,
Ali