-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
unfinished clean up, server/client improvements + examples
- Loading branch information
Showing
17 changed files
with
732 additions
and
112 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 |
---|---|---|
@@ -1 +0,0 @@ | ||
_examples | ||
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
Binary file not shown.
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,79 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net" | ||
"time" | ||
|
||
knet "kinetra.de/net" | ||
"kinetra.de/net/examples/chat/packets" | ||
"kinetra.de/net/examples/chat/packets/initialization" | ||
) | ||
|
||
var ErrResponse = errors.New("response error") | ||
|
||
func main() { | ||
client := &knet.Client{ | ||
RootKeyFile: "client.kr", | ||
BufferSize: 1024, | ||
WriteDeadline: 10 * time.Second, | ||
HandshakeReadDeadline: 500 * time.Millisecond, | ||
HandshakeWriteDeadline: 500 * time.Millisecond, | ||
} | ||
|
||
client.OnRead = func(conn net.Conn, info knet.ReadInfo) knet.AfterAction { | ||
err := client.UnmarshalPacket(info.Data, func(id int, b []byte) (err error) { | ||
switch id { | ||
case packets.InitializationResponsePacket: | ||
var response initialization.Response | ||
if err = response.Unmarshal(b); err != nil { | ||
return | ||
} | ||
|
||
switch response.Data { | ||
case initialization.ResponseUsernameTaken: | ||
fmt.Println("Username is taken.") | ||
case initialization.ResponseUsernameMissing: | ||
fmt.Println("Username is missing.") | ||
case initialization.ResponseSuccess: | ||
return nil | ||
} | ||
|
||
return ErrResponse | ||
} | ||
|
||
return nil | ||
}) | ||
|
||
if err != nil { | ||
return knet.Close | ||
} | ||
|
||
return knet.None | ||
} | ||
|
||
client.OnSecureConnect = func(session knet.ClientSession) knet.AfterAction { | ||
fmt.Println("Secure connection established.") | ||
|
||
username := initialization.Username{ | ||
Data: "deneonet", | ||
} | ||
err := client.SendPacket(packets.InitializationUsernamePacket, &username) | ||
if err != nil { | ||
fmt.Println("Error sending username packet: ", err) | ||
} | ||
|
||
return knet.None | ||
} | ||
|
||
client.OnDisconnect = func() { | ||
fmt.Println("Disconnected from server.") | ||
} | ||
|
||
_, err := client.Connect("localhost:8080") | ||
if err != nil { | ||
fmt.Println("Error connecting to server:", err) | ||
return | ||
} | ||
} |
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,16 @@ | ||
package packets | ||
|
||
const ( | ||
InitializationUsernamePacket int = iota | ||
InitializationResponsePacket | ||
|
||
MessagePacket | ||
MessageResponsePacket | ||
) | ||
|
||
type Message struct { | ||
Username string | ||
Message string | ||
|
||
HasDisconnected bool // After someone disconnected, everyone can reclaim that username, so in order to extinguish the "new user" from the "old users", a "disconnected" mark is appended to the message | ||
} |
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,2 @@ | ||
bencgen --in .\message\packet.benc --out ./ --lang go | ||
bencgen --in .\initialization\packet.benc --out ./ --lang go |
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,7 @@ | ||
package initialization | ||
|
||
const ( | ||
ResponseSuccess byte = iota | ||
ResponseUsernameTaken | ||
ResponseUsernameMissing | ||
) |
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,12 @@ | ||
header initialization; | ||
|
||
ctr Username { | ||
string data = 1; | ||
} | ||
|
||
ctr Response { | ||
byte data = 1; | ||
} | ||
|
||
# DO NOT EDIT. | ||
# [meta_s] eyJtc2dzIjp7IlJlc3BvbnNlIjp7InJJZHMiOm51bGwsImZpZWxkcyI6eyIxIjp7IklkIjoxLCJOYW1lIjoiZGF0YSIsIlR5cGUiOnsiVG9rZW5UeXBlIjoxOSwiTWFwS2V5VHlwZSI6bnVsbCwiQ2hpbGRUeXBlIjpudWxsLCJDdHJOYW1lIjoiIiwiSXNVbnNhZmUiOmZhbHNlLCJJc0FycmF5IjpmYWxzZSwiSXNNYXAiOmZhbHNlfX19fSwiVXNlcm5hbWUiOnsicklkcyI6bnVsbCwiZmllbGRzIjp7IjEiOnsiSWQiOjEsIk5hbWUiOiJkYXRhIiwiVHlwZSI6eyJUb2tlblR5cGUiOjE1LCJNYXBLZXlUeXBlIjpudWxsLCJDaGlsZFR5cGUiOm51bGwsIkN0ck5hbWUiOiIiLCJJc1Vuc2FmZSI6ZmFsc2UsIklzQXJyYXkiOmZhbHNlLCJJc01hcCI6ZmFsc2V9fX19fX0= [meta_e] |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,8 @@ | ||
header message; | ||
|
||
ctr Packet { | ||
string data = 1; | ||
} | ||
|
||
# DO NOT EDIT. | ||
# [meta_s] eyJtc2dzIjp7IlBhY2tldCI6eyJySWRzIjpudWxsLCJmaWVsZHMiOnsiMSI6eyJJZCI6MSwiTmFtZSI6ImRhdGEiLCJUeXBlIjp7IlRva2VuVHlwZSI6MTUsIk1hcEtleVR5cGUiOm51bGwsIkNoaWxkVHlwZSI6bnVsbCwiQ3RyTmFtZSI6IiIsIklzVW5zYWZlIjpmYWxzZSwiSXNBcnJheSI6ZmFsc2UsIklzTWFwIjpmYWxzZX19fX19fQ== [meta_e] |
Oops, something went wrong.