public byte[] ToByteArray(bool cs = false) { using (ByteBuffer buffer = new ByteBuffer()) { if (!cs) { buffer.WriteByte((byte)this.Count); } foreach (int sn in this) { buffer.WriteInt(sn); } if (cs) { for (int i = this.Count; i < 10; i++) { buffer.WriteInt(0); } } buffer.Flip(); return(buffer.GetContent()); } }
public byte[] ToBytes() { ByteBuffer byteBuffer = new ByteBuffer(); byteBuffer.WriteByte((Byte)PrefabName.Length); byteBuffer.WriteString(PrefabName); byteBuffer.WriteInt((int)(X * 100f)); byteBuffer.WriteInt((int)(Y * 100f)); byteBuffer.WriteInt((int)(Z * 100f)); byteBuffer.WriteShort((short)(RotationX * 100f)); byteBuffer.WriteShort((short)(RotationX * 100f)); byteBuffer.WriteShort((short)(RotationX * 100f)); byteBuffer.WriteShort((short)(ScaleX * 100f)); byteBuffer.WriteShort((short)(ScaleY * 100f)); byteBuffer.WriteShort((short)(ScaleZ * 100f)); byteBuffer.WriteByte((Byte)(type)); if (type != GameObjectTypes.Block) { return(byteBuffer.ToBytes()); } byteBuffer.WriteByte((Byte)(ColliderType)); byteBuffer.WriteShort((Byte)(Radius * 100f)); if (ColliderType == ColliderTypes.CapsuleCollider) { byteBuffer.WriteShort((Byte)(Height * 100f)); } return(byteBuffer.ToBytes()); }
private byte[] PackProtocol(IMessage protocol, int addition = 0, List <int> player_id_list = null) { ByteBuffer buffer = new ByteBuffer(); //写入附加位 buffer.WriteInt(addition); //写入玩家ID列表 if (player_id_list != null) { buffer.WriteInt(player_id_list.Count); foreach (int user_id in player_id_list) { buffer.WriteInt(user_id); } } else { buffer.WriteInt(0); } //写入协议体 byte[] protocol_bytes = Protocol.Encode(protocol); buffer.WriteBytes(protocol_bytes); byte[] msg_bytes = buffer.ToBytes(); buffer.Clear(); buffer.WriteInt(msg_bytes.Length); buffer.WriteBytes(Encrypt.Encode(msg_bytes, Encrypt.DefaultKey)); msg_bytes = buffer.ToBytes(); buffer.Close(); return(msg_bytes); }
public void OnClickSendButton() { if (!CanTalk) { return; } ByteBuffer b = new ByteBuffer(); Talk talk = new Talk(); talk.Content = mInputField.text; MemoryStream ms = new MemoryStream(); talk.WriteTo(ms); byte[] bytes = ms.ToArray(); b.WriteInt(bytes.Length); b.WriteInt(2); b.WriteBytes(bytes); netMgr.SendMessage(b); CanTalk = false; }
/// <summary> /// Notifies already connected clients of new player /// </summary> /// <param name="id"></param> /// <param name="p"></param> static void NotifyAlreadyConnected(int id, Player p) { ByteBuffer buffer = new ByteBuffer(); buffer.WriteInt((int)Enums.AllEnums.SSendingMainToAlreadyConnected); buffer.WriteString(p.uName); buffer.WriteString(p.cName); buffer.WriteInt(p.head); buffer.WriteInt(p.body); buffer.WriteInt(p.cloths); for (int i = 0; i < 20; i++) { if (Globals.clients[i] != null && Globals.clients[i].Connected) // sends current player to already connected player { if (i != id) { try { Globals.clients[i].GetStream().Write(buffer.ToArray(), 0, buffer.ToArray().Length); Globals.clients[i].GetStream().Flush(); } catch (Exception) { continue; } } } } }
/// <summary> /// 将配置数据按照值对的形式写入二进制数据 /// </summary> /// <param name="byteBuffer"></param> /// <param name="typeValue"></param> /// <param name="field"></param> /// <param name="propName"></param> private static void WriteField(ref ByteBuffer byteBuffer, string typeValue, string field, string propName) { switch (typeValue) { case "int32": Debug.Log("Write in int32, " + propName + " = " + int.Parse(field)); byteBuffer.WriteInt(int.Parse(field)); break; case "string": Debug.Log("Write in string, " + propName + " = " + field); byteBuffer.WriteString(field); break; case "float": Debug.Log("Write in float, " + propName + " = " + float.Parse(field)); byteBuffer.WriteFloat(float.Parse(field)); break; case "bool": int value = field == "True" || field == "TRUE" ? 1 : 0; Debug.Log("Write in bool, field is " + field); Debug.Log("Write in bool, " + propName + " = " + value); byteBuffer.WriteInt(value); break; } }
/// <summary> /// Sends already connected clients to the connecting one /// </summary> /// <param name="id"></param> /// <param name="p"></param> static void NotifyMainPlayerOfAlreadyConnected(int id) // sends already connected to players current player { for (int i = 0; i < 20; i++) { if (Globals.clients[i] != null && Globals.clients[i].Connected) { if (i != id) { Console.WriteLine(i); ByteBuffer buffer = new ByteBuffer(); buffer.WriteInt((int)Enums.AllEnums.SSendingAlreadyConnectedToMain); buffer.WriteString(Globals.dicPlayers[i].uName); buffer.WriteString(Globals.dicPlayers[i].cName); buffer.WriteInt(Globals.dicPlayers[i].head); buffer.WriteInt(Globals.dicPlayers[i].body); buffer.WriteInt(Globals.dicPlayers[i].cloths); buffer.WriteFloat(Globals.dicPlayers[i].cX); buffer.WriteFloat(Globals.dicPlayers[i].cY); buffer.WriteFloat(Globals.dicPlayers[i].cZ); Thread.Sleep(1000); //If the thread doesnt sleep, the packet is not sent //Console.WriteLine(buffer.ToArray().Length+" to "+id); try { Globals.clients[id].GetStream().Write(buffer.ToArray(), 0, buffer.ToArray().Length); } catch (Exception) { continue; } } } } }
public void SendMessage(int id, int opcode, byte[] data) { // if (IsConnected == false) { return; } try { ByteBuffer buffer = ByteBuffer.Allocate(512); buffer.WriteInt(data.Length); buffer.WriteInt(opcode); buffer.WriteInt(id); buffer.WriteBytes(data); byte[] sendData = buffer.ToArray(); clientSocket.Send(sendData); // Debug.Log("send success,size = "+data.Length); // Monitor.Wait(); } catch { IsConnected = false; clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); Debug.Log("send fail"); } }
/// <summary> /// 发送数据给服务器 /// </summary> // public void SendMessage(string data) // { // if (IsConnected == false) // return; // try // { // ByteBuffer buffer = ByteBuffer.Allocate(512); // buffer.WriteString(data); // clientSocket.Send(WriteMessage(buffer.ToBytes())); // } // catch // { // IsConnected = false; // clientSocket.Shutdown(SocketShutdown.Both); // clientSocket.Close(); // } // } public void SendMessageAsyc(int id, int opcode, byte[] data, ActionForReceive action) { // if (IsConnected == false) { return; } try { ByteBuffer buffer = ByteBuffer.Allocate(512); buffer.WriteInt(data.Length); buffer.WriteInt(opcode); buffer.WriteInt(id); buffer.WriteBytes(data); byte[] sendData = buffer.ToArray(); clientSocket.Send(sendData); dic.Add(id, action); Debug.Log("send success,size = " + data.Length + ",id:" + id); } catch (Exception e) { Debug.Log("send fail," + e); IsConnected = false; clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); } }
/// <summary> /// 发送消息给server /// </summary> /// <param name="datas"></param> public void SendMessage(int msgId, byte[] datas) { ByteBuffer writeBuffer = null; try { if (this.socket == null) { return; } writeBuffer = new ByteBuffer(); writeBuffer.WriteInt(msgId); writeBuffer.WriteInt(datas.Length); writeBuffer.WriteBytes(datas); var sendBytes = writeBuffer.ToBytes(); DebugLog(identiy, "send msg id : " + msgId + " len: " + datas.Length); writeBuffer.Close(); socket.BeginSend(sendBytes, 0, sendBytes.Length, SocketFlags.None, new AsyncCallback(this.SendMessageCallBack), socket); } catch (Exception e) { if (writeBuffer != null) { writeBuffer.Close(); } DoSocketException(); DebugLogError(identiy, e.ToString()); } }
/// <summary> /// Returns stats of all current connected players /// </summary> private void HListPlayers() { ByteBuffer buffer = new ByteBuffer(); TimeSpan aux = new TimeSpan(); if (Globals.dicPlayers.Count == 0) { buffer.WriteByte(0); Globals.httpClient[clNo].GetStream().Write(buffer.ToArray(), 0, buffer.ToArray().Length); return; } buffer.WriteInt(Globals.dicPlayers.Count); foreach (Player p in Globals.dicPlayers.Values) { Console.WriteLine(">>> List players: " + p.uName); buffer.WriteString(p.uName); buffer.WriteString(p.cName); buffer.WriteString(p.playerIP); aux = DateTime.Now - p.currentPlaytime; buffer.WriteInt(aux.Hours * 60 + aux.Minutes); //buffer.WriteInt(p.totalPlaytime+(aux.Hours * 60 + aux.Minutes)); } Console.WriteLine("\n"); Globals.httpClient[clNo].GetStream().Write(buffer.ToArray(), 0, buffer.ToArray().Length); }
/// <summary> /// 获取连接状态 /// </summary> /// <returns></returns> bool IsConnected() { bool ConnectState = true; bool state = mSocket.Blocking; try { ByteBuffer buf = ByteBuffer.Allocate(4 + 4); buf.WriteInt(4); buf.WriteInt((int)MessageId.C2G_HeartBeat); mSocket.Blocking = false; int length = mSocket.Send(buf.GetBytes(), 0, SocketFlags.None); ConnectState = length != 0; } catch (SocketException e) { ConnectState = e.NativeErrorCode.Equals(10035); } finally { mSocket.Blocking = state; } return(ConnectState); }
private static void _SendMessageAsyc(int opcode, byte[] data, ActionForReceive action) { // // if (IsConnected == false) { // ConnectServerAndLogin (); // if (!IsConnected) { // throw new Exception("server is not connect"); // } // } try { int id = Interlocked.Increment(ref idIndex); ByteBuffer buffer = ByteBuffer.Allocate(data.Length + 12); buffer.WriteInt(data.Length); buffer.WriteInt(opcode); buffer.WriteInt(id); buffer.WriteBytes(data); byte[] sendData = buffer.ToArray(); clientSocket.Send(sendData); dic.Add(id, action); } catch (Exception e) { Debug.Log("send fail," + e); // IsConnected = false; // clientSocket.Shutdown(SocketShutdown.Both); // clientSocket.Close(); throw new Exception(e.Message); } }
/// <summary> /// Sends new configuration to game server /// </summary> /// <param name="response">Underlaying in and out streams</param> /// <param name="token">Token for authentication</param> private void ChangeSettings(HttpListenerContext response, string token) { var requestBody = response.Request.InputStream; byte[] data = new byte[4096]; requestBody.Read(data, 0, (int)(response.Request.ContentLength64)); string msg = Encoding.UTF8.GetString(data); ChangeSettingsJson csj = JsonConvert.DeserializeObject <ChangeSettingsJson>(msg); ByteBuffer buffer = new ByteBuffer(); buffer.WriteByte((int)EnumsServer.Enums.AllEnums.HChangeSettings); //mudar buffer.WriteInt(csj.concurrent_players); buffer.WriteInt(csj.port); buffer.WriteByte(Convert.ToByte(csj.restart)); SendToGameServer(buffer.ToArray()); while (!client.GetStream().DataAvailable) { Thread.Sleep(50); } client.GetStream().Read(data, 0, 1); SendToClient(response, "{\"success\":" + Convert.ToBoolean(data[0]) + "}", 200); }
public virtual void writeCompressed(ExtendedDataOutput output) { int dataType = (int)this.getDataType(); int unitLength = getUnitLength(this.getDataType()); int elementCount = this.rows(); int maxCompressedLength = this.rows() * sizeof(long) * 8 * 2 + 64 * 3; ByteBuffer outBuffer = ByteBuffer.Allocate(Math.Max(maxCompressedLength, 65536)); outBuffer.order(output.GetType() == typeof(LittleEndianDataOutputStream)); short flag = (short)((short)DATA_FORM.DF_VECTOR << 8 | (short)DATA_TYPE.DT_COMPRESS & 0xff); outBuffer.WriteShort(flag); outBuffer.WriteInt(0); // compressedBytes outBuffer.WriteInt(1); // cols outBuffer.WriteByte((byte)0); // version outBuffer.WriteByte((byte)1); // flag bit0:littleEndian bit1:containChecksum outBuffer.WriteByte(unchecked ((byte)-1)); // charcode outBuffer.WriteByte((byte)compressedMethod); outBuffer.WriteByte((byte)dataType); outBuffer.WriteByte((byte)unitLength); outBuffer.WriteByte((byte)0); outBuffer.WriteByte((byte)0); outBuffer.WriteInt(-1); //extra outBuffer.WriteInt(elementCount); outBuffer.WriteInt(-1); //TODO: checkSum EncoderFactory.Get(compressedMethod).compress(this, elementCount, unitLength, maxCompressedLength, outBuffer); int compressedLength = outBuffer.ReadableBytes - 10; outBuffer.PutInt(compressedLength, 2); byte[] tmp = new byte[outBuffer.ReadableBytes]; output.write(outBuffer.ToArray()); }
void sendMsgInt(int id, int msg) { ByteBuffer buffer = new ByteBuffer(); buffer.WriteInt(id); buffer.WriteInt(msg); client.Send(buffer.ToBytes()); }
public static void LevelRoom() { ByteBuffer buffer = new ByteBuffer(); buffer.WriteInt(0); buffer.WriteInt((int)CProtocol.SLevelRoom); buffer.WriteMd5(); UserInfo.Net.SendMessage(buffer.ToBytes()); }
public ByteBuffer CreateColumnVector(int rows, int extra, int unitLength, bool isLittleEndian, int minSize) { ByteBuffer ret = ByteBuffer.Allocate(Math.Max(rows * unitLength + 8, minSize)); ret.order(isLittleEndian); ret.WriteInt(rows); ret.WriteInt(extra); return(ret); }
public byte bHasVideo; //有无视频 protected override void WriteData(ByteBuffer buffer) { buffer.WriteInt(dwPlazaVersion); buffer.WriteInt(dwProcessVersion); buffer.WriteInt(dwUserID); buffer.WriteString(KKNetUtil.Get4scalPasswordCN(szPassWord, 33)); buffer.WriteByte(bHasVideo); buffer.WriteBytes(new byte[2]); }
internal void Encode(ByteBuffer os) { os.WriteString(Name); os.WriteInt(LocalId); os.WriteInt(LocalStep); lock (this) // 在Transaction.FlushReadWriteLock保护下应该是不需要需要的。 { os.WriteLong(Current); } }
void getInfoFromNet() { ByteBuffer buffer = ByteBuffer.Allocate(1024); buffer.skip(4); buffer.WriteString("TimeLineEvent"); buffer.WriteInt(_year); buffer.WriteInt(_month); NetUtil.getInstance.SendMessage(buffer); }
public static bool RBetFailed = false; //投注失败 #region 务器相关方法 public static void EnterRoom(int roomID) { ByteBuffer buffer = new ByteBuffer(); buffer.WriteInt(0); buffer.WriteInt((int)CProtocol.SEnterRoom); buffer.WriteInt(1000); buffer.WriteMd5(); UserInfo.Net.SendMessage(buffer.ToBytes()); }
public void Request_EditHeadPic(int headPicId) { ByteBuffer buffer = new ByteBuffer(); buffer.WriteInt(8); buffer.WriteInt((int)CProtocol.SEditHeadPic); buffer.WriteInt(headPicId); //headpicId buffer.WriteMd5(); UserInfo.Net.SendMessage(buffer.ToBytes()); }
public static void AcceptConnection() { ByteBuffer buffer = new ByteBuffer(); buffer.WriteInt((int)PacketEnumeration.ClientPackets.AcceptConnection); buffer.WriteInt(1); // 버전 체크 등의 사항 추가하기 Send(buffer.ToArray()); buffer.Dispose(); }
/// <summary> /// 构建消息数据包 /// </summary> /// <param name="protobufModel"></param> /// <param name="messageId"></param> byte[] BuildPackage(Message c2s) { byte[] b = ProtobufSerilizer.Serialize(c2s.message); ByteBuffer buf = ByteBuffer.Allocate(b.Length + 4 + 4); buf.WriteInt(b.Length); //buf.WriteShort((short)(b.Length)); buf.WriteInt((int)c2s.messageId); buf.WriteBytes(b); return(buf.GetBytes()); }
public void SendMovement(int id, float x, float y, float z) { ByteBuffer buffer = new ByteBuffer(); buffer.WriteInt((int)Assets.Scripts.Enums.AllEnums.SSyncingPlayerMovement); buffer.WriteInt(id); buffer.WriteFloat(x); buffer.WriteFloat(y); buffer.WriteFloat(z); myStream.Write(buffer.ToArray(), 0, buffer.ToArray().Length); myStream.Flush(); }
public static void Welcome(int _sendToPlayer, string _msg) { ByteBuffer _buffer = new ByteBuffer(); _buffer.WriteInt((int)ServerPackets.welcome); _buffer.WriteString(_msg); _buffer.WriteInt(_sendToPlayer); SendDataTo(_sendToPlayer, _buffer.ToArray()); _buffer.Dispose(); }
// 发送验证消息 public static void SendVerifyMessage(int type) { ByteBuffer buff = new ByteBuffer(); buff.WriteInt(GameConst.MSG_VERIFY_ID); buff.WriteInt(4); buff.WriteInt(type); byte[] bytes = buff.ToBytes(); buff.Close(); GameSocket.Instance.SendMessageNoCallBack(bytes); }
public void Encode(ByteBuffer bb) { bb.WriteInt(Tables.Count); foreach (var table in Tables.Values) { table.Encode(bb); } bb.WriteInt(Beans.Count); foreach (var bean in Beans.Values) { bean.Encode(bb); } }
void sendMsgInt(int id, int msg) { ByteBuffer buffer = new ByteBuffer(); buffer.WriteInt(id); buffer.WriteInt(msg); for (int i = 0; i < socketList.Count; i++) { int msgLength = socketList[i].Send(buffer.ToBytes()); Debug.Log(TAG + "Send msg length: " + msgLength); } }
public BaseRqst(int protoId) { m_Bytes = new ByteBuffer(); m_Bytes.WriteInt(protoId); }