forked from scott/threaded_network_chat
update ColumnLimit to match STYLE.md
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
"UseTab": "Always",
|
||||
"IndentWidth": 8,
|
||||
"TabWidth": 8,
|
||||
"ColumnLimit": 120,
|
||||
"LineEnding": "LF",
|
||||
"RemoveBracesLLVM": true,
|
||||
"AlwaysBreakAfterReturnType": "AllDefinitions",
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
"UseTab": "Always",
|
||||
"IndentWidth": 8,
|
||||
"TabWidth": 8,
|
||||
"ColumnLimit": 120,
|
||||
"LineEnding": "LF",
|
||||
"RemoveBracesLLVM": true,
|
||||
"AlwaysBreakAfterReturnType": "AllDefinitions",
|
||||
|
||||
@@ -4,6 +4,7 @@ BasedOnStyle: LLVM # defaults
|
||||
UseTab: Always
|
||||
IndentWidth: 8
|
||||
TabWidth: 8
|
||||
ColumnLimit: 120
|
||||
LineEnding: LF # unix
|
||||
|
||||
RemoveBracesLLVM: true # single-statement blocks
|
||||
|
||||
@@ -30,7 +30,7 @@ const int DEFAULT_CUR_X = 2; // the x position of the preferred default cursor
|
||||
const int DEFAULT_CUR_Y = 12; // the x position of the preferred default cursor
|
||||
// position for message entry
|
||||
|
||||
int mode = 0; // what mode is this program in. 0 = nothing. 1 = server. 2 = client
|
||||
int mode = 0; // what mode is this program in. 0 = nothing. 1 = server. 2 = client
|
||||
int serverSocketDescriptor; // a global variable for storing the server socket
|
||||
// descriptor.
|
||||
int clientSocketDescriptor;
|
||||
@@ -44,7 +44,7 @@ struct timeval start1, end1;
|
||||
int bytesRead, bytesWritten = 0;
|
||||
|
||||
const int LOG_LENGTH = 10; // number of text log file lines to display
|
||||
int linePos = 0; // counter for what current position to use in the file
|
||||
int linePos = 0; // counter for what current position to use in the file
|
||||
|
||||
string logFileName; // the name of the log file
|
||||
|
||||
@@ -86,10 +86,8 @@ 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);
|
||||
@@ -134,9 +132,7 @@ 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)
|
||||
@@ -155,8 +151,7 @@ pollForClient()
|
||||
while (1) {
|
||||
// receive a message from the client (listen)
|
||||
memset(&msg, 0, sizeof(msg)); // clear the buffer
|
||||
bytesRead +=
|
||||
recv(clientSocketDescriptor, (char *)&msg, sizeof(msg), 0);
|
||||
bytesRead += recv(clientSocketDescriptor, (char *)&msg, sizeof(msg), 0);
|
||||
writeToFile(logFileName,
|
||||
msg); // write the client's message to file
|
||||
if (linesInFile(logFileName) > LOG_LENGTH)
|
||||
@@ -189,8 +184,7 @@ 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();
|
||||
@@ -210,8 +204,7 @@ setupServer(int port)
|
||||
auto *aaa = new waitClientArgs{}; // it looks stupid but it works
|
||||
aaa->newSockAddr = newSockAddr;
|
||||
aaa->newSockAddrSize = newSockAddrSize;
|
||||
int rc =
|
||||
pthread_create(&client_wait_thread, nullptr, waitForClient, aaa);
|
||||
int rc = pthread_create(&client_wait_thread, nullptr, waitForClient, aaa);
|
||||
pthread_detach(client_wait_thread);
|
||||
writeToFile(logFileName, "Server started sucessfully");
|
||||
gettimeofday(&start1, NULL);
|
||||
@@ -258,8 +251,7 @@ pollForSever(void *args)
|
||||
char msg[1024];
|
||||
while (1) {
|
||||
memset(&msg, 0, sizeof(msg)); // clear the buffer
|
||||
bytesRead +=
|
||||
recv(socketDescriptor, (char *)&msg, sizeof(msg), 0);
|
||||
bytesRead += recv(socketDescriptor, (char *)&msg, sizeof(msg), 0);
|
||||
|
||||
// not needed for proofs of concept testing
|
||||
/*if(!strcmp(msg, "exit"))
|
||||
@@ -292,13 +284,11 @@ setupClient()
|
||||
sockaddr_in sendSockAddr;
|
||||
bzero((char *)&sendSockAddr, sizeof(sendSockAddr));
|
||||
sendSockAddr.sin_family = AF_INET;
|
||||
sendSockAddr.sin_addr.s_addr =
|
||||
inet_addr(inet_ntoa(*(struct in_addr *)*host->h_addr_list));
|
||||
sendSockAddr.sin_addr.s_addr = inet_addr(inet_ntoa(*(struct in_addr *)*host->h_addr_list));
|
||||
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!");
|
||||
@@ -307,8 +297,7 @@ setupClient()
|
||||
gettimeofday(&start1, NULL);
|
||||
auto *aaa = new waitClientArgs{}; // it looks stupid but it works
|
||||
aaa->auxInt = clientSocketDescriptor;
|
||||
int rc =
|
||||
pthread_create(&client_wait_thread, nullptr, pollForSever, aaa);
|
||||
int rc = pthread_create(&client_wait_thread, nullptr, pollForSever, aaa);
|
||||
pthread_detach(client_wait_thread);
|
||||
}
|
||||
|
||||
@@ -384,13 +373,10 @@ main(int argc, char *argv[])
|
||||
printw("> ");
|
||||
getstr(userInput);
|
||||
writeToFile(logFileName, userInput);
|
||||
if (mode == 1) {
|
||||
send(clientSocketDescriptor, (char *)userInput,
|
||||
strlen(userInput), 0);
|
||||
} else {
|
||||
send(clientSocketDescriptor, (char *)userInput,
|
||||
strlen(userInput), 0);
|
||||
}
|
||||
if (mode == 1)
|
||||
send(clientSocketDescriptor, (char *)userInput, strlen(userInput), 0);
|
||||
else
|
||||
send(clientSocketDescriptor, (char *)userInput, strlen(userInput), 0);
|
||||
}
|
||||
|
||||
endwin();
|
||||
|
||||
Reference in New Issue
Block a user