Ejemplo n.º 1
0
 public void send(Object o, Protocol.ChannelID channel)
 {
     //Todo move to a static list for each object (performance)
     Protocol.SerialInterface proc = Protocol.SerialInterface.Build(o.GetType());
     Protocol.ByteBuffer      buf  = new Protocol.ByteBuffer(1);
     proc.SerializePacket((uint)channel, o, buf);
     send(buf.GetArray(), buf.Length);
     Log.notify("Sent packet on channel " + ((Protocol.ChannelID)channel).ToString(), this);
 }
        protected override void listen()
        {
            bool readPayload = false;
            int  channel     = 0;
            int  size        = 0;

            byte[] payload;
            int    received = 0;

            byte[] headerBuffer = new byte[8];
            while (doListen || connected)
            {
                try
                {
                    received = 0;
                    //Wait for 8 bytes
                    if (!readPayload)
                    {
                        while (received < 8)
                        {
                            received += clientStream.Read(headerBuffer, received, 8 - received);
                        }
                        channel     = BitConverter.ToInt32(headerBuffer, 0);
                        size        = BitConverter.ToInt32(headerBuffer, 4);
                        readPayload = true;
                    }
                    if (readPayload)
                    {
                        payload  = new byte[size];
                        received = 0;
                        while (received < size)
                        {
                            received += clientStream.Read(payload, received, size - received);
                        }
                        byte[] data = new byte[8 + size];
                        BitConverter.GetBytes(channel).CopyTo(data, 0);
                        BitConverter.GetBytes(size).CopyTo(data, 4);
                        payload.CopyTo(data, 8);
                        buf.Clear();
                        Object o = proc.Deserialize(payload, size);
                        Protocol.ServerInfo serverInfo = (Protocol.ServerInfo)o;
                        serverInfo.gameInfo.gameFlags &= ~((uint)Protocol.gameFlags.SupportRegularFactions);
                        serverInfo.gameInfo.gameFlags |= (uint)Protocol.gameFlags.SupportSpectatorFactions;//useless?
                        serverInfo.hostInfo.hostID     = host.hostID;
                        // serverInfo.gameInfo.worldID = host.worldID;
                        serverInfo.hostInfo.serverBasePort = (ushort)host.serverPort;
                        host.serverInfo = serverInfo;
                        proc.SerializePacket(0, serverInfo, buf);
                        server.sendToClients(buf.GetArray(), buf.Length);
                        readPayload = false;
                    }
                    else if (readPayload && size == 0)
                    {
                        //process a signal?
                        readPayload = false;
                    }
                    Thread.Sleep(1);
                }
                catch (Exception e)
                {
                    Log.error(e.Message, this);
                    host.disconnect();
                }
            }
        }