Skip to content

Commit

Permalink
Add a bit of docs to GenTCP.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Mar 13, 2011
1 parent 1e7a837 commit a5be74d
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 48 deletions.
5 changes: 3 additions & 2 deletions examples/gen_tcp_client.ex
@@ -1,5 +1,6 @@
% Start the server in examples/gen_tcp_server.ex and then run this file
Code.require "gen_tcp"

{'ok, tcp} = GenTCP.connect("localhost", 5678, ['binary, {'packet, 0}])
'ok = tcp.send("Some Data")
'ok = tcp.close

'ok = tcp.close
26 changes: 13 additions & 13 deletions examples/gen_tcp_server.ex
@@ -1,24 +1,24 @@
Code.require "gen_tcp"

module Server

def do_recv(tcp, sock, bs)
case tcp.recv(sock,0)
match {'ok, b}
do_recv(tcp, sock, [bs, b])
match {'error, 'closed}
{'ok, Erlang.list_to_binary(bs)}
end
end

def start
{'ok, tcp} = GenTCP.listen(5678, ['binary, {'packet, 0}, {'active, false}])

{'ok, sock} = tcp.accept
{'ok, bin} = do_recv(tcp, sock, [])
{'ok, bin} = recv(tcp, sock, [])
'ok = tcp.close
IO.puts bin.to_s
end

private

def recv(tcp, sock, bs)
case tcp.recv(sock,0)
match {'ok, b}
recv(tcp, sock, [bs, b])
match {'error, 'closed}
{'ok, bs.to_bin}
end
end
end

Server.start
Server.start
97 changes: 64 additions & 33 deletions lib/gen_tcp.ex
@@ -1,72 +1,103 @@
% Interface to Erlang's gen_tcp. Here is a simple example of client/server:
%
% Code.require "gen_tcp"
%
% module Server
% def start
% {'ok, tcp} = GenTCP.listen(5678, ['binary, {'packet, 0}, {'active, false}])
% {'ok, sock} = tcp.accept
% {'ok, bin} = recv(tcp, sock, [])
% 'ok = tcp.close
% IO.puts bin.to_s
% end
%
% private
%
% def recv(tcp, sock, bs)
% case tcp.recv(sock,0)
% match {'ok, b}
% recv(tcp, sock, [bs, b])
% match {'error, 'closed}
% {'ok, bs.to_bin}
% end
% end
% end
%
% Server.start
%
% After starting the server file above, add the client below to a file (or run it
% directly from iex):
%
% Code.require "gen_tcp"
% {'ok, tcp} = GenTCP.connect("localhost", 5678, ['binary, {'packet, 0}])
% 'ok = tcp.send("Some Data")
% 'ok = tcp.close
%
% You will see the server will print <<"Some Data">> (as binary) and then exit.
object GenTCP
module Mixin
def listen(port, options)
case Erlang.gen_tcp.listen(port, options)
match { 'ok, sock }
{ 'ok, GenTCP.new(sock) }
match other
other
end
init_socket Erlang.gen_tcp.listen(port, options)
end
def listen(port, options, timeout)
case Erlang.gen_tcp.listen(port, options, timeout)
match { 'ok, sock }
{ 'ok, GenTCP.new(sock) }
match other
other
end
init_socket Erlang.gen_tcp.listen(port, options, timeout)
end
def connect(address, port, options)
connect(address, port, options, 0)
end
init_socket Erlang.gen_tcp.connect(address.to_char_list, port, options)
end
def connect(address, port, options, timeout)
case Erlang.gen_tcp.connect(address.to_char_list, port, options, timeout)
match { 'ok, sock }
{ 'ok, GenTCP.new(sock) }
init_socket Erlang.gen_tcp.connect(address.to_char_list, port, options, timeout)
end
private
def init_socket(result)
case result
match { 'ok, socket }
{ 'ok, GenTCP.new(socket) }
match other
other
end
end

end
end
def constructor(sock)
{'sock: sock}
def constructor(socket)
{'socket: socket}
end

def controlling_process(pid)
Erlang.gen_tcp.controlling_process(@sock, pid)
Erlang.gen_tcp.controlling_process(@socket, pid)
end

def recv(sock, length)
Erlang.gen_tcp.recv(sock, length)
def recv(socket, length)
Erlang.gen_tcp.recv(socket, length)
end

def recv(sock, length, timeout)
Erlang.gen_tcp.recv(sock, length, timeout)
def recv(socket, length, timeout)
Erlang.gen_tcp.recv(socket, length, timeout)
end

def accept
Erlang.gen_tcp.accept(@sock)
Erlang.gen_tcp.accept(@socket)
end

def accept(timeout)
Erlang.gen_tcp.accept(@sock, timeout)
Erlang.gen_tcp.accept(@socket, timeout)
end

def send(message)
Erlang.gen_tcp.send(@sock, message.to_bin)
Erlang.gen_tcp.send(@socket, message.to_bin)
end

def close
Erlang.gen_tcp.close(@sock)
end
Erlang.gen_tcp.close(@socket)
end

def shutdown
Erlang.gen_tcp.shutdown(@sock)
Erlang.gen_tcp.shutdown(@socket)
end
end

0 comments on commit a5be74d

Please sign in to comment.