Re: Socket server + thread: cpu usage
On Tuesday, 29 April 2014 at 17:19:41 UTC, Adam D. Ruppe wrote: On Tuesday, 29 April 2014 at 17:16:33 UTC, Tim wrote: Is there anything I'm doing wrong? You should be using a blocking socket. With them, the operating system will put your thread on hold until a new connection comes in. Without them, it will endlessly loop doing absolutely nothing except checking if a new connection is there yet. Horribly, horribly inefficient. Blocking sockets are now working as expected, but I've one problem. When I do the following in my server-accept-thread: while (m_oSocket.isAlive) { oSocketSet.reset(); oSocketSet.add(m_oSocket); nAcceptedSockets = Socket.select(oSocketSet, null, null, 25.msecs); if (nAcceptedSockets 0) { // Do something... } } ... and the following in my client: void connect() { m_oSocket = new TcpSocket(AddressFamily.INET); m_oSocket.connect(new InternetAddress(127.0.0.1, 12345)); } The CPU usage is low as long as my connect is connected. When I disconnect the client using the following few lines: void disconnect() { m_oSocket.shutdown(SocketShutdown.BOTH); m_oSocket.close(); } ... the CPU usage goes up. I think that SocketShutdown.BOTH causes Socket.select to fail which results in an endless loop. Any suggestions how to handle that problem? @Ali Çehreli: Thanks, didn't know that UFCS can also handle such constructs :)
Re: Socket server + thread: cpu usage
On Thursday, 1 May 2014 at 08:08:37 UTC, Tim wrote: On Tuesday, 29 April 2014 at 17:19:41 UTC, Adam D. Ruppe wrote: On Tuesday, 29 April 2014 at 17:16:33 UTC, Tim wrote: Is there anything I'm doing wrong? You should be using a blocking socket. With them, the operating system will put your thread on hold until a new connection comes in. Without them, it will endlessly loop doing absolutely nothing except checking if a new connection is there yet. Horribly, horribly inefficient. Blocking sockets are now working as expected, but I've one problem. When I do the following in my server-accept-thread: while (m_oSocket.isAlive) { oSocketSet.reset(); oSocketSet.add(m_oSocket); nAcceptedSockets = Socket.select(oSocketSet, null, null, 25.msecs); if (nAcceptedSockets 0) { // Do something... } } ... and the following in my client: void connect() { m_oSocket = new TcpSocket(AddressFamily.INET); m_oSocket.connect(new InternetAddress(127.0.0.1, 12345)); } The CPU usage is low as long as my connect is connected. When I disconnect the client using the following few lines: void disconnect() { m_oSocket.shutdown(SocketShutdown.BOTH); m_oSocket.close(); } ... the CPU usage goes up. I think that SocketShutdown.BOTH causes Socket.select to fail which results in an endless loop. Any suggestions how to handle that problem? @Ali Çehreli: Thanks, didn't know that UFCS can also handle such constructs :) Small correction: The CPU usage is low as long as my client is connected. When I disconnect the client using the following few lines:
Re: A lot of people want to use D,but they only know MS SQL Server,what will help them to Learn D?
On Monday, 14 April 2014 at 17:13:56 UTC, FrankLike wrote: My advice - use ODBC, it is the fastest way you may connect to the SQL server, and you already have everything you need for that. :) Regards I have test the d\dmd2\windows\lib\odbc32.lib,the size is 4.5kb, I test it by test.d(build :dmd test.d) but find the error: Error 42:Symbol Undefined _SQLFreeHandle@8 Error 42:Symbol Undefined _SQLSetEnvAttr@16 Error 42:Symbol Undefined _SQLAllocHandle@12 Error 42:Symbol Undefined _SQLGetDiagRec@32 -- errorlevel 4 I have fixed the errors. The exe file only 210kb,it works very good. Where the errors is ? In the odbc32.def file. must set the all used function names. such as: _SQLFreeHandle@8 = SQLFreeHandle Ok. Frank.
Re: A lot of people want to use D,but they only know MS SQL Server,what will help them to Learn D?
On Thu, 01 May 2014 09:56:49 +0100, FrankLike 1150015...@qq.com wrote: On Monday, 14 April 2014 at 17:13:56 UTC, FrankLike wrote: My advice - use ODBC, it is the fastest way you may connect to the SQL server, and you already have everything you need for that. :) Regards I have test the d\dmd2\windows\lib\odbc32.lib,the size is 4.5kb, I test it by test.d(build :dmd test.d) but find the error: Error 42:Symbol Undefined _SQLFreeHandle@8 Error 42:Symbol Undefined _SQLSetEnvAttr@16 Error 42:Symbol Undefined _SQLAllocHandle@12 Error 42:Symbol Undefined _SQLGetDiagRec@32 -- errorlevel 4 I have fixed the errors. The exe file only 210kb,it works very good. Where the errors is ? In the odbc32.def file. must set the all used function names. such as: _SQLFreeHandle@8 = SQLFreeHandle That's interesting. Those functions are _stdcall, so should be exported from the lib as _func@N. How did you declare them in arsd.mssql? You should use extern(Windows) e.g. extern(Windows) SQLRETURN SQLFreeHandle(SQLSMALLINT HandleType, SQLHANDLE Handle); The extern(Windows) tells DMD to look for _stdcall. extern(C) tells it to look for _cdecl. The difference boils down to who is responsible for cleaning up the stack after a function call. _stdcall assumes the callee will cleanup the stack, _cdecl assumes the caller will. R -- Using Opera's revolutionary email client: http://www.opera.com/mail/
Strings concatenated at compile time?
In the following example from the documentation, are strings concatenated at compile time? template foo(string s) { string bar() { return s ~ betty; } } void main() { writefln(%s, foo!(hello).bar()); // prints: hello betty }
Re: Strings concatenated at compile time?
On Thursday, 1 May 2014 at 10:42:36 UTC, Unwise wrote: In the following example from the documentation, are strings concatenated at compile time? template foo(string s) { string bar() { return s ~ betty; } } void main() { writefln(%s, foo!(hello).bar()); // prints: hello betty } I guess it's not guaranteed, but constant folding should take care of it, yes.
Reading ELF Files
Have anybody put together some D code for reading out tables from ELF files? A range/slice based version would be nice.
Re: Reading ELF Files
On Thursday, 1 May 2014 at 11:38:50 UTC, Nordlöw wrote: Have anybody put together some D code for reading out tables from ELF files? A range/slice based version would be nice. I don't know this though, in the morning Ubuntu has showed updates. One of them was libelf which says in its description it is used to read and write ELF files. Writing a wrapper class for this could be useful.
Re: Strings concatenated at compile time?
On Thu, 01 May 2014 11:12:41 + anonymous via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: On Thursday, 1 May 2014 at 10:42:36 UTC, Unwise wrote: In the following example from the documentation, are strings concatenated at compile time? template foo(string s) { string bar() { return s ~ betty; } } void main() { writefln(%s, foo!(hello).bar()); // prints: hello betty } I guess it's not guaranteed, but constant folding should take care of it, yes. If you want it to be guaranteed, you'd do something like template foo(string s) { enum foo = s ~ betty; } void main() { writeln(foo!hello); } I would hope that the optimizer would have optimized out the concatenation in your example though. - Jonathan M Davis
Re: Strings concatenated at compile time?
Jonathan M Davis: If you want it to be guaranteed, you'd do something like template foo(string s) { enum foo = s ~ betty; } A more general solution is to wrap the concatenation with a call to: alias ctEval(alias expr) = expr; Use: string bar() { return ctEval!(s ~ betty); } Bye, bearophile
Re: Reading ELF Files
On Thursday, 1 May 2014 at 11:38:50 UTC, Nordlöw wrote: Have anybody put together some D code for reading out tables from ELF files? A range/slice based version would be nice. I have some simple proof of concept code. It is currently able to read elf64 (can be easily adjusted to read elf32 too) headers, extract sections and read string tables. If this is what you need, then I'll upload my code somewhere (although again, it is quite simplistic). If you specify what you need a bit more, I might be able to provide that.
Re: Reading ELF Files
I have some simple proof of concept code. It is currently able to read elf64 (can be easily adjusted to read elf32 too) headers, extract sections and read string tables. If this is what you need, then I'll upload my code somewhere (although again, it is quite simplistic). If you specify what you need a bit more, I might be able to provide that. Seems nice. I need this for my file scanning/indexing engine, where I currently want to extend it to index object file symbols.
Re: Reading ELF Files
again, it is quite simplistic). If you specify what you need a bit more, I might be able to provide that. Please, post :)
Re: Socket server + thread: cpu usage
On Thursday, 1 May 2014 at 08:08:37 UTC, Tim wrote: ... the CPU usage goes up. I think that SocketShutdown.BOTH causes Socket.select to fail which results in an endless loop. Any suggestions how to handle that problem? It shouldn't be here, disconnect would affect the new socket, and you're calling select on the accepting socket, which is still good to accept a new connection. What's your code look like for handing a socket returned by accept?
Re: Reading ELF Files
On Thursday, 1 May 2014 at 13:10:33 UTC, Nordlöw wrote: again, it is quite simplistic). If you specify what you need a bit more, I might be able to provide that. Please, post :) Here you go, https://github.com/yazd/elf-d.
Re: Reading ELF Files
Here you go, https://github.com/yazd/elf-d. Thanks!
Unresolved external symbol
I have the following code: import std.stdio; version(Windows) { extern(Windows) { nothrow { alias void *HANDLE; alias HANDLE HDC; int GetDeviceCaps(HDC, int); } } } int main() { int left = GetDeviceCaps(null, 8); readln(); return 0; } And I am getting a error LNK2019: unresolved external symbol GetDeviceCaps referenced in function _Dmain Why is that? I'm obviously missing something here, because another function, GetDC works properly..
Making enum join variadic
How can I make `join` variadic (by filling in njoin) in the following code? import std.stdio: writeln; import std.traits; string enumsHelper(S...)(S s) { typeof(return) r; foreach (i, e; s) { if (i = 1) r ~= , ; r ~= e; } return r; } /** Join/Chain/Concatenate/Unite Enums $(D E1), $(D E2), ... into $(D E). See also: http://forum.dlang.org/thread/f9vc6p$1b7k$1...@digitalmars.com */ template join(string E, E1, E2) { const join = (enum ~ E ~ { ~ enumsHelper(__traits(allMembers, E1)) ~ , ~ enumsHelper(__traits(allMembers, E2)) ~ } ); } template njoin(string E, Ei...) { import std.algorithm: map; enum string njoin = enum ~ E ~ { ~ ~ } ; } void main(string[] args) { enum E1 { A, B, C } enum E2 { E, F, G } mixin(join!(E12, E1, E2)); E12 e12; writeln(e12.min, ,, e12.max); }
Re: Making enum join variadic
Nordlöw: How can I make `join` variadic (by filling in njoin) in the following code? When you have a D tuple (not Phobos tuple), and it contains values all of the same type, you can turn it into an array with just: [mytuple] Once you have an array of strings, you can use the normal phobos function to join the strings as you desire. Bye, bearophile
Re: Making enum join variadic
On 05/02/14 00:24, Nordlöw via Digitalmars-d-learn wrote: How can I make `join` variadic (by filling in njoin) in the following code? import std.array, std.range, std.algorithm; import std.stdio; template Njoin(ES...) { mixin({ string r = enum Njoin { ; foreach (E; ES) r ~= [__traits(allMembers, E), ].join(,); return r ~ }; }()); } void main(string[] args) { enum E1 { A, B, C } enum E2 { E, F, G } alias E12 = Njoin!(E1, E2); E12 e12; writeln(e12.min, ,, e12.max); } artur