forked from scott/threaded_network_chat
Compare commits
12 Commits
a9ed43b230
...
v2
| Author | SHA1 | Date | |
|---|---|---|---|
| ed437d9b05 | |||
| 2c61dbcc64 | |||
| 442bc8d88f | |||
| 081a278753 | |||
| cae85a7466 | |||
| 0950342de0 | |||
| 3f899a974f | |||
| 039e9abef5 | |||
| 6bc287d6cc | |||
| 37d8b7fd0f | |||
| e68d98a669 | |||
| ce5d66f712 |
@@ -3,7 +3,7 @@
|
|||||||
"UseTab": "Always",
|
"UseTab": "Always",
|
||||||
"IndentWidth": 8,
|
"IndentWidth": 8,
|
||||||
"TabWidth": 8,
|
"TabWidth": 8,
|
||||||
"ColumnLimit": 120,
|
"ColumnLimit": 100,
|
||||||
"LineEnding": "LF",
|
"LineEnding": "LF",
|
||||||
"RemoveBracesLLVM": true,
|
"RemoveBracesLLVM": true,
|
||||||
"AlwaysBreakAfterReturnType": "AllDefinitions",
|
"AlwaysBreakAfterReturnType": "AllDefinitions",
|
||||||
|
|||||||
12
README.md
12
README.md
@@ -1,7 +1,13 @@
|
|||||||
# threaded network chat refactor
|
# threaded network chat refactor
|
||||||
|
|
||||||
The code has been refactored and it compiles. Hopefully it won't explode, but that if it does, that just means we have
|
The code has been refactored and it compiles. Hopefully it won't explode, but we were going to be
|
||||||
actual meaningful work to do.
|
doing a bunch of bug fixes anyway.
|
||||||
|
|
||||||
|
## What now?
|
||||||
|
|
||||||
|
See TODO. From here we're going to push the code to the gitlab like we were told to after everyone
|
||||||
|
is set up. Follow the coding style guidelines because otherwise everything will be extremely gross
|
||||||
|
and inconsistent. If you set up your editor correctly, it will take care of most of that stuff.
|
||||||
|
|
||||||
## TODO
|
## TODO
|
||||||
|
|
||||||
@@ -16,7 +22,7 @@ actual meaningful work to do.
|
|||||||
## Code
|
## Code
|
||||||
|
|
||||||
Everything is in [src/](src).
|
Everything is in [src/](src).
|
||||||
A mostly untouched copy of [scott's original code](https://git.therats.win/scott/threaded_network_chat) is also in [old/](old).
|
A mostly untouched copy of [Scott's original code](https://git.therats.win/scott/threaded_network_chat) is also in [old/](old).
|
||||||
A reformatted version of that code is also in [modified-example.cc](modified-example.cc) but it can be ignored.
|
A reformatted version of that code is also in [modified-example.cc](modified-example.cc) but it can be ignored.
|
||||||
|
|
||||||
## Build
|
## Build
|
||||||
|
|||||||
7
STYLE.md
7
STYLE.md
@@ -37,7 +37,7 @@ I recommend skimming over these for inspiration.
|
|||||||
- return type goes on its own line,
|
- return type goes on its own line,
|
||||||
- function name() is on a line below that, and
|
- function name() is on a line below that, and
|
||||||
- open bracket `{` after that.
|
- open bracket `{` after that.
|
||||||
- `for`, `if`, `while`, etc. statements that only contain one line
|
- no brackets on `for`, `if`, `while`, etc. statements that only contain one line
|
||||||
- Use `/* comments */` for permanent comments, `// comments` for temporary ones e.g. `TODO`'s or notices
|
- Use `/* comments */` for permanent comments, `// comments` for temporary ones e.g. `TODO`'s or notices
|
||||||
- **Try to make every line 100 characters wide or less.**
|
- **Try to make every line 100 characters wide or less.**
|
||||||
- Start source files as `.cc` straight away, even if they're pure **C**.
|
- Start source files as `.cc` straight away, even if they're pure **C**.
|
||||||
@@ -60,7 +60,7 @@ The length of a function name, variable name, et cetera should be directly propo
|
|||||||
|
|
||||||
#### Don't
|
#### Don't
|
||||||
|
|
||||||
- abbreviate global variables,
|
- abbreviate global variables **EVER**,
|
||||||
- mention the data type in a variable name,
|
- mention the data type in a variable name,
|
||||||
- use a full word where an abbreviation will do
|
- use a full word where an abbreviation will do
|
||||||
- (especially not in a variable that will die 5 lines later),
|
- (especially not in a variable that will die 5 lines later),
|
||||||
@@ -70,7 +70,7 @@ The length of a function name, variable name, et cetera should be directly propo
|
|||||||
#### Do
|
#### Do
|
||||||
|
|
||||||
- abbreviate extremely short-lived variables to one letter,
|
- abbreviate extremely short-lived variables to one letter,
|
||||||
- e.g. `for (int index=0, index<10, i++)` -> `for (int i=0, i<10, i++)`
|
- e.g. `for (int index = 0; index < 10; i++)` -> `for (int i = 0; i < 10; i++)`
|
||||||
- break any rule if following it ruins the readability
|
- break any rule if following it ruins the readability
|
||||||
|
|
||||||
## Source File layout
|
## Source File layout
|
||||||
@@ -120,4 +120,3 @@ div_numb(int n, int d)
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -86,7 +86,8 @@ displayFile(string path, int startLineNum = 0, int numLines = 10)
|
|||||||
|
|
||||||
// print each line directly to the screen
|
// print each line directly to the screen
|
||||||
int num = 0;
|
int num = 0;
|
||||||
while (getline(file, line) && num <= numLines + startLineNum + 1) // while there is file content and the
|
while (getline(file, line) &&
|
||||||
|
num <= numLines + startLineNum + 1) // while there is file content and the
|
||||||
// line number isn't too high
|
// line number isn't too high
|
||||||
{
|
{
|
||||||
if (num >= startLineNum) {
|
if (num >= startLineNum) {
|
||||||
@@ -132,7 +133,8 @@ void *
|
|||||||
waitForClient(void *argss)
|
waitForClient(void *argss)
|
||||||
{
|
{
|
||||||
waitClientArgs *args = static_cast<waitClientArgs *>(argss);
|
waitClientArgs *args = static_cast<waitClientArgs *>(argss);
|
||||||
clientSocketDescriptor = accept(serverSocketDescriptor, (sockaddr *)&args->newSockAddr, &args->newSockAddrSize);
|
clientSocketDescriptor =
|
||||||
|
accept(serverSocketDescriptor, (sockaddr *)&args->newSockAddr, &args->newSockAddrSize);
|
||||||
if (clientSocketDescriptor >= 0) {
|
if (clientSocketDescriptor >= 0) {
|
||||||
writeToFile(logFileName, "client connected");
|
writeToFile(logFileName, "client connected");
|
||||||
if (linesInFile(logFileName) > LOG_LENGTH)
|
if (linesInFile(logFileName) > LOG_LENGTH)
|
||||||
@@ -184,7 +186,8 @@ setupServer(int port)
|
|||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
// bind the socket to its local address
|
// bind the socket to its local address
|
||||||
int bindStatus = bind(serverSocketDescriptor, (struct sockaddr *)&servAddr, sizeof(servAddr));
|
int bindStatus =
|
||||||
|
bind(serverSocketDescriptor, (struct sockaddr *)&servAddr, sizeof(servAddr));
|
||||||
if (bindStatus < 0) {
|
if (bindStatus < 0) {
|
||||||
// keeps from bricking the terminal if this happens
|
// keeps from bricking the terminal if this happens
|
||||||
endwin();
|
endwin();
|
||||||
@@ -288,7 +291,8 @@ setupClient()
|
|||||||
sendSockAddr.sin_port = htons(PORT_NUM);
|
sendSockAddr.sin_port = htons(PORT_NUM);
|
||||||
clientSocketDescriptor = socket(AF_INET, SOCK_STREAM, 0);
|
clientSocketDescriptor = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
// try to connect...
|
// try to connect...
|
||||||
int status = connect(clientSocketDescriptor, (sockaddr *)&sendSockAddr, sizeof(sendSockAddr));
|
int status =
|
||||||
|
connect(clientSocketDescriptor, (sockaddr *)&sendSockAddr, sizeof(sendSockAddr));
|
||||||
if (status < 0)
|
if (status < 0)
|
||||||
writeToFile(logFileName, "Error connecting to socket!");
|
writeToFile(logFileName, "Error connecting to socket!");
|
||||||
writeToFile(logFileName, "Connected to the server!");
|
writeToFile(logFileName, "Connected to the server!");
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
"UseTab": "Always",
|
"UseTab": "Always",
|
||||||
"IndentWidth": 8,
|
"IndentWidth": 8,
|
||||||
"TabWidth": 8,
|
"TabWidth": 8,
|
||||||
"ColumnLimit": 120,
|
"ColumnLimit": 100,
|
||||||
"LineEnding": "LF",
|
"LineEnding": "LF",
|
||||||
"RemoveBracesLLVM": true,
|
"RemoveBracesLLVM": true,
|
||||||
"AlwaysBreakAfterReturnType": "AllDefinitions",
|
"AlwaysBreakAfterReturnType": "AllDefinitions",
|
||||||
|
|||||||
@@ -48,4 +48,3 @@ file_disp(std::string p, int lnstart, int lncount)
|
|||||||
refresh();
|
refresh();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
|
|
||||||
#include "chat.h"
|
#include "chat.h"
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
count_lines(std::string p)
|
count_lines(std::string p)
|
||||||
{
|
{
|
||||||
@@ -40,4 +39,3 @@ log_append(std::string p, std::string ln)
|
|||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
14
src/main.cc
14
src/main.cc
@@ -1,4 +1,3 @@
|
|||||||
#include <fstream>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
@@ -36,6 +35,7 @@ std::string log_path;
|
|||||||
int socket_descriptor_server;
|
int socket_descriptor_server;
|
||||||
int socket_descriptor_client;
|
int socket_descriptor_client;
|
||||||
|
|
||||||
|
/* We use this for both. Probably won't be like this forever. */
|
||||||
pthread_t client_wait_thread;
|
pthread_t client_wait_thread;
|
||||||
|
|
||||||
int
|
int
|
||||||
@@ -58,11 +58,13 @@ main(int argc, char *argv[])
|
|||||||
switch (mode) {
|
switch (mode) {
|
||||||
case CLIENT_MODE:
|
case CLIENT_MODE:
|
||||||
log_append(log_path, "CLIENT MODE");
|
log_append(log_path, "CLIENT MODE");
|
||||||
client_start(listen_ip_address, listen_port, &socket_descriptor_client, log_path, &client_wait_thread);
|
client_start(listen_ip_address, listen_port, &socket_descriptor_client, log_path,
|
||||||
|
&client_wait_thread);
|
||||||
break;
|
break;
|
||||||
case SERVER_MODE:
|
case SERVER_MODE:
|
||||||
log_append(log_path, "SERVER MODE");
|
log_append(log_path, "SERVER MODE");
|
||||||
err = server_start(listen_port, &socket_descriptor_server, log_path, &client_wait_thread, &time_start);
|
err = server_start(listen_port, &socket_descriptor_server, log_path,
|
||||||
|
&client_wait_thread, &time_start);
|
||||||
if (err)
|
if (err)
|
||||||
goto panic_no_server_sock;
|
goto panic_no_server_sock;
|
||||||
break;
|
break;
|
||||||
@@ -93,10 +95,12 @@ main(int argc, char *argv[])
|
|||||||
|
|
||||||
switch (mode) {
|
switch (mode) {
|
||||||
case CLIENT_MODE:
|
case CLIENT_MODE:
|
||||||
client_stop(&time_start, &time_end, &socket_descriptor_client, &bytes_read, &bytes_written);
|
client_stop(&time_start, &time_end, &socket_descriptor_client, &bytes_read,
|
||||||
|
&bytes_written);
|
||||||
break;
|
break;
|
||||||
case SERVER_MODE:
|
case SERVER_MODE:
|
||||||
server_stop(&time_start, &time_end, &socket_descriptor_server, &bytes_read, &bytes_written);
|
server_stop(&time_start, &time_end, &socket_descriptor_server, &bytes_read,
|
||||||
|
&bytes_written);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
goto panic_no_mode;
|
goto panic_no_mode;
|
||||||
|
|||||||
@@ -32,7 +32,9 @@ typedef struct __wserver_args_t {
|
|||||||
std::string log;
|
std::string log;
|
||||||
} wserver_args_t;
|
} wserver_args_t;
|
||||||
|
|
||||||
/* client_start(listen_ip, listen_port, &socket_descriptor_client, log_path, &client_wait_thread); */
|
/*
|
||||||
|
* client_start(listen_ip, listen_port, &socket_descriptor_client, log_path, &client_wait_thread);
|
||||||
|
*/
|
||||||
void
|
void
|
||||||
client_start(std::string ip, int port, int *sock, std::string log, pthread_t *thr)
|
client_start(std::string ip, int port, int *sock, std::string log, pthread_t *thr)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user