From 45c145439a76da52f899007f24c656c5a2ebfccb Mon Sep 17 00:00:00 2001 From: Pontus Fransson Date: Mon, 25 Nov 2013 20:27:23 +0100 Subject: [PATCH] Fixade crash och flyttade initsockets, shutdownsockets. Moved initsocket and shutdownsockets to WinsockFunctions. --- .../NetworkDependencies/Connection.cpp | 14 +++++---- .../Messages/MessageHeader.cpp | 8 ++--- .../NetworkDependencies.vcxproj | 2 ++ .../NetworkDependencies.vcxproj.filters | 2 ++ Code/Network/NetworkDependencies/Translator.h | 2 +- .../NetworkDependencies/WinsockFunctions.cpp | 13 ++++++++ .../NetworkDependencies/WinsockFunctions.h | 11 +++++++ .../OysterNetworkClient/ClientMain.cpp | 27 +++++------------ .../OysterNetworkServer/ServerMain.cpp | 30 +++++-------------- 9 files changed, 57 insertions(+), 52 deletions(-) create mode 100644 Code/Network/NetworkDependencies/WinsockFunctions.cpp create mode 100644 Code/Network/NetworkDependencies/WinsockFunctions.h diff --git a/Code/Network/NetworkDependencies/Connection.cpp b/Code/Network/NetworkDependencies/Connection.cpp index 4cb9c8c1..57287669 100644 --- a/Code/Network/NetworkDependencies/Connection.cpp +++ b/Code/Network/NetworkDependencies/Connection.cpp @@ -85,11 +85,8 @@ bool Connection::Send(const unsigned char message[]) { int nBytes; unsigned long messageSize = strlen((char*)message); - int optlen = sizeof(int); - int optval = 0; - getsockopt(this->socket, SOL_SOCKET, SO_MAX_MSG_SIZE, (char *)&optval, &optlen); - messageSize = 255; + messageSize = 1600; nBytes = send(this->socket, (char*)message , messageSize, 0); if(nBytes == SOCKET_ERROR) { @@ -97,6 +94,8 @@ bool Connection::Send(const unsigned char message[]) return false; } + std::cout << "Size of the sent data: " << nBytes << " bytes" << std::endl; + return true; } @@ -104,7 +103,7 @@ int Connection::Recieve(unsigned char message[]) { int nBytes; - nBytes = recv(this->socket, (char*)message , 255, 0); + nBytes = recv(this->socket, (char*)message, 1600, 0); if(nBytes == SOCKET_ERROR) { //Recv failed @@ -118,6 +117,11 @@ int Connection::Recieve(unsigned char message[]) int Connection::Listen() { + int optlen = sizeof(int); + int optval; + int Return = getsockopt(this->socket, SOL_SOCKET, SO_MAX_MSG_SIZE, (char *)&optval, &optlen); + std::cout << optval << std::endl; + int clientSocket; if((clientSocket = accept(this->socket, NULL, NULL)) == INVALID_SOCKET) { diff --git a/Code/Network/NetworkDependencies/Messages/MessageHeader.cpp b/Code/Network/NetworkDependencies/Messages/MessageHeader.cpp index 8418da77..66d9bbfc 100644 --- a/Code/Network/NetworkDependencies/Messages/MessageHeader.cpp +++ b/Code/Network/NetworkDependencies/Messages/MessageHeader.cpp @@ -99,12 +99,12 @@ void MessageHeader::PackFloat(float i, unsigned char msg[]) void MessageHeader::PackFloat(float i[], unsigned int elementCount, unsigned char msg[]) { //Pack number of elements - PackUnsignedInt(elementCount, &msg[size]); + PackUnsignedInt(elementCount, msg); //Pack all elements for(int j = 0; j < elementCount; j++) { - PackFloat(i[j], &msg[size]); + PackFloat(i[j], msg); } } @@ -204,12 +204,12 @@ float* MessageHeader::UnpackFloat(unsigned int& elementCount, unsigned char msg[ { float* i; - elementCount = UnpackUnsignedInt(&msg[size]); + elementCount = UnpackUnsignedInt(msg); i = new float[elementCount]; for(int j = 0; j < elementCount; j++) { - i[j] = UnpackFloat(&msg[size]); + i[j] = UnpackFloat(msg); } return i; diff --git a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj index d302efe7..73eb03d0 100644 --- a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj +++ b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj @@ -166,6 +166,7 @@ + @@ -179,6 +180,7 @@ + diff --git a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters index f3cfc718..babc0a34 100644 --- a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters +++ b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters @@ -8,6 +8,7 @@ + @@ -21,5 +22,6 @@ + \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/Translator.h b/Code/Network/NetworkDependencies/Translator.h index 514d325c..581bb406 100644 --- a/Code/Network/NetworkDependencies/Translator.h +++ b/Code/Network/NetworkDependencies/Translator.h @@ -16,7 +16,7 @@ namespace Oyster class Translator : public ITranslate { public: - Translator () { msg = new unsigned char[256]; }; + Translator () { msg = new unsigned char[1601]; }; ~Translator() { if(msg != NULL) { delete [] this->msg; }}; unsigned char* Pack (Protocols::ProtocolHeader &header ); diff --git a/Code/Network/NetworkDependencies/WinsockFunctions.cpp b/Code/Network/NetworkDependencies/WinsockFunctions.cpp new file mode 100644 index 00000000..92382ee9 --- /dev/null +++ b/Code/Network/NetworkDependencies/WinsockFunctions.cpp @@ -0,0 +1,13 @@ +#include "WinsockFunctions.h" +#include + +bool InitSockets() +{ + WSADATA wsaData; + return WSAStartup(MAKEWORD(2, 2), &wsaData) == NO_ERROR; +} + +void ShutdownSockets() +{ + WSACleanup(); +} \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/WinsockFunctions.h b/Code/Network/NetworkDependencies/WinsockFunctions.h new file mode 100644 index 00000000..a2a3eee0 --- /dev/null +++ b/Code/Network/NetworkDependencies/WinsockFunctions.h @@ -0,0 +1,11 @@ +#ifndef NETWORK_DEPENDENCIES_WINSOCK_FUNCTIONS_H +#define NETWORK_DEPENDENCIES_WINSOCK_FUNCTIONS_H + +///////////////////////////////////// +// Created by Pontus Fransson 2013 // +///////////////////////////////////// + +void ShutdownSockets(); +bool InitSockets(); + +#endif \ No newline at end of file diff --git a/Code/Network/OysterNetworkClient/ClientMain.cpp b/Code/Network/OysterNetworkClient/ClientMain.cpp index 1976d1fe..719ca872 100644 --- a/Code/Network/OysterNetworkClient/ClientMain.cpp +++ b/Code/Network/OysterNetworkClient/ClientMain.cpp @@ -1,18 +1,17 @@ #include -#include "Client.h" #include #include +#include "../NetworkDependencies/WinsockFunctions.h" #include "..\NetworkDependencies\Translator.h" #include "..\NetworkDependencies\Protocols.h" +#include "Client.h" + +#pragma comment(lib, "ws2_32.lib") using namespace std; using namespace Oyster::Network::Protocols;; using namespace Oyster::Network::Client; -#pragma comment(lib, "ws2_32.lib") - -void ShutdownSockets(); -bool InitSockets(); void chat(Client &client); int main() @@ -37,22 +36,11 @@ int main() return 0; } -bool InitSockets() -{ - WSADATA wsaData; - return WSAStartup(MAKEWORD(2, 2), &wsaData) == NO_ERROR; -} - -void ShutdownSockets() -{ - WSACleanup(); -} - void chat(Client &client) { Oyster::Network::Translator *t = new Oyster::Network::Translator(); - unsigned char msgRecv[2560] = "\0"; + unsigned char msgRecv[1601] = "\0"; string msgSend = ""; ProtocolSet* set = new ProtocolSet; @@ -82,8 +70,9 @@ void chat(Client &client) cout <<"Client 2: " << set->Protocol.pTest->textMessage <Protocol.pTest->numOfFloats; i++) { - cout << set->Protocol.pTest->f[i] << ' ' ; + //cout << set->Protocol.pTest->f[i] << ' ' ; } + cout << endl; break; } @@ -100,8 +89,6 @@ void chat(Client &client) msgSend = "ERROR!"; } - test.packageType = package_type_test; - test.size = msgSend.length(); test.textMessage = msgSend; unsigned char *message = t->Pack(test); diff --git a/Code/Network/OysterNetworkServer/ServerMain.cpp b/Code/Network/OysterNetworkServer/ServerMain.cpp index 2dfbc875..80a59eca 100644 --- a/Code/Network/OysterNetworkServer/ServerMain.cpp +++ b/Code/Network/OysterNetworkServer/ServerMain.cpp @@ -1,24 +1,21 @@ #include #include #include +#include "../NetworkDependencies/WinsockFunctions.h" #include "../NetworkDependencies/Listener.h" +#include "../NetworkDependencies/Translator.h" #include "Client.h" -#include "../NetworkDependencies/Packing.h" -using namespace std; -using namespace Oyster::Network::Server; #pragma comment(lib, "ws2_32.lib") -void ShutdownSockets(); -bool InitSockets(); - -#include "../NetworkDependencies/Translator.h" +using namespace std; +using namespace Oyster::Network::Server; using namespace Oyster::Network; using namespace ::Protocols; int main() { - unsigned char* recvBuffer = new unsigned char[5000]; + unsigned char* recvBuffer = new unsigned char[1601]; cout << "Server" << endl; Translator t; @@ -46,9 +43,9 @@ int main() ProtocolTest test; test.clientID = 0; test.textMessage = "hej"; - test.numOfFloats = 500; + test.numOfFloats = 350; test.f = new float[test.numOfFloats]; - float temp = 500.456f; + float temp = 395.456f; for(int i = 0; i < test.numOfFloats; i++) { test.f[i] = temp; @@ -57,7 +54,7 @@ int main() recvBuffer = t.Pack(test); client1.Send(recvBuffer); - + while(1) { client1.Recv(recvBuffer); @@ -94,14 +91,3 @@ int main() system("pause"); return 0; } - -bool InitSockets() -{ - WSADATA wsaData; - return WSAStartup(MAKEWORD(2, 2), &wsaData) == NO_ERROR; -} - -void ShutdownSockets() -{ - WSACleanup(); -} \ No newline at end of file