Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client connection closes automatically after a while #92

Open
eliteforcevn opened this issue Nov 3, 2023 · 3 comments · May be fixed by #97
Open

Client connection closes automatically after a while #92

eliteforcevn opened this issue Nov 3, 2023 · 3 comments · May be fixed by #97

Comments

@eliteforcevn
Copy link

eliteforcevn commented Nov 3, 2023

On Unity Editor the connection automatic closed after a period of not receiving or sending messages
My server has packets sent in a certain order. If the wrong packet is sent, the connection will be disconnected. I wonder if nativesocket will automatically send anything?
Thanks for your works nativesocket very useful :3

@eliteforcevn
Copy link
Author

using System;

public class MessageReceiving {
private int lengData;
private int currentReader;

public short cmd;
private byte[] buffer;
private bool isMessageCorrect;

public long timeProcess;
public MessageReceiving(byte[] DATA) {
	int ch1 = DATA[0] & 0xFF;
	int ch2 = DATA[1] & 0xFF;
	cmd=(short)((ch1 << 8) | (ch2 << 0));

    lengData=DATA.Length;
	currentReader=2;
	buffer=DATA;

	isMessageCorrect=true;
}
public short getCMD() {return cmd;}
//public string getCMDName(){return CMD_REALTIME.getCMDName(cmd);}
public int avaiable() {return lengData-currentReader;}
public void moveToEnd(){currentReader=lengData;}
public byte[] getEndByte() {
	int lengClone=lengData-currentReader;
	byte[] data=new byte[lengClone];
	for(int i=0;i<lengClone;i++)
		data[i]=buffer[i+currentReader];
	return data;
}
public bool validate() {return isMessageCorrect && (lengData-currentReader)==0;}
public bool isCorrect() {return isMessageCorrect;}
public bool isRelease() {return lengData-currentReader==0;}
public int lengthReceive() { return lengData; }
public void skip(int numberArray){
	if(currentReader+numberArray>lengData){
		isMessageCorrect=false;
	}else
		currentReader+=numberArray;
}
public bool readBoolean() {
	if(currentReader<lengData){
		bool result=buffer[currentReader]!=0;
		currentReader++;
		return result;
	}else{
		isMessageCorrect=false;
		return false;
	}
}
public sbyte readByte() {
	if(currentReader<lengData){
		sbyte result= (sbyte)buffer[currentReader];
		currentReader++;
		return result;
	}else{
		isMessageCorrect=false;
		return 0;
	}
}

public short readShort() {
	if(currentReader+1<lengData){
		int ch1 = buffer[currentReader] & 0xFF;
		int ch2 = buffer[currentReader+1] & 0xFF;
		currentReader=currentReader+2;
		return (short)((ch1 << 8) + (ch2 << 0));
	}else{
		isMessageCorrect=false;
		return 0;
	}
}

public int readInt() {
	if(currentReader+3<lengData){
        int ch1 = buffer[currentReader] & 0xFF;
        int ch2 = buffer[currentReader+1] & 0xFF;
        int ch3 = buffer[currentReader+2] & 0xFF;
        int ch4 = buffer[currentReader+3] & 0xFF;
		currentReader=currentReader+4;
		return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
	}else{
		isMessageCorrect=false;
		return 0;
	}
}

public long readLong() {
	if(currentReader+7<lengData){
		long l0 = buffer[currentReader] & 0xFF;
		long l1 = buffer[currentReader+1] & 0xFF;
		long l2 = buffer[currentReader+2] & 0xFF;
		long l3 = buffer[currentReader+3] & 0xFF;
		long l4 = buffer[currentReader+4] & 0xFF;
		long l5 = buffer[currentReader+5] & 0xFF;
		long l6 = buffer[currentReader+6] & 0xFF;
		long l7 = buffer[currentReader+7] & 0xFF;

		long r0 = l0 << 56;
		long r1 = l1 << 48;
		long r2 = l2 << 40;
		long r3 = l3 << 32;
		long r4 = l4 << 24;
		long r5 = l5 << 16;
		long r6 = l6 << 8;
		long r7 = l7;
		currentReader=currentReader+8;
		return r0 + r1 + r2 + r3 + r4 + r5 + r6 + r7;
	}else{
		isMessageCorrect=false;
		return 0;
	}
}

public float readFloatFromInt() {return ((float)readInt())/1000;}
public double readDoubleFromLong() {return ((double)readLong())/1000000;}
public sbyte[] readMiniByte(){
	sbyte lengthReceive = readByte();
	if (lengthReceive < 1)
		return null;
	sbyte[] dataReceive = new sbyte[lengthReceive];
	for (sbyte i = 0; i < lengthReceive; i++)
		dataReceive[i] = readByte();
	return dataReceive;
}

public String readString(){
	if(lengData-currentReader<2){
		isMessageCorrect=false;
		return "";
	}
	int utflen = (((buffer[currentReader] & 0xff) << 8) | (buffer[currentReader+1] & 0xff));
	if(lengData-currentReader<utflen+2){
		isMessageCorrect=false;
		return "";
	}
	byte[] bytearr = null;
	char[] chararr = null;

// if(data.length<utflen){
bytearr = new byte[utflen2];
chararr = new char[utflen
2];
// }

	int c, char2, char3;
	int count = 0;
	int chararr_count=0;
	
	for(int i=0;i<utflen;i++)
		bytearr[i]=buffer[i+currentReader+2];
	
	currentReader=currentReader+utflen+2;
	
	while (count < utflen) {
		c = (int) bytearr[count] & 0xff;
		if (c > 127) break;
		count++;
		chararr[chararr_count++]=(char)c;
	}
	
	while (count < utflen) {
		c = (int) bytearr[count] & 0xff;
		switch (c >> 4) {
			case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
				/* 0xxxxxxx*/
				count++;
				chararr[chararr_count++]=(char)c;
				break;
			case 12: case 13:
				/* 110x xxxx   10xx xxxx*/
				count += 2;
				if (count > utflen){

// throw new UTFDataFormatException("malformed input: partial character at end");
return "";
}
char2 = (int) bytearr[count-1];
if ((char2 & 0xC0) != 0x80){
// throw new UTFDataFormatException("malformed input around byte " + count);
return "";
}
chararr[chararr_count++]=(char)(((c & 0x1F) << 6) |
(char2 & 0x3F));
break;
case 14:
/* 1110 xxxx 10xx xxxx 10xx xxxx /
count += 3;
if (count > utflen){
// throw new UTFDataFormatException("malformed input: partial character at end");
return "";
}
char2 = (int) bytearr[count-2];
char3 = (int) bytearr[count-1];
if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80)){
// throw new UTFDataFormatException("malformed input around byte " + (count-1));
return "";
}
chararr[chararr_count++]=(char)(((c & 0x0F) << 12) |
((char2 & 0x3F) << 6) |
((char3 & 0x3F) << 0));
break;
default:
/
10xx xxxx, 1111 xxxx */
// throw new UTFDataFormatException("malformed input around byte " + count);
return "";
}
}
// The number of chars produced may be less than utflen
return new String(chararr, 0, chararr_count);
}
}

1 similar comment
@eliteforcevn
Copy link
Author

using System;

public class MessageReceiving {
private int lengData;
private int currentReader;

public short cmd;
private byte[] buffer;
private bool isMessageCorrect;

public long timeProcess;
public MessageReceiving(byte[] DATA) {
	int ch1 = DATA[0] & 0xFF;
	int ch2 = DATA[1] & 0xFF;
	cmd=(short)((ch1 << 8) | (ch2 << 0));

    lengData=DATA.Length;
	currentReader=2;
	buffer=DATA;

	isMessageCorrect=true;
}
public short getCMD() {return cmd;}
//public string getCMDName(){return CMD_REALTIME.getCMDName(cmd);}
public int avaiable() {return lengData-currentReader;}
public void moveToEnd(){currentReader=lengData;}
public byte[] getEndByte() {
	int lengClone=lengData-currentReader;
	byte[] data=new byte[lengClone];
	for(int i=0;i<lengClone;i++)
		data[i]=buffer[i+currentReader];
	return data;
}
public bool validate() {return isMessageCorrect && (lengData-currentReader)==0;}
public bool isCorrect() {return isMessageCorrect;}
public bool isRelease() {return lengData-currentReader==0;}
public int lengthReceive() { return lengData; }
public void skip(int numberArray){
	if(currentReader+numberArray>lengData){
		isMessageCorrect=false;
	}else
		currentReader+=numberArray;
}
public bool readBoolean() {
	if(currentReader<lengData){
		bool result=buffer[currentReader]!=0;
		currentReader++;
		return result;
	}else{
		isMessageCorrect=false;
		return false;
	}
}
public sbyte readByte() {
	if(currentReader<lengData){
		sbyte result= (sbyte)buffer[currentReader];
		currentReader++;
		return result;
	}else{
		isMessageCorrect=false;
		return 0;
	}
}

public short readShort() {
	if(currentReader+1<lengData){
		int ch1 = buffer[currentReader] & 0xFF;
		int ch2 = buffer[currentReader+1] & 0xFF;
		currentReader=currentReader+2;
		return (short)((ch1 << 8) + (ch2 << 0));
	}else{
		isMessageCorrect=false;
		return 0;
	}
}

public int readInt() {
	if(currentReader+3<lengData){
        int ch1 = buffer[currentReader] & 0xFF;
        int ch2 = buffer[currentReader+1] & 0xFF;
        int ch3 = buffer[currentReader+2] & 0xFF;
        int ch4 = buffer[currentReader+3] & 0xFF;
		currentReader=currentReader+4;
		return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
	}else{
		isMessageCorrect=false;
		return 0;
	}
}

public long readLong() {
	if(currentReader+7<lengData){
		long l0 = buffer[currentReader] & 0xFF;
		long l1 = buffer[currentReader+1] & 0xFF;
		long l2 = buffer[currentReader+2] & 0xFF;
		long l3 = buffer[currentReader+3] & 0xFF;
		long l4 = buffer[currentReader+4] & 0xFF;
		long l5 = buffer[currentReader+5] & 0xFF;
		long l6 = buffer[currentReader+6] & 0xFF;
		long l7 = buffer[currentReader+7] & 0xFF;

		long r0 = l0 << 56;
		long r1 = l1 << 48;
		long r2 = l2 << 40;
		long r3 = l3 << 32;
		long r4 = l4 << 24;
		long r5 = l5 << 16;
		long r6 = l6 << 8;
		long r7 = l7;
		currentReader=currentReader+8;
		return r0 + r1 + r2 + r3 + r4 + r5 + r6 + r7;
	}else{
		isMessageCorrect=false;
		return 0;
	}
}

public float readFloatFromInt() {return ((float)readInt())/1000;}
public double readDoubleFromLong() {return ((double)readLong())/1000000;}
public sbyte[] readMiniByte(){
	sbyte lengthReceive = readByte();
	if (lengthReceive < 1)
		return null;
	sbyte[] dataReceive = new sbyte[lengthReceive];
	for (sbyte i = 0; i < lengthReceive; i++)
		dataReceive[i] = readByte();
	return dataReceive;
}

public String readString(){
	if(lengData-currentReader<2){
		isMessageCorrect=false;
		return "";
	}
	int utflen = (((buffer[currentReader] & 0xff) << 8) | (buffer[currentReader+1] & 0xff));
	if(lengData-currentReader<utflen+2){
		isMessageCorrect=false;
		return "";
	}
	byte[] bytearr = null;
	char[] chararr = null;

// if(data.length<utflen){
bytearr = new byte[utflen2];
chararr = new char[utflen
2];
// }

	int c, char2, char3;
	int count = 0;
	int chararr_count=0;
	
	for(int i=0;i<utflen;i++)
		bytearr[i]=buffer[i+currentReader+2];
	
	currentReader=currentReader+utflen+2;
	
	while (count < utflen) {
		c = (int) bytearr[count] & 0xff;
		if (c > 127) break;
		count++;
		chararr[chararr_count++]=(char)c;
	}
	
	while (count < utflen) {
		c = (int) bytearr[count] & 0xff;
		switch (c >> 4) {
			case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
				/* 0xxxxxxx*/
				count++;
				chararr[chararr_count++]=(char)c;
				break;
			case 12: case 13:
				/* 110x xxxx   10xx xxxx*/
				count += 2;
				if (count > utflen){

// throw new UTFDataFormatException("malformed input: partial character at end");
return "";
}
char2 = (int) bytearr[count-1];
if ((char2 & 0xC0) != 0x80){
// throw new UTFDataFormatException("malformed input around byte " + count);
return "";
}
chararr[chararr_count++]=(char)(((c & 0x1F) << 6) |
(char2 & 0x3F));
break;
case 14:
/* 1110 xxxx 10xx xxxx 10xx xxxx /
count += 3;
if (count > utflen){
// throw new UTFDataFormatException("malformed input: partial character at end");
return "";
}
char2 = (int) bytearr[count-2];
char3 = (int) bytearr[count-1];
if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80)){
// throw new UTFDataFormatException("malformed input around byte " + (count-1));
return "";
}
chararr[chararr_count++]=(char)(((c & 0x0F) << 12) |
((char2 & 0x3F) << 6) |
((char3 & 0x3F) << 0));
break;
default:
/
10xx xxxx, 1111 xxxx */
// throw new UTFDataFormatException("malformed input around byte " + count);
return "";
}
}
// The number of chars produced may be less than utflen
return new String(chararr, 0, chararr_count);
}
}

@eliteforcevn
Copy link
Author

using System;

public class MessageSending {
public int timeWithNetwork;
private short cmd;
private byte[] data;
public MessageSending(short CMD) {
cmd=CMD;
data = new byte[2];
data[0] = (byte)((int)((uint)CMD >> 8) & 0xFF);
data[1] = (byte)((int)((uint)CMD >> 0) & 0xFF);
}

public void ClearData(){
	data = new byte[2];
	data[0] = (byte)((int)((uint)cmd >> 8) & 0xFF);
	data[1] = (byte)((int)((uint)cmd >> 0) & 0xFF);
}
public int avaiable() {return data.Length;}
public short getCMD() {return cmd;}

public void writeBoolean(bool value) {
    int length=data.Length;
	byte[] temp=new byte[length+1];
	for(int i=0;i<length;i++)
		temp[i]=data[i];
	if(value)
		temp[length]=1;
	else
		temp[length]=0;
	data=temp;
}

public void writeByte(sbyte value) {
	writeByte((byte)value);
}
public void writeByte(byte value) {
    int length=data.Length;
	byte[] temp=new byte[length+1];
	for(int i=0;i<length;i++)
		temp[i]=data[i];
	temp[length]=value;
	data=temp;
}

public void writeByteArray(byte[] arr) {
    int l=data.Length;
	byte[] temp;
	if(arr==null){
		temp=new byte[l+4];
		for(int i=0;i<l;i++)
			temp[i]=data[i];
	}else{
        int larr=arr.Length;
		temp=new byte[l+4+larr];
		for(int i=0;i<l;i++)
			temp[i]=data[i];
		for(int i=0;i<larr;i++)
			temp[l+4+i]=arr[i];
        temp[l]  =(byte)((int)((uint)larr >> 24) & 0xFF);
        temp[l+1]=(byte)((int)((uint)larr >> 16) & 0xFF);
        temp[l+2]=(byte)((int)((uint)larr >> 8) & 0xFF);
        temp[l+3]=(byte)((int)((uint)larr >> 0) & 0xFF);
	}
	data=temp;
}

public void writeshort(short paramInt) {
    int length=data.Length;
	byte[] temp=new byte[length+2];
	for(int i=0;i<length;i++)
		temp[i]=data[i];
    temp[length]	= (byte) ((int)((uint)paramInt >> 8) & 0xFF);
    temp[length+1]	= (byte) ((int)((uint)paramInt >> 0) & 0xFF);
	data=temp;
}

public void writeInt(int paramInt) {
    int length=data.Length;
	byte[] temp=new byte[length+4];
	for(int i=0;i<length;i++)
		temp[i]=data[i];
    temp[length]  =(byte)((int)((uint)paramInt >> 24) & 0xFF);
    temp[length+1]=(byte)((int)((uint)paramInt >> 16) & 0xFF);
    temp[length+2]=(byte)((int)((uint)paramInt >> 8) & 0xFF);
    temp[length+3]=(byte)((int)((uint)paramInt >> 0) & 0xFF);
	data=temp;
}

public void writeLong(long paramLong) {
    int length=data.Length;
	byte[] temp=new byte[length+8];
	for(int i=0;i<length;i++)
		temp[i]=data[i];
    temp[length]   = (byte)((ulong)paramLong >> 56);
    temp[length+1] = (byte)((ulong)paramLong >> 48);
    temp[length+2] = (byte)((ulong)paramLong >> 40);
    temp[length+3] = (byte)((ulong)paramLong >> 32);
    temp[length+4] = (byte)((ulong)paramLong >> 24);
    temp[length+5] = (byte)((ulong)paramLong >> 16);
    temp[length+6] = (byte)((ulong)paramLong >> 8);
    temp[length+7] = (byte)((ulong)paramLong >> 0);
	data=temp;
}

public void writeCopyData(byte[] copyData) {
	if(copyData==null)
		return;
    int lengthCopy=copyData.Length;
	if(lengthCopy==0)
		return;
    int currentLength=data.Length;
	byte[] temp=new byte[currentLength+lengthCopy];
	for(int i=0;i<currentLength;i++)
		temp[i]=data[i];
	for(int i=0;i<lengthCopy;i++)
		temp[i+currentLength]=copyData[i];
	data=temp;
}

public void writeFloatFromInt(float n) {writeInt((int) (n*1000));}
public void writeDoubleFromLong(double n) {writeLong((long) (n*1000000));}

public int writeString(String value) {
	if(value==null)
		value="";
    int stringLenth = value.Length;
	int j = 0;
    int k;
	for (int n = 0; n < stringLenth; n++) {
		k = value[n];
		if ((k >= 1) && (k <= 127)) {
			j++;
		} else if (k > 2047) {
			j += 3;
		} else {
			j += 2;
		}
	}

// if (j > 65535) {
// throw new UTFDataFormatException("encoded string too long: " + j + " bytes");
// }
byte[] arrayOfString = new byte[j * 2 + 2];
arrayOfString[0] = (byte)((int)((uint)j >> 8) & 0xFF);
arrayOfString[1] = (byte)((int)((uint)j >> 0) & 0xFF);

	int count=2;
	int i1 = 0;
	for (i1 = 0; i1 < stringLenth; i1++) {
        k = value[i1];
		if ((k < 1) || (k > 127)) {
			break;
		}
		arrayOfString[(count++)] = ((byte) k);
	}
	while (i1 < stringLenth) {
		k = value[i1];
		if ((k >= 1) && (k <= 127)) {
			arrayOfString[(count++)] = ((byte) k);
		} else if (k > 2047) {
			arrayOfString[(count++)] = ((byte) (0xE0 | k >> 12 & 0xF));
			arrayOfString[(count++)] = ((byte) (0x80 | k >> 6 & 0x3F));
			arrayOfString[(count++)] = ((byte) (0x80 | k >> 0 & 0x3F));
		} else {
			arrayOfString[(count++)] = ((byte) (0xC0 | k >> 6 & 0x1F));
			arrayOfString[(count++)] = ((byte) (0x80 | k >> 0 & 0x3F));
		}
		i1++;
	}

// paramDataOutput.write(arrayOfString, 0, j + 2);
int lengString = j+2;
int lengthData=data.Length;
byte[] temp=new byte[lengthData+lengString];
for(int i=0;i<lengthData;i++)
temp[i]=data[i];
for(int i=0;i<lengString;i++)
temp[lengthData+i]=arrayOfString[i];
data=temp;
return j + 2;
}

public byte[] getBytesArray() {return data;}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant