예제 #1
0
 public void Init()
 {
     ServerSocket.Getinstance().AddHandler(typeof(CSLogin), this.CSLogin);
     ServerSocket.Getinstance().AddHandler(typeof(CSCreatePlayer), this.CSCreatePlayer);
     ServerSocket.Getinstance().AddHandler(typeof(CSLoadPlayer), this.CSLoadPlayer);
     LogTool.Tip("Server Init");
 }
예제 #2
0
    public static void SavePlayer(int player_id, PlayerStruct player_struct)
    {
        //FileStream fs = File.Create(PathTool.GetPlayerSavePath(player_id));
        //BinaryFormatter bf = new BinaryFormatter();
        //bf.Serialize(fs, player_struct);
        //file.Close();


        //string json = SaveAsJSON(player_struct);
        //LogTool.Tip("player_id:" + player_id + " SavePlayer JSON: " + json);

        //FileStream fs = File.Create(PathTool.GetPlayerSavePath(player_id));
        //byte[] bytes = System.Text.Encoding.UTF8.GetBytes(json);
        //fs.Write(bytes, 0, bytes.Length);
        //fs.Close();

        LogTool.Tip(PathTool.GetPlayerSavePath(player_id));
        Common.Protobuf.PlayerStruct mPlayerStruct = new Common.Protobuf.PlayerStruct();

        mPlayerStruct.ItemData.Grid = player_struct.item_data.grid;
        foreach (KeyValuePair <string, int> pair in player_struct.item_data.item_list)
        {
            mPlayerStruct.ItemData.ItemList[pair.Key] = pair.Value;
        }
        FileStream file = File.Create(PathTool.GetPlayerSavePath(player_id));

        mPlayerStruct.WriteTo(file);
    }
예제 #3
0
 public static void LogProtocolMap()
 {
     foreach (KeyValuePair <Type, short> pair in ProtocolMap)
     {
         LogTool.Tip("Key:" + pair.Key + " Value:" + pair.Value);
     }
 }
예제 #4
0
    public void Init()
    {
        this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        this.socket.Bind(new IPEndPoint(IPAddress.Parse(Const.IpStr), Const.Port));
        this.socket.Listen(10);
        LogTool.Tip("服务端Socket创建成功:" + this.socket.LocalEndPoint.ToString());

        Thread thread = new Thread(ConnectListen);

        thread.Start();
    }
예제 #5
0
    public static string InitPath(string file_path)
    {
        string path = file_path.Substring(0, file_path.LastIndexOf("/"));

        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
            LogTool.Tip("Create Path" + path);
        }
        return(file_path);
    }
예제 #6
0
    private void ConnectListen()
    {
        while (true)
        {
            Socket client_socket = socket.Accept();
            client_socket.ReceiveBufferSize = Const.BUFFSIZE;
            client_socket.SendBufferSize    = Const.BUFFSIZE;
            this.temp_socket_map.Add(this.CreateConnectId(), client_socket);
            LogTool.Tip("客户端Socket连接成功:" + client_socket.RemoteEndPoint.ToString());

            Thread thread = new Thread(RecieveMessage);
            thread.Start(client_socket);
        }
    }
예제 #7
0
    public void Dispatch(IMessage protocol, int connect_id)
    {
        Type type = protocol.GetType();

        if (!handle_map.ContainsKey(type))
        {
            LogTool.Tip("协议类型没有注册处理函数:" + type.ToString());
            return;
        }

        Handler handler = handle_map[type];

        handler(protocol, connect_id);
    }
예제 #8
0
    public static IMessage Decode(byte[] msg_bytes)
    {
        ByteBuffer buffer   = new ByteBuffer(msg_bytes);
        short      msg_code = buffer.ReadShort();

        byte[] msg = buffer.ReadBytes((int)buffer.RemainingBytes());

        if (!ProtocolParser.ContainsKey(msg_code))
        {
            LogTool.Tip("未知类型的协议号:" + msg_code);
            return(null);
        }
        MessageParser parser = ProtocolParser[msg_code];

        return(parser.ParseFrom(msg));
    }
예제 #9
0
    public static byte[] Encode(IMessage protocol)
    {
        Type type = protocol.GetType();

        if (!ProtocolMap.ContainsKey(type))
        {
            LogTool.Tip("协议类型没有赋予协议号:" + type.ToString());
            return(null);
        }
        short msg_code = ProtocolMap[type];

        ByteBuffer buffer = new ByteBuffer();

        buffer.WriteShort(msg_code);
        buffer.WriteBytes(protocol.ToByteArray());
        return(buffer.ToBytes());
    }
예제 #10
0
    public void SendMessage(IMessage protocol)
    {
        if (!this.IsActive())
        {
            ServerSocket.Getinstance().DispatchProtocol(protocol, Const.LOCAL_CONNECT_ID);
        }
        else
        {
            byte[]     msg_bytes = Protocol.Encode(protocol);
            ByteBuffer buffer    = new ByteBuffer();
            buffer.WriteInt(msg_bytes.Length);
            buffer.WriteBytes(msg_bytes);

            byte[] data = buffer.ToBytes();
            LogTool.Tip("客户端发送消息类型:" + protocol.GetType() + " 长度为:" + data.Length);
            this.socket.Send(data);
        }
    }
예제 #11
0
    public void SendMessage(IMessage protocol, int connect_id)
    {
        if (connect_id == Const.LOCAL_CONNECT_ID)
        {
            ClientSocket.Getinstance().DispatchProtocol(protocol);
        }
        else
        {
            byte[]     msg_bytes = Protocol.Encode(protocol);
            ByteBuffer buffer    = new ByteBuffer();
            buffer.WriteInt(msg_bytes.Length);
            buffer.WriteBytes(msg_bytes);

            Socket socket = this.GetSocket(connect_id);
            if (socket == null)
            {
                LogTool.Tip("玩家Socket不存在:" + connect_id);
                return;
            }
            socket.Send(buffer.ToBytes());
        }
    }
예제 #12
0
    private void RecieveMessage()
    {
        while (true)
        {
            try
            {
                byte[] data   = new byte[Const.BUFFSIZE];
                int    length = this.socket.Receive(data);
                LogTool.Tip("客户端接收消息:" + this.socket.RemoteEndPoint.ToString() + "长度为:" + length);

                if (length > 0)
                {
                    byte_cache.AddBytes(data, length);

                    while (true)
                    {
                        byte[] msg_bytes = byte_cache.DivideMessage();
                        if (msg_bytes == null)
                        {
                            break;
                        }

                        IMessage protocol = Protocol.Decode(msg_bytes);
                        if (protocol != null)
                        {
                            this.DispatchProtocol(protocol);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                LogTool.Tip(e.Message);
                this.socket.Shutdown(SocketShutdown.Both);
                this.socket.Close();
                break;
            }
        }
    }
예제 #13
0
 public void Init()
 {
     ClientSocket.Getinstance().AddHandler(typeof(SCLogin), this.SCLogin);
     LogTool.Tip("Client Init");
 }
예제 #14
0
 public void Release()
 {
     ClientSocket.Getinstance().Release();
     LogTool.Tip("Client Release");
 }
예제 #15
0
 public void Release()
 {
     ServerSocket.Getinstance().Release();
     LogTool.Tip("Server Release");
 }
예제 #16
0
 public void InitSocket()
 {
     ServerSocket.Getinstance().Init();
     LogTool.Tip("ServerSocket Init");
 }
예제 #17
0
    public void SCLogin(object data, int connect_id)
    {
        SCLogin protocol = data as SCLogin;

        LogTool.Tip("Login RetCode:" + protocol.Code);
    }
예제 #18
0
    private void RecieveMessage(object client)
    {
        Socket client_socket = (Socket)client;

        while (true)
        {
            try
            {
                byte[] data   = new byte[Const.BUFFSIZE];
                int    length = client_socket.Receive(data);
                LogTool.Tip("服务端接收消息:" + client_socket.RemoteEndPoint.ToString() + "长度为:" + length);

                if (length > 0)
                {
                    if (!cache_map.ContainsKey(client_socket))
                    {
                        cache_map[client_socket] = new ByteCache();
                    }
                    ByteCache cache = cache_map[client_socket];
                    cache.AddBytes(data, length);

                    while (true)
                    {
                        byte[] msg_bytes = cache.DivideMessage();
                        if (msg_bytes == null)
                        {
                            break;
                        }

                        IMessage protocol = Protocol.Decode(msg_bytes);
                        if (protocol != null)
                        {
                            int connect_id;
                            if (this.socket_map.HaveValue(client_socket))
                            {
                                connect_id = this.socket_map.GetKey(client_socket);
                            }
                            else if (this.temp_socket_map.HaveValue(client_socket))
                            {
                                connect_id = this.temp_socket_map.GetKey(client_socket);
                            }
                            else
                            {
                                Exception e = new Exception("Socket对应的连接ID不存在!!!");
                                throw e;
                            }

                            this.DispatchProtocol(protocol, connect_id);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                LogTool.Tip(e.StackTrace);
                client_socket.Shutdown(SocketShutdown.Both);
                client_socket.Close();
                break;
            }
        }
    }
예제 #19
0
 public void InitSocket()
 {
     ClientSocket.Getinstance().Init();
     LogTool.Tip("ClientSocket Init");
 }