Fixade crash och flyttade initsockets, shutdownsockets.

Moved initsocket and shutdownsockets to WinsockFunctions.
This commit is contained in:
Pontus Fransson 2013-11-25 20:27:23 +01:00
parent 6b57815bb8
commit 45c145439a
9 changed files with 57 additions and 52 deletions

View File

@ -85,11 +85,8 @@ bool Connection::Send(const unsigned char message[])
{ {
int nBytes; int nBytes;
unsigned long messageSize = strlen((char*)message); 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); nBytes = send(this->socket, (char*)message , messageSize, 0);
if(nBytes == SOCKET_ERROR) if(nBytes == SOCKET_ERROR)
{ {
@ -97,6 +94,8 @@ bool Connection::Send(const unsigned char message[])
return false; return false;
} }
std::cout << "Size of the sent data: " << nBytes << " bytes" << std::endl;
return true; return true;
} }
@ -104,7 +103,7 @@ int Connection::Recieve(unsigned char message[])
{ {
int nBytes; int nBytes;
nBytes = recv(this->socket, (char*)message , 255, 0); nBytes = recv(this->socket, (char*)message, 1600, 0);
if(nBytes == SOCKET_ERROR) if(nBytes == SOCKET_ERROR)
{ {
//Recv failed //Recv failed
@ -118,6 +117,11 @@ int Connection::Recieve(unsigned char message[])
int Connection::Listen() 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; int clientSocket;
if((clientSocket = accept(this->socket, NULL, NULL)) == INVALID_SOCKET) if((clientSocket = accept(this->socket, NULL, NULL)) == INVALID_SOCKET)
{ {

View File

@ -99,12 +99,12 @@ void MessageHeader::PackFloat(float i, unsigned char msg[])
void MessageHeader::PackFloat(float i[], unsigned int elementCount, unsigned char msg[]) void MessageHeader::PackFloat(float i[], unsigned int elementCount, unsigned char msg[])
{ {
//Pack number of elements //Pack number of elements
PackUnsignedInt(elementCount, &msg[size]); PackUnsignedInt(elementCount, msg);
//Pack all elements //Pack all elements
for(int j = 0; j < elementCount; j++) 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; float* i;
elementCount = UnpackUnsignedInt(&msg[size]); elementCount = UnpackUnsignedInt(msg);
i = new float[elementCount]; i = new float[elementCount];
for(int j = 0; j < elementCount; j++) for(int j = 0; j < elementCount; j++)
{ {
i[j] = UnpackFloat(&msg[size]); i[j] = UnpackFloat(msg);
} }
return i; return i;

View File

@ -166,6 +166,7 @@
<ClCompile Include="Messages\MessageTest.cpp" /> <ClCompile Include="Messages\MessageTest.cpp" />
<ClCompile Include="Packing.cpp" /> <ClCompile Include="Packing.cpp" />
<ClCompile Include="Translator.cpp" /> <ClCompile Include="Translator.cpp" />
<ClCompile Include="WinsockFunctions.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="Connection.h" /> <ClInclude Include="Connection.h" />
@ -179,6 +180,7 @@
<ClInclude Include="ITranslate.h" /> <ClInclude Include="ITranslate.h" />
<ClInclude Include="Protocols.h" /> <ClInclude Include="Protocols.h" />
<ClInclude Include="Translator.h" /> <ClInclude Include="Translator.h" />
<ClInclude Include="WinsockFunctions.h" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">

View File

@ -8,6 +8,7 @@
<ClCompile Include="Packing.cpp" /> <ClCompile Include="Packing.cpp" />
<ClCompile Include="Translator.cpp" /> <ClCompile Include="Translator.cpp" />
<ClCompile Include="Listener.cpp" /> <ClCompile Include="Listener.cpp" />
<ClCompile Include="WinsockFunctions.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="Connection.h" /> <ClInclude Include="Connection.h" />
@ -21,5 +22,6 @@
<ClInclude Include="Messages\MessagesInclude.h" /> <ClInclude Include="Messages\MessagesInclude.h" />
<ClInclude Include="Listener.h" /> <ClInclude Include="Listener.h" />
<ClInclude Include="IListener.h" /> <ClInclude Include="IListener.h" />
<ClInclude Include="WinsockFunctions.h" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -16,7 +16,7 @@ namespace Oyster
class Translator : public ITranslate class Translator : public ITranslate
{ {
public: public:
Translator () { msg = new unsigned char[256]; }; Translator () { msg = new unsigned char[1601]; };
~Translator() { if(msg != NULL) { delete [] this->msg; }}; ~Translator() { if(msg != NULL) { delete [] this->msg; }};
unsigned char* Pack (Protocols::ProtocolHeader &header ); unsigned char* Pack (Protocols::ProtocolHeader &header );

View File

@ -0,0 +1,13 @@
#include "WinsockFunctions.h"
#include <WinSock2.h>
bool InitSockets()
{
WSADATA wsaData;
return WSAStartup(MAKEWORD(2, 2), &wsaData) == NO_ERROR;
}
void ShutdownSockets()
{
WSACleanup();
}

View File

@ -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

View File

@ -1,18 +1,17 @@
#include <iostream> #include <iostream>
#include "Client.h"
#include <WinSock2.h> #include <WinSock2.h>
#include <vld.h> #include <vld.h>
#include "../NetworkDependencies/WinsockFunctions.h"
#include "..\NetworkDependencies\Translator.h" #include "..\NetworkDependencies\Translator.h"
#include "..\NetworkDependencies\Protocols.h" #include "..\NetworkDependencies\Protocols.h"
#include "Client.h"
#pragma comment(lib, "ws2_32.lib")
using namespace std; using namespace std;
using namespace Oyster::Network::Protocols;; using namespace Oyster::Network::Protocols;;
using namespace Oyster::Network::Client; using namespace Oyster::Network::Client;
#pragma comment(lib, "ws2_32.lib")
void ShutdownSockets();
bool InitSockets();
void chat(Client &client); void chat(Client &client);
int main() int main()
@ -37,22 +36,11 @@ int main()
return 0; return 0;
} }
bool InitSockets()
{
WSADATA wsaData;
return WSAStartup(MAKEWORD(2, 2), &wsaData) == NO_ERROR;
}
void ShutdownSockets()
{
WSACleanup();
}
void chat(Client &client) void chat(Client &client)
{ {
Oyster::Network::Translator *t = new Oyster::Network::Translator(); Oyster::Network::Translator *t = new Oyster::Network::Translator();
unsigned char msgRecv[2560] = "\0"; unsigned char msgRecv[1601] = "\0";
string msgSend = ""; string msgSend = "";
ProtocolSet* set = new ProtocolSet; ProtocolSet* set = new ProtocolSet;
@ -82,8 +70,9 @@ void chat(Client &client)
cout <<"Client 2: " << set->Protocol.pTest->textMessage <<endl; cout <<"Client 2: " << set->Protocol.pTest->textMessage <<endl;
for(int i = 0; i < set->Protocol.pTest->numOfFloats; i++) for(int i = 0; i < set->Protocol.pTest->numOfFloats; i++)
{ {
cout << set->Protocol.pTest->f[i] << ' ' ; //cout << set->Protocol.pTest->f[i] << ' ' ;
} }
cout << endl;
break; break;
} }
@ -100,8 +89,6 @@ void chat(Client &client)
msgSend = "ERROR!"; msgSend = "ERROR!";
} }
test.packageType = package_type_test;
test.size = msgSend.length();
test.textMessage = msgSend; test.textMessage = msgSend;
unsigned char *message = t->Pack(test); unsigned char *message = t->Pack(test);

View File

@ -1,24 +1,21 @@
#include <iostream> #include <iostream>
#include <WinSock2.h> #include <WinSock2.h>
#include <vld.h> #include <vld.h>
#include "../NetworkDependencies/WinsockFunctions.h"
#include "../NetworkDependencies/Listener.h" #include "../NetworkDependencies/Listener.h"
#include "../NetworkDependencies/Translator.h"
#include "Client.h" #include "Client.h"
#include "../NetworkDependencies/Packing.h"
using namespace std;
using namespace Oyster::Network::Server;
#pragma comment(lib, "ws2_32.lib") #pragma comment(lib, "ws2_32.lib")
void ShutdownSockets(); using namespace std;
bool InitSockets(); using namespace Oyster::Network::Server;
#include "../NetworkDependencies/Translator.h"
using namespace Oyster::Network; using namespace Oyster::Network;
using namespace ::Protocols; using namespace ::Protocols;
int main() int main()
{ {
unsigned char* recvBuffer = new unsigned char[5000]; unsigned char* recvBuffer = new unsigned char[1601];
cout << "Server" << endl; cout << "Server" << endl;
Translator t; Translator t;
@ -46,9 +43,9 @@ int main()
ProtocolTest test; ProtocolTest test;
test.clientID = 0; test.clientID = 0;
test.textMessage = "hej"; test.textMessage = "hej";
test.numOfFloats = 500; test.numOfFloats = 350;
test.f = new float[test.numOfFloats]; test.f = new float[test.numOfFloats];
float temp = 500.456f; float temp = 395.456f;
for(int i = 0; i < test.numOfFloats; i++) for(int i = 0; i < test.numOfFloats; i++)
{ {
test.f[i] = temp; test.f[i] = temp;
@ -94,14 +91,3 @@ int main()
system("pause"); system("pause");
return 0; return 0;
} }
bool InitSockets()
{
WSADATA wsaData;
return WSAStartup(MAKEWORD(2, 2), &wsaData) == NO_ERROR;
}
void ShutdownSockets()
{
WSACleanup();
}