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",
|
||||
"IndentWidth": 8,
|
||||
"TabWidth": 8,
|
||||
"ColumnLimit": 120,
|
||||
"ColumnLimit": 100,
|
||||
"LineEnding": "LF",
|
||||
"RemoveBracesLLVM": true,
|
||||
"AlwaysBreakAfterReturnType": "AllDefinitions",
|
||||
|
||||
12
README.md
12
README.md
@@ -1,7 +1,13 @@
|
||||
# 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
|
||||
actual meaningful work to do.
|
||||
The code has been refactored and it compiles. Hopefully it won't explode, but we were going to be
|
||||
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
|
||||
|
||||
@@ -16,7 +22,7 @@ actual meaningful work to do.
|
||||
## Code
|
||||
|
||||
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.
|
||||
|
||||
## 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,
|
||||
- function name() is on a line below that, and
|
||||
- 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
|
||||
- **Try to make every line 100 characters wide or less.**
|
||||
- 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
|
||||
|
||||
- abbreviate global variables,
|
||||
- abbreviate global variables **EVER**,
|
||||
- mention the data type in a variable name,
|
||||
- use a full word where an abbreviation will do
|
||||
- (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
|
||||
|
||||
- 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
|
||||
|
||||
## Source File layout
|
||||
@@ -120,4 +120,3 @@ div_numb(int n, int d)
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
@@ -86,8 +86,9 @@ displayFile(string path, int startLineNum = 0, int numLines = 10)
|
||||
|
||||
// print each line directly to the screen
|
||||
int num = 0;
|
||||
while (getline(file, line) && num <= numLines + startLineNum + 1) // while there is file content and the
|
||||
// line number isn't too high
|
||||
while (getline(file, line) &&
|
||||
num <= numLines + startLineNum + 1) // while there is file content and the
|
||||
// line number isn't too high
|
||||
{
|
||||
if (num >= startLineNum) {
|
||||
move(lineNum, 0);
|
||||
@@ -132,7 +133,8 @@ void *
|
||||
waitForClient(void *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) {
|
||||
writeToFile(logFileName, "client connected");
|
||||
if (linesInFile(logFileName) > LOG_LENGTH)
|
||||
@@ -184,7 +186,8 @@ setupServer(int port)
|
||||
exit(0);
|
||||
}
|
||||
// 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) {
|
||||
// keeps from bricking the terminal if this happens
|
||||
endwin();
|
||||
@@ -288,7 +291,8 @@ setupClient()
|
||||
sendSockAddr.sin_port = htons(PORT_NUM);
|
||||
clientSocketDescriptor = socket(AF_INET, SOCK_STREAM, 0);
|
||||
// try to connect...
|
||||
int status = connect(clientSocketDescriptor, (sockaddr *)&sendSockAddr, sizeof(sendSockAddr));
|
||||
int status =
|
||||
connect(clientSocketDescriptor, (sockaddr *)&sendSockAddr, sizeof(sendSockAddr));
|
||||
if (status < 0)
|
||||
writeToFile(logFileName, "Error connecting to socket!");
|
||||
writeToFile(logFileName, "Connected to the server!");
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
"UseTab": "Always",
|
||||
"IndentWidth": 8,
|
||||
"TabWidth": 8,
|
||||
"ColumnLimit": 120,
|
||||
"ColumnLimit": 100,
|
||||
"LineEnding": "LF",
|
||||
"RemoveBracesLLVM": true,
|
||||
"AlwaysBreakAfterReturnType": "AllDefinitions",
|
||||
|
||||
@@ -48,4 +48,3 @@ file_disp(std::string p, int lnstart, int lncount)
|
||||
refresh();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
|
||||
#include "chat.h"
|
||||
|
||||
|
||||
int
|
||||
count_lines(std::string p)
|
||||
{
|
||||
@@ -40,4 +39,3 @@ log_append(std::string p, std::string ln)
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
18
src/main.cc
18
src/main.cc
@@ -1,4 +1,3 @@
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
|
||||
#include <arpa/inet.h>
|
||||
@@ -36,6 +35,7 @@ std::string log_path;
|
||||
int socket_descriptor_server;
|
||||
int socket_descriptor_client;
|
||||
|
||||
/* We use this for both. Probably won't be like this forever. */
|
||||
pthread_t client_wait_thread;
|
||||
|
||||
int
|
||||
@@ -58,11 +58,13 @@ main(int argc, char *argv[])
|
||||
switch (mode) {
|
||||
case 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;
|
||||
case 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)
|
||||
goto panic_no_server_sock;
|
||||
break;
|
||||
@@ -93,10 +95,12 @@ main(int argc, char *argv[])
|
||||
|
||||
switch (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;
|
||||
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;
|
||||
default:
|
||||
goto panic_no_mode;
|
||||
@@ -117,7 +121,7 @@ panic_no_mode:
|
||||
|
||||
panic_no_server_sock:
|
||||
big_panic_msg();
|
||||
|
||||
|
||||
fprintf(stderr, "Fatal: Could not establish server socket!\n");
|
||||
return 1;
|
||||
}
|
||||
@@ -132,7 +136,7 @@ big_panic_msg(void)
|
||||
{
|
||||
if (!isendwin())
|
||||
endwin();
|
||||
|
||||
|
||||
fprintf(stderr, "-----FATAL ERROR, THIS SHOULD NEVER HAPPEN-----\n");
|
||||
usleep(400 * 1000);
|
||||
fprintf(stderr, "You may ask yourself... how do I work this?\n");
|
||||
|
||||
@@ -32,7 +32,9 @@ typedef struct __wserver_args_t {
|
||||
std::string log;
|
||||
} 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
|
||||
client_start(std::string ip, int port, int *sock, std::string log, pthread_t *thr)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user