Skip to content

Commit

Permalink
modified: src/computicle/IOCPSocket.lua
Browse files Browse the repository at this point in the history
  • Loading branch information
William Adams committed Jul 30, 2013
1 parent 9443e70 commit 604ad35
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 37 deletions.
Binary file modified src/bin/tinn.exe
Binary file not shown.
10 changes: 9 additions & 1 deletion src/computicle/IOCPSocket.lua
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,14 @@ IOCPSocket.bind = function(self, addr, addrlen)
return WinSock.bind(self:getNativeSocket(), addr, addrlen)
end

IOCPSocket.bindToPort = function(self, port)
local addr = sockaddr_in(port);
local addrlen = ffi.sizeof(addr);

return self:bind(addr,addrlen)
end


--[[
Data Transport
--]]
Expand Down Expand Up @@ -620,7 +628,7 @@ IOCPSocket.receiveFrom = function(self, lpFrom, fromLen, buff, bufflen)

local key, bytes, ovl = IOProcessor:yieldForIo(self, SocketOps.READ, lpOverlapped.opcounter);

print("WSARecvFrom: ", key, bytes, ovl);
--print("WSARecvFrom: ", key, bytes, ovl);

return bytes;
end
Expand Down
24 changes: 13 additions & 11 deletions tests/UdpEchoClient.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ local serviceport = 9090
local argv = {...}
local argc = #argv

local EchoRequest = function(socket, addr, addrlen)
local EchoRequest = function(socket, addr, addrlen, phrase)
-- fill the buffer with current time
local datestr = os.date("%c");
phrase = phrase or os.date("%c");

local bytessent, err = socket:sendTo(addr, addrlen, datestr, #datestr);
local bytessent, err = socket:sendTo(addr, addrlen, phrase, #phrase);

print("bytessent: ", bytessent, err);
--print("bytessent: ", bytessent, err);

if not bytessent then
print("sendTo ERROR: ", bytessent, err);
Expand All @@ -40,19 +40,21 @@ print("bytessent: ", bytessent, err);
end

if bytesreceived > 0 then
print("RECEIVED: ", bytesreceived);
return ffi.string(buff, bytesreceived);
--print("RECEIVED: ", bytesreceived);
--return ffi.string(buff, bytesreceived);
return true
end

return string.format("bytesreceived == %d", bytesreceived);
end



loop = function()
loop = function(phrase)
local sw = StopWatch();

local iterations = tonumber(argv[1]) or 1;

--print("iterations: ", iterations);


Expand All @@ -73,19 +75,19 @@ loop = function()
local transcount = 0;

for i=1,iterations do
local dtc, err = EchoRequest(socket, addr, addrlen);
local dtc, err = EchoRequest(socket, addr, addrlen, phrase);

if dtc then
transcount = transcount + 1;
print(transcount, dtc, transcount/sw:Seconds());
--print(transcount, dtc, transcount/sw:Seconds());
else
print("Error: ", i, err);
end
collectgarbage();
--collectgarbage();
end

print("Transactions: ", transcount, transcount/sw:Seconds());
end

run(loop);
run(loop, "Hello, World!");

38 changes: 13 additions & 25 deletions tests/UdpEchoServer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,25 @@ local ffi = require("ffi");

local IOProcessor = require("IOProcessor");
local IOCPSocket = require("IOCPSocket");
local ws2_32 = require("ws2_32");

--IOProcessor:setMessageQuanta(5);
local serverport = 9090;


local createServerSocket = function(port)
local port = port or serverport;
local socket, err = IOCPSocket:create(AF_INET, SOCK_DGRAM, 0);
-- Setup the server socket
local socket, err = IOCPSocket:create(AF_INET, SOCK_DGRAM, 0);

if not socket then
print("create socket ERROR: ", err);
return nil, err;
end
--if not socket then
-- print("create socket ERROR: ", err);
-- return nil, err;
--end

local addr = sockaddr_in(port);
local addrlen = ffi.sizeof(addr);
local success, err = socket:bindToPort(serverport);

success, err = socket:bind(addr,addrlen)
--if not success then
-- print("bindToPort, ERROR: ", err);
-- return false, err;
--end

if not success then
return false, err;
end

return socket;
end

-- The primary application loop
local loop = function()
Expand All @@ -36,12 +29,7 @@ local loop = function()
local from = sockaddr_in();
local fromLen = ffi.sizeof(from);

local socket, err = createServerSocket(serverport);

if not socket then
print("createServerSocket Error: ", err);
return false, err;
end

while true do
local bytesread, err = socket:receiveFrom(from, fromLen, buff, bufflen);
Expand All @@ -51,9 +39,9 @@ local loop = function()
return false, err;
end

print("BYTESREAD: ", bytesread, from);
--print("BYTESREAD: ", bytesread, from);

print(ffi.string(buff, bytesread));
--print(ffi.string(buff, bytesread));

-- echo back to sender
local bytessent, err = socket:sendTo(from, fromLen, buff, bufflen);
Expand Down

0 comments on commit 604ad35

Please sign in to comment.