Browse Source

use pgsql_wire to encode messages

Anton Lebedevich 13 years ago
parent
commit
835fd65076
2 changed files with 11 additions and 15 deletions
  1. 9 11
      src/pgsql_sock.erl
  2. 2 4
      src/pgsql_wire.erl

+ 9 - 11
src/pgsql_sock.erl

@@ -39,7 +39,7 @@ init([C, Host, Username, Opts]) ->
     State = #state{
     State = #state{
       mod  = gen_tcp,
       mod  = gen_tcp,
       sock = S,
       sock = S,
-      tail = <<>>},
+      decoder = pgsql_wire:init([])},
 
 
     case proplists:get_value(ssl, Opts) of
     case proplists:get_value(ssl, Opts) of
         T when T == true; T == required ->
         T when T == true; T == required ->
@@ -51,17 +51,12 @@ init([C, Host, Username, Opts]) ->
     end,
     end,
 
 
     setopts(State2, [{active, true}]),
     setopts(State2, [{active, true}]),
-    send(self(), [<<196608:32>>, Opts3, 0]),
+    send([<<196608:32>>, Opts3, 0], State2),
     {ok, State2}.
     {ok, State2}.
 
 
 handle_call(Call, _From, State) ->
 handle_call(Call, _From, State) ->
     {stop, {unsupported_call, Call}, State}.
     {stop, {unsupported_call, Call}, State}.
 
 
-handle_cast({send, Data}, State) ->
-    #state{mod = Mod, sock = Sock} = State,
-    ok = Mod:send(Sock, Data),
-    {noreply, State};
-
 handle_cast(cancel, State = #state{backend = {Pid, Key}}) ->
 handle_cast(cancel, State = #state{backend = {Pid, Key}}) ->
     {ok, {Addr, Port}} = inet:peername(State#state.sock),
     {ok, {Addr, Port}} = inet:peername(State#state.sock),
     SockOpts = [{active, false}, {packet, raw}, binary],
     SockOpts = [{active, false}, {packet, raw}, binary],
@@ -69,10 +64,7 @@ handle_cast(cancel, State = #state{backend = {Pid, Key}}) ->
     Msg = <<16:?int32, 80877102:?int32, Pid:?int32, Key:?int32>>,
     Msg = <<16:?int32, 80877102:?int32, Pid:?int32, Key:?int32>>,
     ok = gen_tcp:send(Sock, Msg),
     ok = gen_tcp:send(Sock, Msg),
     gen_tcp:close(Sock),
     gen_tcp:close(Sock),
-    {noreply, State};
-
-handle_cast(Cast, State) ->
-    {stop, {unsupported_cast, Cast}, State}.
+    {noreply, State}.
 
 
 handle_info({_, _Sock, Data}, #state{tail = Tail} = State) ->
 handle_info({_, _Sock, Data}, #state{tail = Tail} = State) ->
     State2 = decode(<<Tail/binary, Data/binary>>, State),
     State2 = decode(<<Tail/binary, Data/binary>>, State),
@@ -113,6 +105,12 @@ setopts(#state{mod = Mod, sock = Sock}, Opts) ->
         ssl     -> ssl:setopts(Sock, Opts)
         ssl     -> ssl:setopts(Sock, Opts)
     end.
     end.
 
 
+send(Data, State#state{mod = Mod, sock = Sock, decoder = Decoder}) ->
+    Mod:send(Sock, pgsql_wire:encode(Data, Decoder)).
+
+send(Type, Data, State#state{mod = Mod, sock = Sock, decoder = Decoder}) ->
+    Mod:send(Sock, pgsql_wire:encode(Type, Data, Decoder)).
+
 decode(<<Type:8, Len:?int32, Rest/binary>> = Bin, #state{c = C} = State) ->
 decode(<<Type:8, Len:?int32, Rest/binary>> = Bin, #state{c = C} = State) ->
     Len2 = Len - 4,
     Len2 = Len - 4,
     case Rest of
     case Rest of

+ 2 - 4
src/pgsql_wire.erl

@@ -17,10 +17,8 @@ decode(Data, State = #state{}) ->
 
 
 encode(Data, State = #state{}) ->
 encode(Data, State = #state{}) ->
     Bin = iolist_to_binary(Data),
     Bin = iolist_to_binary(Data),
-    Msg = <<(byte_size(Bin) + 4):?int32, Bin/binary>>,
-    {Msg, State}.
+    <<(byte_size(Bin) + 4):?int32, Bin/binary>>.
 
 
 encode(Type, Data, State = #state{}) ->
 encode(Type, Data, State = #state{}) ->
     Bin = iolist_to_binary(Data),
     Bin = iolist_to_binary(Data),
-    Msg = <<Type:8, (byte_size(Bin) + 4):?int32, Bin/binary>>,
-    {Msg, State}.
+    <<Type:8, (byte_size(Bin) + 4):?int32, Bin/binary>>.