Re: Can't build asyncnet example on macosx

2016-08-15 Thread qqtop
@dom96

I would vote against sub-forums for different languages and rather suggest to 
anyone to use one of the translator plugins in modern browsers like:

[https://addons.mozilla.org/id/firefox/addon/s3google-translator](https://addons.mozilla.org/id/firefox/addon/s3google-translator)/

or others. Most people hardly have enough time to keep up with the main-forum. 


Re: Can't build asyncnet example on macosx

2016-08-15 Thread _tulayang
@OderWat

Don't mind.


Re: Can't build asyncnet example on macosx

2016-08-14 Thread dom96
Personally I think that whatever users can use to communicate most easily is 
fine. We do need to improve this forum to contain sub-forums for different 
languages though IMO.


Re: Can't build asyncnet example on macosx

2016-08-14 Thread Angluca
Sorry, Don't care about details please.:)

This error maybe don't use global variables directly in proc can correct build 
it.


Re: Can't build asyncnet example on macosx

2016-08-14 Thread OderWat
I was of the opinion that this forums is englisch only?


Re: Can't build asyncnet example on macosx

2016-08-14 Thread Angluca
赞一下:), 谢谢了, 知道怎么不报错就行。

用callback只是想更细致的操作buf, 也只是试试先, 最近才研究nim的async和threadpool这块.


Re: Can't build asyncnet example on macosx

2016-08-14 Thread _tulayang
不过我还是建议你用 {.async.} , 省不少事情,性能其实损失不了多少:


import asyncnet, asyncdispatch

proc recvAll(sock: AsyncSocket) {.async.} =
  for i in 0 .. 50:
var ret = await recv(sock, 10)
echo("Read ", ret.len, ": ", ret.repr)

proc main() {.async.} =
  var sock = newAsyncSocket()
  await sock.connect("irc.freenode.net", Port(6667))
  await sock.recvAll()

waitFor main()



Re: Can't build asyncnet example on macosx

2016-08-14 Thread _tulayang
OO!

我加了个 {.gcsafe.} 可以通过了:


import asyncnet, asyncdispatch

var sock = newAsyncSocket()
var f = connect(sock, "irc.freenode.net", Port(6667))
f.callback =
  proc (future: Future[void]) {.gcsafe.} =
echo("Connected in future!")
echo repr sock
for i in 0 .. 50:
  var recvF = recv(sock, 10)
  recvF.callback =
proc (future: Future[string]) =
  echo("Read ", future.read.len, ": ", future.read.repr)

runForever()


如果不加的话,会出现 Error:


Error: type mismatch: got (Future[system.void], proc (future: 
Future[system.void]){.locks: .})
but expected one of:
proc callback=(future: FutureBase; cb: proc ())
proc callback=[T](future: Future[T];
 cb: proc (future: Future[T]))



Re: Can't build asyncnet example on macosx

2016-08-14 Thread Angluca
好吧, 不错! 不过我就是用的官方例子改了下, 也是这个错误. (asyncnet.nim 最下面) 


var sock = newAsyncSocket()
var f = connect(sock, "irc.freenode.net", Port(6667))
f.callback =
  proc (future: Future[void]) =
echo("Connected in future!")
for i in 0 .. 50:
  var recvF = recv(sock, 10)
  recvF.callback =
proc (future: Future[string]) =
  echo("Read ", future.read.len, ": ", future.read.repr)


之前我发的例子不知道关闭线程检测能不能编译成功, 回去试试.


Re: Can't build asyncnet example on macosx

2016-08-13 Thread _tulayang
这样才是正确的:


import asyncnet, asyncdispatch

var sock = newAsyncSocket()

proc onConnect(sock: AsyncSocket, host: string, port: Port) =
  var ft = connect(sock, "127.0.0.1", Port(12345))
  ft.callback = proc (future: Future[void]) =
echo("Connected in future!")
for i in 0 .. 50:
  var recvF = recv(sock, 10)
  recvF.callback =
proc (future: Future[string]) =
  echo("Read ", future.read.len, ": ", future.read.repr)

sock.onConnect("127.0.0.1", Port(12345))
runForever()


你的版本 onConnect 里边的 sock 被认为是 GC 不安全的 --- 编译器认为,搞不好下一次 pull 
的时候,其内存已经不在了,所以不让你编译通过。

但是放在嵌套函数中,Nim 会在内部始终保存其一个副本。

:)


Re: Can't build asyncnet example on macosx

2016-08-13 Thread OderWat
It works when put it into a proc. But it does not do what I expect from it. It 
only reads 10 chars and in the next turn it reads nothing anymore.


import asyncnet, asyncdispatch
import asyncnet, asyncdispatch

proc main() =
  var sock = newAsyncSocket()
  
  proc onConnect() =
echo("Connected in future!")
asyncCheck sock.send("GET /\r\l\r\l")
for i in 0 .. 50:
  var recvF = recv(sock, 10)
  recvF.callback =
proc (future: Future[string]) =
  echo("Read ", future.read.len, ": ", future.read.repr)
  
  var ft = connect(sock, "nim-lang.org", Port(80))
  ft.callback = onConnect
  
  runForever()

main()