Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Networking.h"
- #include "DialogBox.h"//for the dialogue box which the user enters the IP address into (contains char ipAddress[16] global)
- #include "CORE LIB/Window.h"
- extern window_struct window;
- //sort out these globals later
- int port = 27015;
- int num_clients = 0;
- bool client = false;
- bool host = false;
- bool networking = false;
- Socket network;
- //these two things are the only things that need to be externally available
- bool transmit = false;
- client_data_struct client_data[MAX_CLIENTS+1];
- void WSAInit()
- {
- if(WSAStartup( MAKEWORD(2, 2), &network.wsaData ) != NO_ERROR){
- printf("Socket Initialization: Error with WSAStartup\n");
- PostQuitMessage(0);
- WSACleanup();
- return;
- }
- }
- //handle socket data
- Socket::Socket()
- {
- WSAInit();
- }
- Socket::~Socket()
- {
- WSACleanup();
- }
- bool Socket::init_socket()
- {
- mySocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- if (mySocket == INVALID_SOCKET){
- printf("Socket Initialization: Error creating socket");
- PostQuitMessage(0);
- WSACleanup();
- return 0;
- }
- //set socket option tcp_nodelay to stop packets being delayed and sent out as bigger packets (really messes up the reading)
- int flag = 1;
- int result = setsockopt(mySocket, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int));
- if (result < 0){return 0;}
- return 1;
- }
- bool Socket::StartHosting(int port)//server bind port
- {
- myAddress.sin_family = AF_INET;
- myAddress.sin_port = htons(port);
- myAddress.sin_addr.s_addr = htonl(INADDR_ANY);//inet_addr( "0.0.0.0" );
- if (bind(mySocket, (SOCKADDR*)&myAddress, sizeof(myAddress) ) == SOCKET_ERROR){
- printf("Unable to bind socket\n");
- WSACleanup();
- WSAInit();
- return 0;
- }
- if (listen(mySocket, MAX_CLIENTS) == SOCKET_ERROR){
- printf("Unable to listen to socket\n");
- WSACleanup();
- WSAInit();
- return 0;
- }
- return 1;
- }
- bool Socket::ConnectToServer(const char *ipAddress, int port)//client connect
- {
- myAddress.sin_family = AF_INET;
- myAddress.sin_port = htons(port);
- myAddress.sin_addr.s_addr = inet_addr(ipAddress);
- if (myAddress.sin_addr.s_addr == INADDR_NONE){printf("Invalid IP!\n");return 0;}
- if (connect(mySocket, (LPSOCKADDR)&myAddress, sizeof(myAddress) ) == SOCKET_ERROR){
- int Error = WSAGetLastError();
- if (Error==WSAEWOULDBLOCK){
- printf("Connection requested waiting for response...\n");
- return 0;
- }
- printf("Connection failed: %d\n",Error);
- closesocket(mySocket);
- WSACleanup();
- return 0;
- }
- //connected on first try
- printf("Connected (YES!)!\n");
- return 1;
- }
- //
- void connect(int set_port)
- {
- if (host || client){
- if (MessageBox( NULL, "You must disconnect first.\nDo you want to disconnect now and connect to a new host?","Disconnect and Continue?", MB_YESNO | MB_ICONINFORMATION)==IDYES){ disconnect(); }
- else{return;}
- }
- //open dialogue box, user inputs ipaddress
- if ( DialogBox(window.hInstance, MAKEINTRESOURCE(IDD_DIALOG1), window.hWnd, (DLGPROC)IP_DlgProc) != IDOK ){return;}
- //check if IP address (ipAddress) is valid
- //init and attempt to connect
- if (!network.init_socket()){return;}
- if (set_port != port){port=set_port;}
- printf("Attempting to connect to IP %s, on port %d\n", ipAddress, port);
- WSAAsyncSelect(network.mySocket, window.hWnd, WM_SOCKET, (FD_READ | FD_CONNECT | FD_CLOSE));
- if (!network.ConnectToServer(ipAddress, port)){return;}
- client = true;
- networking = true;
- }
- void connected(int Error)
- {
- if (Error != 0){
- //possible errors: WSAEAFNOSUPPORT WSAECONNREFUSED WSAENETUNREACH WSAEFAULT WSAEINVAL WSAEISCONN WSAEMFILE WSAENOBUFS WSAENOTCONN WSAETIMEDOUT
- printf("Connection errror.");
- PostQuitMessage(0);
- WSACleanup();
- return;
- }
- client = true;
- networking = true;
- printf("Connected!\n");
- }
- void disconnect()
- {
- if (host){
- for (int id=0; id<num_clients; id++){
- char buffer[1] = {1};
- send(client_data[id].socket, buffer, 18, 0);
- shutdown(client_data[id].socket, SD_BOTH);
- closesocket(client_data[id].socket);
- }
- shutdown(network.mySocket, SD_BOTH);
- closesocket(network.mySocket);
- network.init_socket();
- num_clients = 0;
- printf("Stopped hosting!\n");
- }
- else if (client){
- shutdown(network.mySocket, SD_BOTH);
- closesocket(network.mySocket);
- network.init_socket();
- printf("Disconnected!\n");
- }
- else{printf("Not connected!\n");}
- networking = false;
- client = false;
- host = false;
- }
- void host_server(int set_port)
- {
- if (host || client){
- if (MessageBox( NULL, "You must disconnect first.\nDo you want to disconnect now and connect to a new host?","Disconnect and Continue?", MB_YESNO | MB_ICONINFORMATION)==IDYES){ disconnect(); }
- else{return;}
- }
- //init and start hosting
- if (!network.init_socket()){return;}
- if (set_port != port){port=set_port;}
- if (!network.StartHosting(port)){return;}
- printf("Hosting on port: %d\n", port);
- WSAAsyncSelect(network.mySocket, window.hWnd, WM_SOCKET, (FD_READ | FD_ACCEPT | FD_CLOSE));
- host = true;
- networking = true;
- }
- void accept_connection()
- {
- int addr_size = sizeof(sockaddr);
- client_data[num_clients].socket = accept(network.mySocket, &client_data[num_clients].socketADDR, &addr_size);
- if (client_data[num_clients].socket==INVALID_SOCKET){printf("Unable to accept connection!\n");return;}
- printf("Client Connected!\n");
- num_clients++;
- }
- void close_connection(int socket_id, int Error)
- {
- if (Error != 0){
- //possible errors: WSAENETDOWN WSAECONNRESET WSAECONNABORTED
- printf("Connection errror.");
- PostQuitMessage(0);
- WSACleanup();
- return;
- }
- if (client){
- printf("Host Disconnected!\n");
- disconnect();
- return;
- }
- //host: close client socket
- int s_id = 0;
- for (int id=0; id<num_clients; id++){
- if (int(client_data[id].socket) == socket_id){s_id=id;}
- }
- printf("Client (%d) Disconnected!\n",s_id);
- shutdown(client_data[s_id].socket, SD_BOTH);
- closesocket(client_data[s_id].socket);
- //re-jiggle the clients
- for (int id=s_id+1; id<num_clients; id++){
- client_data[id] = client_data[id-1];
- }
- num_clients--;
- }
- void recieve_data(int socket_id)
- {
- char buffer[64];
- memset(&buffer,0,64);
- recv(socket_id, buffer, 64, 0);
- // [0] *message id*, [1] *size high bits*, [2] *size low bits*, [3] *data*...
- string message = (string)buffer;
- if (message[0] == 1){//
- }
- if (message[0] == 2){//
- }
- if (message[0] == 3){//sync data
- //recieve a load of data
- //used for when a client connects etc
- }
- if (message[0] == 4){//
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement