adding source code from first video
migration from gitlab
This commit is contained in:
@@ -0,0 +1,32 @@
|
|||||||
|
# C3hat
|
||||||
|
|
||||||
|
## Chat App in C3
|
||||||
|
|
||||||
|
Hello, this is a very basic chat application built in C3 for learning and fun. Don't take this repo seriously. Of course I am learning C3 so if there is something that could be done better in the C3 way let me know!
|
||||||
|
|
||||||
|
## Videos
|
||||||
|
|
||||||
|
This will be a series on my YouTube channel: [here](https://www.youtube.com/@aveydotdev)
|
||||||
|
### Roadmap
|
||||||
|
|
||||||
|
#### Server
|
||||||
|
- [ ] Accept Connection
|
||||||
|
- [ ] Threading
|
||||||
|
- [ ] Broadcast messages to all clients
|
||||||
|
- [ ] Parse Commands
|
||||||
|
- [ ] Channels or Chat Rooms
|
||||||
|
- [ ] Private Messages
|
||||||
|
|
||||||
|
#### Client
|
||||||
|
- [ ] Send Messages
|
||||||
|
- [ ] Stay Connected to Server
|
||||||
|
- [ ] Send Commands
|
||||||
|
- [ ] Join Channels or Chat Rooms
|
||||||
|
- [ ] Send Private Messages
|
||||||
|
- [ ] GUI
|
||||||
|
|
||||||
|
The GUI will be necessary because when a user is typing and someone else sends a message the user will have their message over written by the incoming message. This could be handled with threading, but learning C3 and creating a simple GUI will be a lesson opportunity.
|
||||||
|
|
||||||
|
## Why
|
||||||
|
|
||||||
|
I decided to build this because it's fun and secondly to really learn a language it's a good idea to build something more than trivial. Your "hello world" application isn't teaching you much about the language you are learning outside of printing to standard out, compiling and/or running.
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
{
|
||||||
|
// Language version of C3.
|
||||||
|
"langrev": "1",
|
||||||
|
// Warnings used for all targets.
|
||||||
|
"warnings": [ "no-unused" ],
|
||||||
|
// Directories where C3 library files may be found.
|
||||||
|
"dependency-search-paths": [ "lib" ],
|
||||||
|
// Libraries to use for all targets.
|
||||||
|
"dependencies": [ ],
|
||||||
|
// Authors, optionally with email.
|
||||||
|
"authors": [ "John Doe <john.doe@example.com>" ],
|
||||||
|
// Version using semantic versioning.
|
||||||
|
"version": "0.1.0",
|
||||||
|
// Sources compiled for all targets.
|
||||||
|
"sources": [ "src/**" ],
|
||||||
|
// Test sources compiled for all targets.
|
||||||
|
"test-sources": [ "test/**" ],
|
||||||
|
// C sources if the project also compiles C sources
|
||||||
|
// relative to the project file.
|
||||||
|
// "c-sources": [ "csource/**" ],
|
||||||
|
// Include directories for C sources relative to the project file.
|
||||||
|
// "c-include-dirs": [ "csource/include" ],
|
||||||
|
// Build location, relative to project file.
|
||||||
|
"build-dir": "build",
|
||||||
|
// Output location, relative to project file.
|
||||||
|
"output": "build",
|
||||||
|
// Architecture and OS target.
|
||||||
|
// You can use 'c3c --list-targets' to list all valid targets.
|
||||||
|
// "target": "windows-x64",
|
||||||
|
// Targets.
|
||||||
|
"targets": {
|
||||||
|
"c3hat": {
|
||||||
|
// Executable or library.
|
||||||
|
"type": "executable",
|
||||||
|
// Additional libraries, sources
|
||||||
|
// and overrides of global settings here.
|
||||||
|
},
|
||||||
|
},
|
||||||
|
// Global settings.
|
||||||
|
// CPU name, used for optimizations in the LLVM backend.
|
||||||
|
"cpu": "generic",
|
||||||
|
// Optimization: "O0", "O1", "O2", "O3", "O4", "O5", "Os", "Oz".
|
||||||
|
"opt": "O0"
|
||||||
|
// See resources/examples/project_all_settings.json and 'c3c --list-project-properties' to see more properties.
|
||||||
|
}
|
||||||
+61
@@ -0,0 +1,61 @@
|
|||||||
|
module c3hat;
|
||||||
|
import std::io;
|
||||||
|
import std::net;
|
||||||
|
import std::collections;
|
||||||
|
|
||||||
|
fn int main(String[] args) {
|
||||||
|
String host = "localhost";
|
||||||
|
uint port = 9500;
|
||||||
|
uint backlog = 100;
|
||||||
|
bool shouldQuit = false;
|
||||||
|
const int BUFFSIZE = 4096;
|
||||||
|
|
||||||
|
TcpServerSocket? server = tcp::listen(
|
||||||
|
host, port, backlog,
|
||||||
|
SocketOption.REUSEADDR,
|
||||||
|
ip_protocol: IpProtocol.IPV4);
|
||||||
|
|
||||||
|
if (catch err = server) {
|
||||||
|
io::printfn("unable to create server socket %s", err);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
defer {
|
||||||
|
if (catch err = server.close()) {
|
||||||
|
io::printfn("error closing socket %s", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
while (!shouldQuit) {
|
||||||
|
TcpSocket? client = tcp::accept(&server);
|
||||||
|
if (catch err = client) {
|
||||||
|
io::printfn("error accepting client %s", err);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
defer {
|
||||||
|
if (catch err = client.close()) {
|
||||||
|
io::printfn("error closing client socket %s", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
char[BUFFSIZE] buffer;
|
||||||
|
|
||||||
|
List{String} data;
|
||||||
|
data.init(mem);
|
||||||
|
while (try usz read = client.read(&buffer)) {
|
||||||
|
//io::printfn("buffer %s", (String)buffer[:read]);
|
||||||
|
data.push((String)buffer[:read]);
|
||||||
|
if (read < BUFFSIZE) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (item : data) {
|
||||||
|
io::printfn("%s", item);
|
||||||
|
if (item.contains("quit")) {
|
||||||
|
shouldQuit = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user