Skip to content

Commit

Permalink
new file: UdpEchoClient.lua
Browse files Browse the repository at this point in the history
	new file:   UdpEchoServer.lua
  • Loading branch information
William Adams committed Jul 29, 2013
1 parent a298486 commit 9443e70
Show file tree
Hide file tree
Showing 11 changed files with 366 additions and 37 deletions.
Binary file modified src/bin/tinn.exe
Binary file not shown.
143 changes: 122 additions & 21 deletions src/computicle/IOCPSocket.lua
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,14 @@ IOCPSocket.setKeepAlive = function(self, keepalive, timeout, interval)
return success, err
end

IOCPSocket.setNoDelay = function(self, nodelay)
local oneint = ffi.new("int[1]");
if nodelay then
oneint[0] = 1
end
IOCPSocket.setNoDelay = function(self, nodelay)
local oneint = ffi.new("int[1]");
if nodelay then
oneint[0] = 1
end

return WinSock.setsockopt(self:getNativeSocket(), IPPROTO_TCP, TCP_NODELAY, oneint, ffi.sizeof(oneint))
end
return WinSock.setsockopt(self:getNativeSocket(), IPPROTO_TCP, TCP_NODELAY, oneint, ffi.sizeof(oneint))
end

IOCPSocket.setNonBlocking = function(self, nonblocking)
local oneint = ffi.new("int[1]");
Expand All @@ -223,23 +223,23 @@ IOCPSocket.setNonBlocking = function(self, nonblocking)
return WinSock.ioctlsocket(self:getNativeSocket(), FIONBIO, oneint);
end

IOCPSocket.setReuseAddress = function(self, reuse)
local oneint = ffi.new("int[1]");
if reuse then
oneint[0] = 1
end
IOCPSocket.setReuseAddress = function(self, reuse)
local oneint = ffi.new("int[1]");
if reuse then
oneint[0] = 1
end

return WinSock.setsockopt(self:getNativeSocket(), SOL_SOCKET, SO_REUSEADDR, oneint, ffi.sizeof(oneint))
end
return WinSock.setsockopt(self:getNativeSocket(), SOL_SOCKET, SO_REUSEADDR, oneint, ffi.sizeof(oneint))
end

IOCPSocket.setExclusiveAddress = function(self, exclusive)
local oneint = ffi.new("int[1]");
if exclusive then
oneint[0] = 1
end
IOCPSocket.setExclusiveAddress = function(self, exclusive)
local oneint = ffi.new("int[1]");
if exclusive then
oneint[0] = 1
end

return WinSock.setsockopt(self:getNativeSocket(), SOL_SOCKET, SO_EXCLUSIVEADDRUSE, oneint, ffi.sizeof(oneint))
end
return WinSock.setsockopt(self:getNativeSocket(), SOL_SOCKET, SO_EXCLUSIVEADDRUSE, oneint, ffi.sizeof(oneint))
end

--[[
Reading Socket Options
Expand Down Expand Up @@ -474,6 +474,55 @@ IOCPSocket.send = function(self, buff, bufflen)
return bytes;
end

IOCPSocket.sendTo = function(self, lpTo, iTolen, buff, bufflen)
if lpTo == nil then
return false;
end


bufflen = bufflen or #buff

local lpBuffers = ffi.new("WSABUF", bufflen, ffi.cast("char *",buff));
local dwBufferCount = 1;
local lpNumberOfBytesSent = nil;
local dwFlags = 0;
local lpOverlapped = self:createOverlapped(ffi.cast("uint8_t *",buff), bufflen, SocketOps.WRITE);
local lpCompletionRoutine = nil;

local status = ws2_32.WSASendTo(self:getNativeSocket(),
lpBuffers,
dwBufferCount,
lpNumberOfBytesSent,
dwFlags,
ffi.cast("const struct sockaddr *", lpTo),
iTolen,
ffi.cast("OVERLAPPED *",lpOverlapped),
lpCompletionRoutine);


-- Do the following if we want to handle
-- immediate success
if status == 0 then
--print("#### IOCPSocket, WSASend STATUS == 0 ####")
-- return the number of bytes transferred
--return lpNumberOfBytesSent[0];
else
local err = ws2_32.WSAGetLastError();
if err ~= WSA_IO_PENDING then
print(" IOCPSocket.send, ERROR: ", err);
return false, err;
end
end

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

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

-- BUGBUG
return bytes;
end




IOCPSocket.receive = function(self, buff, bufflen)
Expand Down Expand Up @@ -524,6 +573,58 @@ IOCPSocket.receive = function(self, buff, bufflen)
end


IOCPSocket.receiveFrom = function(self, lpFrom, fromLen, buff, bufflen)

local lpBuffers = ffi.new("WSABUF", bufflen, buff);
local dwBufferCount = 1;
-- local lpNumberOfBytesRecvd = ffi.new("DWORD[1]");
-- if we're going to use io completion ports, then
-- the numberOfBytesRecvd MUST be == nil
local lpNumberOfBytesRecvd = nil;
local lpFlags = ffi.new("DWORD[1]");
local lpOverlapped = self:createOverlapped(buff, bufflen, SocketOps.READ);
local lpCompletionRoutine = nil;
local lpFromlen = ffi.new("DWORD[1]", fromLen);

local status = ws2_32.WSARecvFrom(self:getNativeSocket(),
lpBuffers,
dwBufferCount,
lpNumberOfBytesRecvd,
lpFlags,
ffi.cast("struct sockaddr *", lpFrom),
lpFromlen,
ffi.cast("OVERLAPPED *",lpOverlapped),
lpCompletionRoutine);


--print("IOCPSocket.receive, WSARecv(), STATUS: ", status);

-- if the return value is == 0, then the transfer
-- to the network stack has already completed,
-- but the completion notification has not necessarily
-- happened, so treat it the same as PENDING
if status == 0 then
--print("#### IOCPSocket.WSARecv, STATUS == 0 ####");

--return lpNumberOfBytesRecvd[0];
else
-- didn't get bytes immediately, so see if it's a 'pending'
-- or some other error
local err = ws2_32.WSAGetLastError();

if err ~= WSA_IO_PENDING then
print("IOCPSocket.WSARecv, ERROR: ", err);
return false, err;
end
end

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

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

return bytes;
end



return IOCPSocket;
2 changes: 1 addition & 1 deletion src/net/SocketServer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ end


SocketServer.handleAccepted = function(self, sock)
--print("SocketServer.handleAccepted(): ", sock);
print("SocketServer.handleAccepted(): ", sock);

if self.OnAccept then
--print("CALLING self.OnAccept")
Expand Down
7 changes: 3 additions & 4 deletions src/win32/apiset/WinCon.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ local ffi = require("ffi");

local NOGDI = true;

ffi.cdef[[
typedef int16_t SHORT;
]]
local basetsd = require("basetsd");


ffi.cdef[[
typedef struct _COORD {
Expand Down Expand Up @@ -245,7 +244,7 @@ static const int ENABLE_WRAP_AT_EOL_OUTPUT = 0x0002;



typedef BOOL ( *PHANDLER_ROUTINE)(DWORD CtrlType);
typedef BOOL (__stdcall *PHANDLER_ROUTINE)(DWORD CtrlType);

typedef struct _CONSOLE_READCONSOLE_CONTROL {
ULONG nLength;
Expand Down
18 changes: 17 additions & 1 deletion src/win32/apiset/core_console_l1_1_0.lua
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,20 @@ WriteConsoleW(HANDLE hConsoleOutput,
LPVOID lpReserved);
]]

return k32Lib;
return {
AllocConsole = k32Lib.AllocConsole,
GetConsoleCP = k32Lib.GetConsoleCP,
GetConsoleMode = k32Lib.GetConsoleMode,
GetConsoleOutputCP = k32Lib.GetConsoleOutputCP,
GetNumberOfConsoleInputEvents = k32Lib.GetNumberOfConsoleInputEvents,
PeekConsoleInputA = k32Lib.PeekConsoleInputA,
ReadConsoleA = k32Lib.ReadConsoleA,
ReadConsoleInputA = k32Lib.ReadConsoleInputA,
ReadConsoleInputW = k32Lib.ReadConsoleInputW,
ReadConsoleW = k32Lib.ReadConsoleW,
SetConsoleCtrlHandler = k32Lib.SetConsoleCtrlHandler,
SetConsoleMode = k32Lib.SetConsoleMode,
WriteConsoleA = k32Lib.WriteConsoleA,
WriteConsoleW = k32Lib.WriteConsoleW,
}

29 changes: 26 additions & 3 deletions src/win32/apiset/ws2_32.lua
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,16 @@ typedef struct __WSABUF {
char * buf;
} WSABUF, *LPWSABUF;
]]
WSABUF = ffi.typeof("WSABUF");
WSABUF_mt = {
__new = function(ct, buf, len)
return ffi.new(ct, len, ffi.cast("char *", buf));
end,

__index = {

},
}
--
-- MSTcpIP.h
--
Expand Down Expand Up @@ -988,6 +997,18 @@ int WSARecv(
LPWSAOVERLAPPED lpOverlapped,
void* lpCompletionRoutine);

int
WSARecvFrom(
SOCKET s,
LPWSABUF lpBuffers,
DWORD dwBufferCount,
LPDWORD lpNumberOfBytesRecvd,
LPDWORD lpFlags,
struct sockaddr * lpFrom,
LPINT lpFromlen,
LPWSAOVERLAPPED lpOverlapped,
LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine);


int WSASend(SOCKET s,
LPWSABUF lpBuffers,
Expand Down Expand Up @@ -1321,10 +1342,10 @@ WSApSetPostRoutine = Lib.WSApSetPostRoutine,
--]]

WSARecv = Lib.WSARecv,
WSARecvFrom = Lib.WSARecvFrom,

--[[
WSARecvDisconnect = Lib.WSARecvDisconnect,
WSARecvFrom = Lib.WSARecvFrom,
WSARemoveServiceClass = Lib.WSARemoveServiceClass,
WSAResetEvent = Lib.WSAResetEvent,
--]]
Expand All @@ -1334,10 +1355,12 @@ WSAResetEvent = Lib.WSAResetEvent,
--[[
WSASendDisconnect = Lib.
WSASendMsg = Lib.
WSASendTo = Lib.
WSASetBlockingHook = Lib.
--]]

WSASendTo = Lib.WSASendTo,

-- deprecated: WSASetBlockingHook = Lib.

WSASetEvent = Lib.WSASetEvent,

--[[
Expand Down
13 changes: 12 additions & 1 deletion tests/LineServer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,18 @@ local OnAccept = function(param, sock)
print("LINE: ", line);

-- write the line back out to the socket
socket:send(line, #line);
local body = "<html><body>"..line.."</body></html>\r\n"
local content = [[
HTTP/1.1 200 OK
Connection: close
]]..body

print("CONTENT");
print(content);

socket:send(content, #content);
socket:closeDown();
--socket:send(buff, bytesread);
end

Expand Down
23 changes: 17 additions & 6 deletions tests/SToken.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,28 @@ local SToken_mt = {
end,

__eq = function(self, other)
print("SToken:__eq()");
local maxbytes = math.min(self.Length, other.Length);
-- print("SToken:__eq(): ", type(self), type(other));

local otherData = ffi.cast("const uint8_t *", other);
local otherLength = #other;
local otherOffset = 0;

if type(other) == "string" then
else
otherOffset = other.Offset;
end


local maxbytes = math.min(self.Length, otherLength);

for i=0,maxbytes-1 do
if self.Data[self.Offset+i] > other.Data[other.Offset+i] then
print("RETURN 1.0");
if self.Data[self.Offset+i] > otherData[otherOffset+i] then
--print("RETURN 1.0");
return false
end

if self.Data[self.Offset+i] < other.Data[other.Offset+i] then
print("RETURN 2.0");
if self.Data[self.Offset+i] < otherData[otherOffset+i] then
--print("RETURN 2.0");
return false
end
end
Expand Down
Loading

0 comments on commit 9443e70

Please sign in to comment.