Ejemplo n.º 1
0
 private void DoSend(byte[] data, Protocal type = Protocal.Tcp)
 {
     //SafeDebug.Log("Sending some shit 1");
     if (type == Protocal.Tcp || !udpStarted)
     {
         //SafeDebug.Log("Sending some shit 2");
         if (tcpClient != null && tcpSocket != null)
         {
             //if ((ServerCMD)data[0] != ServerCMD.Ping)
             //    SafeDebug.Log("SENDING: " + (ServerCMD)data[0]);
             data = BufferUtils.AddLength(data);
             //SafeDebug.Log("Sending some shit 3");
             tcpSocket.Send(data);
         }
         else
         {
             SafeDebug.LogError("Client TCP socket null!");
         }
     }
     else
     {
         if (udpClient != null)
         {
             byte[] udpIdBuff = BitConverter.GetBytes((UInt16)UdpID);
             byte[] buffer    = BufferUtils.Add(udpIdBuff, data);
             udpClient.Send(buffer, buffer.Length);
         }
         else
         {
             DoSend(data);
         }
     }
 }
Ejemplo n.º 2
0
        private async Task Accept(SocketUser user)
        {
            try
            {
                //Logger.Log("Client connecting...");
                byte result  = 0x00;
                bool success = await user.HandleStartConnect();

                //Logger.Log("Client Connected: " + success);
                Token.ThrowIfCancellationRequested();
                byte[] udpidBuff = new byte[0];
                if (success)
                {
                    AddPlayer(user.SessionToken, user);
                    result = 0x01;
                    ushort udpid = GetNewUdpID();
                    user.UdpID = udpid;
                    AddUdpID(udpid, user.SessionToken);
                    udpidBuff = BitConverter.GetBytes(udpid);
                    ServerBase.BaseInstance.UserConnected(user);
                }
                user.Send(0xff, BufferUtils.Add(new byte[] { 0x01, result }, udpidBuff));
                if (success)
                {
                    //Logger.Log("Start handling messages...");
                    await user.HandleMessages();
                }
            }
            catch (Exception ex)
            {
                Logger.LogError("{0}: {1}\n{2}", ex.GetType(), ex.Message, ex.StackTrace);
            }
            finally
            {
                if (user.Client != null)
                {
                    user.Client.Close();
                }
            }
        }
Ejemplo n.º 3
0
 private byte[] AddLength(byte[] data)
 {
     byte[] lengthBuff = BitConverter.GetBytes((UInt16)(data.Length + 2));
     return(BufferUtils.Add(lengthBuff, data));
 }