示例#1
0
        public void ProcessReceivedMessage(Connection connection, ref byte messageCode, ref byte[] data)
        {
            switch (messageCode)
            {
            case MessageCodes.Chat:
                string chatMessage = Encoding.ASCII.GetString(data, 1, data[0]);
                ChatMessage(connection, chatMessage);
                break;

            case MessageCodes.Disconnecting:
                DisconnectingNotification(connection);
                break;

            case MessageCodes.FileDataExchangeAdd:
                byte   addExchangeType = 0;
                byte[] addHash         = null;
                byte[] addFileData     = null;
                FileDataExchangeUtils.ParseAddFileMessage(ref data, out addExchangeType, out addHash, out addFileData);

                AddFile(connection, addExchangeType, addHash, addFileData);
                break;

            case MessageCodes.FileDataExchangeRequest:
                byte   requestExchangeType = 0;
                byte[] requestHash         = null;
                FileDataExchangeUtils.ParseCheckFileExistRequest(ref data, out requestExchangeType, out requestHash);

                CheckFileExistRequest(connection, requestExchangeType, requestHash);
                break;

            case MessageCodes.FileDataExchangeResponse:
                byte   responseExchangeType = 0;
                byte[] responseHash         = null;
                bool   responseExists       = false;
                FileDataExchangeUtils.ParseCheckFileExistResponse(ref data, out responseExchangeType, out responseHash, out responseExists);

                CheckFileExistResponse(connection, responseExchangeType, responseHash, responseExists);
                break;

            case MessageCodes.FileDataRequest:
                byte   fileExchangeType = 0;
                byte[] fileHash         = null;

                FileDataExchangeUtils.ParseFileRequest(ref data, out fileExchangeType, out fileHash);

                RequestFile(connection, fileExchangeType, fileHash);
                break;

            case MessageCodes.GeneralNo:
                break;

            case MessageCodes.GeneralYes:
                break;

            case MessageCodes.Login:
                int offset = 0;

                int nameLength = data[offset];
                offset++;
                string name = Encoding.ASCII.GetString(data, offset, nameLength);
                offset += nameLength;

                int passwordLength = data[offset];
                offset++;
                string password = Encoding.ASCII.GetString(data, offset, passwordLength);
                offset += passwordLength;

                int versionLength = data[offset];
                offset++;
                string version = Encoding.ASCII.GetString(data, offset, versionLength);
                offset += versionLength;

                byte[] skinHash = new byte[16];
                for (int i = 0; i < 16; i++)
                {
                    skinHash[i] = data[offset + i];
                }

                LoginRequest(connection, name, password, version, skinHash);
                break;

            case MessageCodes.Moved:
                Point3D movedLocation = new Point3D();
                movedLocation.InitializeFromByteArray(ref data, 0);
                float rotationLeft = BitConverter.ToSingle(data, Point3D.SerializedSize);
                float rotationUp   = BitConverter.ToSingle(data, Point3D.SerializedSize + 4);

                PlayerMovedNotification(connection, movedLocation, rotationLeft, rotationUp);
                break;

            case MessageCodes.Moving:
                break;

            //case MessageCodes.MultipartCompressed:
            //   connection.ProcessMultipartMessage(ref data);
            //   break;
            case MessageCodes.PlaceOrRemoveCube:
                bool    place         = (data[0] > 0);
                Point3D placeLocation = new Point3D();
                placeLocation.InitializeFromByteArray(ref data, 1);
                float  placeRotationLeft = BitConverter.ToSingle(data, Point3D.SerializedSize + 1);
                float  placeRotationUp   = BitConverter.ToSingle(data, Point3D.SerializedSize + 5);
                ushort placeMaterialId   = BitConverter.ToUInt16(data, Point3D.SerializedSize + 9);

                PlaceOrRemoveCubeNotification(connection, place, placeLocation, placeRotationLeft, placeRotationUp, placeMaterialId);
                break;

            case MessageCodes.Terrain:
                int     terrainMessageId = BitConverter.ToInt32(data, 0);
                Point3D terrainLocation  = new Point3D();
                terrainLocation.InitializeFromByteArray(ref data, 4);
                float terrainRotationLeft = BitConverter.ToSingle(data, Point3D.SerializedSize + 4);
                float terrainRotationUp   = BitConverter.ToSingle(data, Point3D.SerializedSize + 8);

                TerrainDataRequest(connection, terrainMessageId, terrainLocation, terrainRotationLeft, terrainRotationUp);
                break;
            }
        }
            public void ProcessReceivedMessage(Connection connection, ref byte messageCode, ref byte[] data)
            {
                switch (messageCode)
                {
                case MessageCodes.Chat:
                    string chatMessage = Encoding.ASCII.GetString(data, 1, data[0]);
                    ChatMessage(connection, chatMessage);
                    break;

                case MessageCodes.Disconnecting:
                    string reason = Encoding.ASCII.GetString(data, 1, data[0]);

                    DisconnectingNotification(connection, reason);
                    break;

                case MessageCodes.EntityCreateUpdate:
                    Entity entity = Entity.CreateFromByteArray(data);

                    EntityCreateOrUpdateNotification(connection, entity);
                    break;

                case MessageCodes.EntityDelete:
                    ushort deleteEntityId = BitConverter.ToUInt16(data, 0);

                    EntityDeleteNotification(connection, deleteEntityId);
                    break;

                case MessageCodes.FileDataExchangeAdd:
                    byte   addExchangeType = 0;
                    byte[] addHash         = null;
                    byte[] addFileData     = null;
                    FileDataExchangeUtils.ParseAddFileMessage(ref data, out addExchangeType, out addHash, out addFileData);

                    AddFile(connection, addExchangeType, addHash, addFileData);
                    break;

                case MessageCodes.FileDataExchangeRequest:
                    byte   requestExchangeType = 0;
                    byte[] requestHash         = null;
                    FileDataExchangeUtils.ParseCheckFileExistRequest(ref data, out requestExchangeType, out requestHash);

                    CheckFileExistRequest(connection, requestExchangeType, requestHash);
                    break;

                case MessageCodes.FileDataExchangeResponse:
                    byte   responseExchangeType = 0;
                    byte[] responseHash         = null;
                    bool   responseExists       = false;
                    FileDataExchangeUtils.ParseCheckFileExistResponse(ref data, out responseExchangeType, out responseHash, out responseExists);

                    CheckFileExistResponse(connection, responseExchangeType, responseHash, responseExists);
                    break;

                case MessageCodes.FileDataRequest:
                    byte   fileExchangeType = 0;
                    byte[] fileHash         = null;

                    FileDataExchangeUtils.ParseFileRequest(ref data, out fileExchangeType, out fileHash);

                    RequestFile(connection, fileExchangeType, fileHash);
                    break;

                case MessageCodes.GeneralNo:
                    break;

                case MessageCodes.GeneralYes:
                    break;

                case MessageCodes.Login:
                    byte   result               = data[0];
                    ushort entityId             = BitConverter.ToUInt16(data, 1);
                    byte   additionalInfoLength = data[3];
                    string additionalInfo       = null;
                    if (additionalInfoLength > 0)
                    {
                        additionalInfo = Encoding.ASCII.GetString(data, 4, additionalInfoLength);
                    }

                    LoginResponse(connection, result, entityId, additionalInfo);
                    break;

                case MessageCodes.Moved:
                    ushort  movedEntityId = BitConverter.ToUInt16(data, 0);
                    Point3D movedLocation = new Point3D();
                    movedLocation.InitializeFromByteArray(ref data, 2);
                    float movedRotationLeft = BitConverter.ToSingle(data, Point3D.SerializedSize + 2);
                    float movedRotationUp   = BitConverter.ToSingle(data, Point3D.SerializedSize + 6);
                    int   movedTime         = BitConverter.ToInt32(data, Point3D.SerializedSize + 10);

                    EntityMovedNotification(connection, movedEntityId, movedLocation, movedRotationLeft, movedRotationUp, movedTime);
                    break;

                case MessageCodes.Moving:
                    break;

                //case MessageCodes.MultipartCompressed:
                //   connection.ProcessMultipartMessage(ref data);
                //   break;
                case MessageCodes.PlaceOrRemoveCube:
                    bool   place      = (data[0] > 0);
                    int    key        = BitConverter.ToInt32(data, 1);
                    int    x          = BitConverter.ToInt32(data, 5);
                    int    y          = BitConverter.ToInt32(data, 9);
                    int    z          = BitConverter.ToInt32(data, 13);
                    ushort materialId = BitConverter.ToUInt16(data, 17);
                    uint   version    = BitConverter.ToUInt32(data, 19);

                    lock (_TerrainUpdateLock)
                    {
                        ClientSegment segment = ClientWorldManager.Instance.GetSegment(key);
                        if (segment != null)
                        {
                            PlaceOrRemoveCubeNotification(connection, place, segment, x, y, z, materialId, version);
                        }
                        else
                        {
                            if (!_DelayedSegmentUpdates.ContainsKey(key))
                            {
                                _DelayedSegmentUpdates.Add(key, new List <DelayedSegmentUpdate>());
                            }
                            _DelayedSegmentUpdates[key].Add(new DelayedSegmentUpdate()
                            {
                                MaterialID = materialId,
                                Place      = place,
                                SegmentKey = key,
                                Version    = version,
                                X          = x,
                                Y          = y,
                                Z          = z
                            });
                        }
                    }
                    break;

                case MessageCodes.Terrain:
                    int  terrainMessageId = BitConverter.ToInt32(data, 0);
                    byte current          = data[4];
                    byte total            = data[5];
                    int  areaCount        = data[6];

                    //Console.WriteLine("Received terrain data: terrainMessageId = {0}, current = {1}, total = {2}, areaCount = {3}", terrainMessageId, current, total, areaCount);

                    // clean areas prior to deserialization
                    lock (_TerrainUpdateLock)
                    {
                        ClientWorldManager.Instance.CleanAreas(ref terrainMessageId);
                    }

                    // deserialize the terrain
                    List <ClientArea> areas = new List <ClientArea>(areaCount);
                    int offset = 7;
                    for (int i = 0; i < areaCount; i++)
                    {
                        ClientArea area = new ClientArea();
                        area.InitializeFromByteArray <ClientSegment, ClientBox>(data, ref offset);
                        areas.Add(area);
                    }

                    TerrainDataResponse(connection, terrainMessageId, areas, current, total);
                    break;
                }
            }