This repository has been archived by the owner on Oct 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added sample for Console and Socket modules.
Added sample for Console and Socket modules.
- Loading branch information
1 parent
3fd23f3
commit 4b85381
Showing
1 changed file
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
----------------------------------------------------------------------------- | ||
-- This sample works fine with PC utility, CHMM2 sender -- | ||
-- You can find the source of this utility here: -- | ||
-- https://github.com/Rinnegatamante/CHMM2/blob/master/tools/sender/main.c -- | ||
----------------------------------------------------------------------------- | ||
|
||
-- Init socketing and create a server socket on Port 5000 | ||
Socket.init() | ||
server = Socket.createServerSocket(5000) | ||
|
||
-- Create a debug console | ||
cns = Console.new(TOP_SCREEN) | ||
state = 0 | ||
|
||
while true do | ||
|
||
-- Clear screen | ||
Screen.refresh() | ||
Screen.clear(TOP_SCREEN) | ||
|
||
-- State 0: Looking for connections | ||
if state == 0 then | ||
|
||
-- Writing info on screen | ||
Console.append(cns, "Looking for connections...") | ||
Console.append(cns, "\nIP: "..Network.getIPAddress()) | ||
Console.show(cns) | ||
Screen.flip() | ||
Screen.waitVblankStart() | ||
Console.clear(cns) | ||
|
||
-- Searching for clients | ||
client = Socket.accept(server) | ||
if client == nil then | ||
state = 0 | ||
else | ||
state = 1 | ||
end | ||
|
||
-- State 1: Connection estabilished, sending response to client | ||
elseif state == 1 then | ||
|
||
-- Writing info on screen | ||
Console.append(cns, "Sending magic...\n") | ||
Console.show(cns) | ||
Screen.flip() | ||
Screen.waitVblankStart() | ||
Console.clear(cns) | ||
|
||
-- Sending magic word expected from client to communicate | ||
Socket.send(client, "YATA SENDER") | ||
state = 2 | ||
|
||
-- Opening file to be received | ||
new_theme = io.open("/tmp.zip",FCREATE) | ||
offs = 0 | ||
|
||
-- State 2: File receiver | ||
elseif state == 2 then | ||
|
||
-- Writing info on screen | ||
Console.append(cns, "Receiving file...\n") | ||
Console.show(cns) | ||
Screen.flip() | ||
Screen.waitVblankStart() | ||
|
||
-- File receiver on work with 256 bytes packets | ||
block = Socket.receive(client, 256) | ||
while string.len(block) > 0 do | ||
io.write(new_theme, offs, block, string.len(block)) | ||
offs = offs + string.len(block) | ||
block = Socket.receive(client, 256) | ||
end | ||
|
||
-- Closing sockets instances and file | ||
io.close(new_theme) | ||
Socket.close(server) | ||
Socket.close(client) | ||
Socket.term() | ||
|
||
-- Writing info on screen | ||
Console.clear(cns) | ||
Console.append(cns, "File received\nPress START to exit.") | ||
state = 3 | ||
|
||
else | ||
|
||
-- Script finished | ||
Console.show(cns) | ||
Screen.flip() | ||
Screen.waitVblankStart() | ||
if Controls.check(Controls.read(), KEY_START) then | ||
System.exit() | ||
end | ||
|
||
end | ||
|
||
end |