예제 #1
0
        private void OnReadEnd(IAsyncResult ar)
        {
            int count = client.GetStream().EndRead(ar);

            DLogger.WriteLineToScreen("GTcpClient: EndRead, count: " + count);
            if (count == 0)
            {
                CloseConnection();
                return;
            }
            int cmdSize = NetworkCryptor.NetCommand.Size;
            int mcount  = count / cmdSize;

            for (int i = 0; i < mcount; i++)
            {
                byte[] b = new byte[cmdSize];
                Buffer.BlockCopy(readbuffer, i * cmdSize, b, 0, cmdSize);
                MessageArrived(b);
            }
            var stream = client.GetStream();

            if (client.Connected && stream.CanRead)
            {
                client.GetStream().BeginRead(readbuffer, 0, readbuffer.Length, OnReadEnd, null);
            }
        }
예제 #2
0
        protected override void MessageArrived(byte[] message)
        {
            //DLogger.WriteLineToScreen("Message Length: " + message.Length);
            if (message.Length >= 3)
            {
                char   c1  = Convert.ToChar(message[1]);
                char   c2  = Convert.ToChar(message[2]);
                string str = String.Format("1,2,3 bytes {0}, {1}, {2}", message[0], c1, c2);
                DLogger.WriteLineToScreen(str);
            }
            else
            {
                DLogger.WriteLineToScreen("Message error");
            }

            var type = NetworkCryptor.GetTypeOfMessage(message);


            if (type == NetworkCryptor.NetworkMessageType.InstanceCharacter)
            {
                var pi = NetworkCryptor.PlayerInstanceInfo.fromBytes(message);
                DLogger.WriteLineToScreen("Instance id: " + pi.id);
                gameAdmin.AddMessageToQueue(pi);
            }
            if (type == NetworkCryptor.NetworkMessageType.MoveTo)
            {
                var pi = NetworkCryptor.PlayerMoveToInfo.fromBytes(message);
                gameAdmin.AddMessageToQueue(pi);
            }
            if (type == NetworkCryptor.NetworkMessageType.DestroyInstance)
            {
                var pi = NetworkCryptor.PlayerDestroyInfo.fromBytes(message);
                gameAdmin.AddMessageToQueue(pi);
            }
        }
예제 #3
0
 protected override void BeforeSending()
 {
     if (!Connected)
     {
         DLogger.WriteLineToScreen("Client not connected, send fail");
     }
 }
예제 #4
0
파일: GServer.cs 프로젝트: Noortvel/MyRpg
        public void RemoveClient(GTcpClientOnServer client)
        {
            clients.Remove(client);
            var message = new NetworkCryptor.PlayerDestroyInfo(client.idNet);

            SendMessageToClients(message.toBytes());
            DLogger.WriteLineToScreen("GServer: Removed client: " + client.idNet);
            //client.CloseConnection();
        }
예제 #5
0
 public void CloseConnection()
 {
     if (client.Connected)
     {
         DLogger.WriteLineToScreen("GTcpClient: Connection closing");
         client.GetStream().Flush();
         client.GetStream().Close();
         client.Close();
         ConnectionAfterClosed();
     }
     else
     {
         DLogger.WriteLineToScreen("GTcpClient: Called Connection closing, but it already closed");
     }
 }
예제 #6
0
파일: GClient.cs 프로젝트: Noortvel/MyRpg
 private void Initialize(IPAddress ip, int port)
 {
     tcpClient = new TcpClient();
     try
     {
         tcpClient.Connect(ip, port);
         isConnected = true;
     }
     catch (SocketException e)
     {
         isConnected = false;
         DLogger.WriteLineToScreen("GClient tcp connect error: " + e);
     }
     gConnectedClient = new GTcpClientOnClient(tcpClient, gameAdmin);
 }
예제 #7
0
 private void ExecuteCommand(NetworkCryptor.NetCommand cmd)
 {
     if (cmd is NetworkCryptor.PlayerInstanceInfo)
     {
         var inf       = (NetworkCryptor.PlayerInstanceInfo)cmd;
         var character = Instantiate(characterPrefab, inf.position, transform.rotation);
         character.netId = inf.id;
         if (connectedCharcters.Count == 0)
         {
             var controller = gameObject.AddComponent <PlayerController>();
             var cam        = ccamera.transform.GetChild(0).GetComponent <Camera>();
             controller.Initialize(character, cam, gClient.connectedClient);
         }
         connectedCharcters.Add(character);
     }
     if (cmd is NetworkCryptor.PlayerMoveToInfo)
     {
         var inf = (NetworkCryptor.PlayerMoveToInfo)cmd;
         DLogger.WriteLineToScreen("Game Admin Move to:");
         Character character = null;
         if (inf != null)
         {
             character = connectedCharcters.Find(fc => fc.netId == inf.id);
             DLogger.WriteLineToScreen("TO: " + " id: " + inf.id + " dest " + inf.destonation);
         }
         if (character != null)
         {
             character.MoveTo(inf.destonation);
         }
     }
     if (cmd is NetworkCryptor.PlayerDestroyInfo)
     {
         var       inf       = (NetworkCryptor.PlayerDestroyInfo)cmd;
         Character character = null;
         if (inf != null)
         {
             character = connectedCharcters.Find(fc => fc.netId == inf.id);
             if (character != null)
             {
                 connectedCharcters.Remove(character);
                 Destroy(character.gameObject);
             }
         }
     }
 }
예제 #8
0
파일: GServer.cs 프로젝트: Noortvel/MyRpg
        private void OnConnect(IAsyncResult ar)
        {
            TcpClient client = listener.EndAcceptTcpClient(ar);

            DLogger.WriteLineToScreen("Client Connected, id: " + playerCount);
            DLogger.WriteLineToScreen("Connected clients: " + clients.Count);

            var gclient = new GTcpClientOnServer(client, this, playerCount);

            playerCount++;

            ReplicateCharacterToClients(gclient);



            clients.Add(gclient);
            listener.BeginAcceptTcpClient(OnConnect, null);
        }
예제 #9
0
        protected override void MessageArrived(byte[] message)
        {
            if (message.Length >= 3)
            {
                char   c1  = Convert.ToChar(message[1]);
                char   c2  = Convert.ToChar(message[2]);
                string str = String.Format("1,2,3 bytes {0}, {1}, {2}", message[0], c1, c2);
                DLogger.WriteLineToScreen(str);
            }
            else
            {
                DLogger.WriteLineToScreen("Message error");
            }
            var type = NetworkCryptor.GetTypeOfMessage(message);

            if (type == NetworkCryptor.NetworkMessageType.MoveTo)
            {
                server.SendMessageToClientNotOne(message, this);
            }
        }
예제 #10
0
파일: GServer.cs 프로젝트: Noortvel/MyRpg
 private void ReplicateCharacterToClients(GTcpClientOnServer gclient)
 {
     DLogger.WriteLineToScreen("Replicate client start");
     byte[] cur = (new NetworkCryptor.PlayerInstanceInfo(gclient.idNet, gameAdmin.SpawnPoint.position)).toBytes();
     gclient.SendAsync(cur);
     //Отсылка всем существующим клиентам player instance
     for (int i = 0; i < clients.Count; i++)
     {
         DLogger.WriteLineToScreen("new to clients: " + gclient.idNet);
         clients[i].SendAsync(cur);
     }
     //Отсылка новому клиенту информации о всех подключенных клиентах
     for (int i = 0; i < clients.Count; i++)
     {
         byte[] mes = new NetworkCryptor.PlayerInstanceInfo(
             clients[i].idNet, gameAdmin.SpawnPoint.position).toBytes();
         DLogger.WriteLineToScreen("clients to new: " + clients[i].idNet);
         gclient.SendAsync(mes);
     }
     DLogger.WriteLineToScreen("Replicate client end");
 }
예제 #11
0
        /// <summary>
        /// If it is not connected, or connected lose, return false,
        /// else message sended
        /// </summary>
        /// <param name="message"></param>
        public bool SendAsync(byte[] message)
        {
            BeforeSending();
            var stream = client.GetStream();

            if (client.Connected && stream.CanWrite)
            {
                try
                {
                    client.GetStream().WriteAsync(message, 0, message.Length);
                    return(true);
                }
                catch (Exception e)
                {
                    DLogger.WriteLineToScreen("GTcpClient: Send Async Fail, ex: " + e);
                }
                return(false);
            }
            else
            {
                DLogger.WriteLineToScreen("Cant send, stream cant write");
            }
            return(false);
        }
예제 #12
0
 protected override void ConnectionAfterClosed()
 {
     DLogger.WriteLineToScreen("GClientOnClient disconnected");
 }
예제 #13
0
 protected override void ConnectionAfterClosed()
 {
     DLogger.WriteLineToScreen("GTcpClientOnServer: Disconnected, removing it from server list, id: " + idNet);
     server.RemoveClient(this);
 }