Re: Nim, Support for https?

2019-10-22 Thread 2vg
OpenSSL is wrapped, but you need to know about OpenSSL functions and network 
I/O.


Re: Modern way to pass a sequence to a c function as a void pointer and to cast it back as seq

2019-05-16 Thread 2vg
` cFunction(sq[0].unsafeAddr.pointer) `

Run


Re: Using var object in a proc that is the object's property

2018-12-21 Thread 2vg
ah sorry, missed


Re: Using var object in a proc that is the object's property

2018-12-21 Thread 2vg

proc registerEvents(wrapper: var SomeWrapper) =
  wrapper.events.onload =
proc (name: string) =
  echo "loaded " & name
  wrapper.onload(name)


Run

why call onload recursively?


Re: Using var object in a proc that is the object's property

2018-12-20 Thread 2vg
if using ref object,


type
  Events = object
onload, onfail: proc (name: string)

type
  SomeWrapper = ref object
events: Events
onload, onfail: proc (name: string)

proc registerEvents(wrapper: SomeWrapper) =
  wrapper.events.onload = proc (name: string) = echo "loaded " & name; 
wrapper.onload(name)
  wrapper.events.onfail = proc (name: string) = echo "failed to load " & 
name; wrapper.onfail(name)

var s = SomeWrapper()
registerEvents(s) # work


Run


Re: Using var object in a proc that is the object's property

2018-12-20 Thread 2vg
using addr.

> 
> var someObj = SomeWrapper()
> 
> registerEvents(addr someObj)
> 
> 
> Run

why not using ref object? performance?


Re: why var can not be declared this way?

2018-11-05 Thread 2vg
read-only -> using let


proc f(size=1) =
let (widFrame, hiFrame) =
if size == 1: (320, 200) else: (3200, 2000)

echo (widFrame, hiFrame)

f()


Run


Re: Got "Warning: < is deprecated [Deprecated]"

2018-11-04 Thread 2vg
` for i in 0 ..< param.len: do() `

Run


Re: Why is it needed to discard void return value from c function?

2018-07-28 Thread 2vg
` quit() `

Run


Re: atomic and ref pointer

2018-04-04 Thread 2vg
> Well if you protect and dispose the ref properly,

"dispose"

Does it mean that the tracking of the GC disappears when casting ref to ptr ?

Should I use boehm gc ?


type
  Foo = object
head, tail: ptr Foo

var
  f = cast[ptr Foo](Foo.new())
  pf = cast[ptr Foo](Foo.new())

f.head = pf
f.tail = pf

# proc doWork() =
#   discard cas(addr(f.head), pf, cast[ptr Foo(Foo.new))

# dealloc(f.head) Is this need ?



atomic and ref pointer

2018-04-01 Thread 2vg
Is there a correct way to handle ref object with atomic? Is it a bad way to 
cast to ptr?


Re: Is there any way to create template with await?

2018-03-23 Thread 2vg
@adrianv

[this](https://nim-lang.org/docs/asyncdispatch.html#asynchronous-procedures-handling-exceptions)


Re: Warning: parseopt2 is deprecated

2018-03-16 Thread 2vg
parseopt.

Please see this article 
[version-0180-released](https://nim-lang.org/blog/2018/03/01/version-0180-released.html)


Re: How to turn thread spawn call into an async call

2018-03-15 Thread 2vg
Have you tried this? [asyncpg](https://github.com/cheatfate/asyncpg)

To wait for some result of Spawn's task, you can confirm by using isReady() 
function.

For example, 
[ChatApp#L38](https://github.com/dom96/nim-in-action-code/blob/master/Chapter3/ChatApp/src/client.nim#L38)


Re: Need help with async client/server

2018-03-12 Thread 2vg
In addition, recv is executed during sleepAsync, but it is blocked immediately 
by readLine (). It will not be a solution because we can not receive messages 
during blocking.


Re: Need help with async client/server

2018-03-12 Thread 2vg
Probably because stdin.readLine() is a blocking call, the event loop is blocked 
and other tasks can not be executed.

wait well in a separate thread, or such a library will be useful 
[asynctools/asyncpty](https://github.com/cheatfate/asynctools/blob/master/asynctools/asyncpty.nim)


Re: C's char* -> ptr char or cstring ?

2018-01-19 Thread 2vg
thx a lot @jangko !!

> when you free the memory, Nim internal allocator might reuse it at the next 
> alloc call, if it thinks the previous block allocated could be reuse, hence 
> the garbage.

learned a lot, thank you !!!

> isn't this scenario a common practice when using C language?(I really have no 
> idea about your C skills)

yay... since i started learning C at the same time as Nim's C wrapping, I do 
not have enough knowledge... sorry

> also I noticed that in line 67: if nread == 0: return, you don't free the 
> buffer, doesn't it will leak?

!!! This will cause leaks, thanks !!!


Re: C's char* -> ptr char or cstring ?

2018-01-19 Thread 2vg
@jangko thx ! but, However, although I am freeing up the memory, when request 
coming shorter than the previous request, the character of the previous request 
is added at the end of the short request.

However, if call alloc0 instead, the problem is gone

for example, 


GET /foo HTTP1.1\13\10


next req, 


GET / HTTP1.11\13\10
#↑↑↑
# GET /foo HTTP1.1\13\10


sometimes, adding garbage char such as unicode which a previous request does 
not have.

This was confirmed with the debug code here:

[mofuw#L65](https://github.com/2vg/mofuw/blob/master/mofuw.nim#L65)

Since libuv calls the callback to store the buffer when storing the request, we 
are securing the memory here:

[mofuw#L49](https://github.com/2vg/mofuw/blob/master/mofuw.nim#L49)

memory is released with one of the following:

when req is end,

[mofuw#L70](https://github.com/2vg/mofuw/blob/master/mofuw.nim#L70)

when some error,

[mofuw#L74](https://github.com/2vg/mofuw/blob/master/mofuw.nim#L74)

when parse error, (my parser returns a negative value on error.)

[mofuw#L95](https://github.com/2vg/mofuw/blob/master/mofuw.nim#L95)

and, after callback end,

[mofuw#L108](https://github.com/2vg/mofuw/blob/master/mofuw.nim#L108)

although memory leak does not occur even if high load is applied, but why is 
unexpected character added when alloc0() is not used ... ;;


C's char* -> ptr char or cstring ?

2018-01-19 Thread 2vg
I using ptr char for wrapping char*... (yay, libuv wrap)

However, should I use cstring? Is it recommend?

The string remains even after buffer is released on libuv.

I call alloc () and then cast it to ptr char, is this wrong?


Re: Is anyone using the libuv wrappers?

2018-01-12 Thread 2vg
perhaps Araq is talking about SSL wrapping.

looking at devel, it seems that the wrapper of libuv has been delete.

[https://github.com/nim-lang/Nim/tree/devel/lib/wrappers](https://github.com/nim-lang/Nim/tree/devel/lib/wrappers)


Re: high load nil error on my web server

2018-01-10 Thread 2vg
yep... that code is very bad sorry x(

approach of the model as the Web server was wrong, this cord deletes it now!

* * *

> Why not use Nim's asynhttpserver?

answer is simple.

because, i want to make fast web server. its just my **hobby**.

i think AsyncHTTPServer is a bit slow... ah, this does not mean that 
AsyncHTTPServer is bad.

when I create a web application I will use AsyncHTTPServer  that great web 
server tool.

i made a Web server with AsyncDispatcher alone to see the performance of 
AsyncDispatcher.

it was about 1.5 times faster than AsyncHTTPServer.

i have not seen all the internal code of AsyncHTTPServer, but I expected that 
the parser is probably a bottleneck.

assume that requests received from clients are temporarily saved in variables.

better parsers will not allocate memory in the process of parsing the request.

for example, like [picohttpparser](https://github.com/h2o/picohttpparser)

i needed such a fast parser to make a fast web server.

the resulting parser is this [mofuparser](https://github.com/2vg/mofuparser)

since it is zero copy, even when parsing 100, 000 times, the request parsing 
ends in about 0.05 seconds. (CPU: Intel core 2Duo T7700 2.40GHz 3 Core)

also, by using libuv to create multithreaded event loops, a web server with 
considerable performance was completed.

... talk is wrong

AsyncHTTPServer is not bad, rather a wonderful tool.

i dont know a lot about async, but i think the async of Nim will become better 
in future.

thank you for splendid language, Araq 

(sorry if wrong because im weak in English! ; _ ; )


Re: sometimes using spawn passed pointer is nil

2018-01-10 Thread 2vg
@Araq thx araq !

this code is garbage... so i removed it 

but thx !


Re: What can I do the help get to v1.0?

2018-01-10 Thread 2vg
Hi Kevin  Welcome Nim-land !!

you can see many [Issue](https://github.com/nim-lang/Nim/issues)

And... can make other wrapper not 
[Here](https://nim-lang.org/docs/lib.html#wrappers)


sometimes using spawn passed pointer is nil

2018-01-09 Thread 2vg
see this code [mofuw](https://github.com/2vg/mofuw/blob/master/mofuw.nim)

i trying make own async method approach

but, sometimes it works and seldom goes well ...

i found using spawn method passed pointer is nil.

i checked in this part 
[mofuw#L108](https://github.com/2vg/mofuw/blob/master/mofuw.nim#L108)

i think my code is not the best approach, but i think that this will work, but 
it does not work.

these are the code of the approach I am thinking about now

[mofuw#L15](https://github.com/2vg/mofuw/blob/master/mofuw.nim#L15)

[mofuw#L58](https://github.com/2vg/mofuw/blob/master/mofuw.nim#L58)

[mofuw#L93](https://github.com/2vg/mofuw/blob/master/mofuw.nim#L93)

[mofuw#L129](https://github.com/2vg/mofuw/blob/master/mofuw.nim#L129)

[mofuw#L181](https://github.com/2vg/mofuw/blob/master/mofuw.nim#L181)

[mofuw#L192](https://github.com/2vg/mofuw/blob/master/mofuw.nim#L192)

anyone know what is wrong ? is there any other good way ?


Re: Is anyone using the libuv wrappers?

2018-01-07 Thread 2vg
I have seen evt-tls now.

dont the code base to look so big, so I would like to write a wrapper or 
implement it pure.


Re: Is anyone using the libuv wrappers?

2018-01-07 Thread 2vg
Please use the rapper I made.

It supports APIs included in the latest version(libuv 1.18.1), and it can be 
used comfortably as API can be used like the original.

In order to use it, the latest libuv must be installed.(made when libuv 1.18.1)

Since this wrapper is still in beta, please send me a pull request to tell me 
if there is any problem.

[nimuv](https://github.com/2vg/nimuv)

this is toy project using my wrapper made by me.

However, it is faster than Rust's tokio-minihttp.

[mofuw_uv](https://github.com/2vg/mofuw/blob/master/mofuw_uv.nim)

If you can do an asynchronous approach yourself, using the event loop of the 
Selectors module will make for even faster things.

Or, if you do not support Windows, libev will be slightly faster than Selectors.

Here is the wrapper for libev.

[nimev](https://github.com/2vg/nimev)


Re: Sockets in Nim: I must choose between "select" and "accept"?

2018-01-04 Thread 2vg
> > "accept" in "nativesockets"?

you can use "accept".

this is example code.

check 
[accept](https://nim-lang.org/docs/posix.html#accept,SocketHandle,ptr.SockAddr,ptr.Socklen)

if you support only linux, can use accept4.

(can make socket non-blocking at the same time as accept.)


var server = newNativeSocket()

# ~
# bind, listen
# ~

var
  client: SocketHandle
  sockAddress: Sockaddr_in
  addrLen = SockLen(sizeof(sockAddress))
  
  # for accept4
  SOCK_NONBLOCK* {.importc, header: "".}: cint
  
  # for accept4
  SOCK_CLOEXEC* {.importc, header: "".}: cint

proc accept4(a1: cint, a2: ptr SockAddr, a3: ptr Socklen, flags: cint): cint
  {.importc, header: "".}

client = accept(server, cast[ptr SockAddr](addr(sockAddress)), 
addr(addrLen))

client = accept4(server, cast[ptr SockAddr](addr(sockAddress)), 
addr(addrLen), SOCK_NONBLOCK or SOCK_CLOEXEC)




Re: where "getIoHandler()" ?

2018-01-04 Thread 2vg
thx dom96 !! 


where "getIoHandler()" ?

2018-01-03 Thread 2vg
i saw this code 
[httpbeast](https://github.com/dom96/httpbeast/blob/master/src/httpbeast.nim#L216)

i saw a proc named getIoHandler for the first time.

which module is this proc ?


high load nil error on my web server

2017-12-29 Thread 2vg
hi, im developping high-performance libev's event-loop multi-thread web server.

this: [mofuw_ev](https://github.com/2vg/mofuw/blob/master/mofuw_ev.nim)

but, when exec 


 wrk -c 1000 http://localhost:8080

get nil error...

when the load becomes large, the server drops due to an error.

but, i dont know where the error is occurring.

im creating a web server for the first time, it may be wrong somewhere on my 
code.

can help me ? thx.