Skip to content

Instantly share code, notes, and snippets.

@OperationDarkside
Created November 20, 2017 21:33
Show Gist options
  • Save OperationDarkside/bd5f157f4d11df11881dfc66a34bd4aa to your computer and use it in GitHub Desktop.
Save OperationDarkside/bd5f157f4d11df11881dfc66a34bd4aa to your computer and use it in GitHub Desktop.
Minecraft Clone Network test
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TestRange {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
Thread t = new Thread(new ThreadStart(receive_game_data));
t.Start();
/*Thread.Sleep(500);
UdpClient dsfgdgf = new UdpClient("127.0.0.1", 1337);
byte[] data = { 0, 1, 20, 3 };
dsfgdgf.Send(data, data.Length);
*/
}
public void receive_game_data() {
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
EndPoint ep = new IPEndPoint(IPAddress.Loopback, 1337);
sock.Bind(ep);
while (true) {
byte[] raw = new byte[512];
sock.ReceiveFrom(raw, ref ep);
// byte[] raw = client.Receive(ref groupEP);
int x = BitConverter.ToInt32(raw, 0);
int y = BitConverter.ToInt32(raw, 4);
int z = BitConverter.ToInt32(raw, 8);
int r_x = BitConverter.ToInt32(raw, 12);
int r_y = BitConverter.ToInt32(raw, 16);
int r_z = BitConverter.ToInt32(raw, 20);
string output = "";
output += x + "\r\n";
output += y + "\r\n";
output += z + "\r\n";
output += r_x + "\r\n";
output += r_y + "\r\n";
output += r_z;
AppendTextBox(output);
}
}
public void AppendTextBox(string value) {
if (InvokeRequired) {
this.Invoke(new Action<string>(AppendTextBox), new object[] { value });
return;
}
textBox1.Text = value;
}
}
}
#include "PlayPositionTest.h"
void PlayPositionTest::run(){
m_net_thread = std::thread([this](){
run_internal();
});
}
void PlayPositionTest::setPlayer(const Player* _player) {
m_player = _player;
}
struct Data {
int x = 0;
int y = 0;
int z = 0;
int r_x = 0;
int r_y = 0;
int r_z = 0;
};
void PlayPositionTest::run_internal(){
Data d;
sf::Clock clock;
sf::Time time;
//sf::Packet packet;
is_running = true;
// Init Sockets
SOCKET sock;
WSADATA wsa;
struct sockaddr_in addr;
int slen;
slen = sizeof(addr) ;
//Initialise winsock
long rc = WSAStartup(MAKEWORD(2,2),&wsa);
if (rc != 0)
{
std::cout << "Failed. Error Code : " << WSAGetLastError();
}
// Socket
if((sock = socket(AF_INET , SOCK_DGRAM , 0 )) == INVALID_SOCKET)
{
std::cout << "Could not create socket : " << WSAGetLastError();
}
// Address
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("127.0.0.1");;
addr.sin_port = htons( 1337 );
while(is_running){
if(m_player == nullptr){
return;
}
// Get Position
d.x = m_player->position.x;
d.y = m_player->position.y;
d.z = m_player->position.z;
// Get Rotation
d.r_x = m_player->rotation.x;
d.r_y = m_player->rotation.y;
d.r_z = m_player->rotation.z;
// Preparing data
// packet << d.x << d.y << d.z << d.r_x << d.r_y << d.r_z;
std::array<int, 6> buffer = {d.x, d.y, d.z, d.r_x, d.r_y, d.r_z};
std::cout << "Trying to send data\n";
// Send data
if (sendto(sock, (char*)buffer.data(), 6 * sizeof(int), 0, (struct sockaddr*) &addr, slen) == SOCKET_ERROR)
{
std::cout << "sendto() failed with error code : " << WSAGetLastError();
}
std::cout << "Data sent!\n";
// FPS Limiting
time = clock.restart();
if(time.asMicroseconds() < tick_time){
sf::sleep(sf::microseconds(tick_time - time.asMicroseconds()));
}
}
}
// HEADER FILE
#ifndef PLAYPOSITIONTEST_H
#define PLAYPOSITIONTEST_H
#include <iostream>
#include <thread>
#include<winsock2.h>
#include <windows.h>
// #include <SFML/Network.hpp>
#include "../Player/Player.h"
class PlayPositionTest {
public:
void run();
void setPlayer(const Player* _player);
bool is_running = false;
private:
void run_internal();
std::thread m_net_thread;
const Player* m_player;
const static long ticks_per_scond = 2;
constexpr const static long tick_time = ((float)1 / (float)ticks_per_scond) * 1000000;
};
#endif // PLAYPOSITIONTEST_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment