Esempio n. 1
0
        private static void AcceptCallback(IAsyncResult ar)
        {
            Socket s = (Socket)ar.AsyncState;

            if (s == null)
            {
                return;
            }

            try
            {
                Socket  connClient = s.EndAccept(ar);
                XClient client     = new XClient();
                client.s  = connClient;
                client.id = connClient.GetHashCode();
                clients.Add(client);

                Console.WriteLine("[Python] Client connected!");
                client.s.BeginReceive(client.buffer, 0, XClient.buffer_size, SocketFlags.None, new AsyncCallback(ReceiveCallback), client);
            }
            catch
            {
                Console.WriteLine("[Python] Accept client error!");
                return;
            }
            finally
            {
                s.BeginAccept(new AsyncCallback(AcceptCallback), s);
            }
        }
Esempio n. 2
0
 private static void SendString(XClient client, string data)
 {
     try
     {
         byte[] bytes = Encoding.UTF8.GetBytes(data);
         client.s.BeginSend(bytes, 0, bytes.Length, SocketFlags.None, new AsyncCallback(SendCallback), client.s);
     }
     catch
     {
         return;
     }
 }
Esempio n. 3
0
        private static void ProcessData(XClient client, byte[] data)
        {
            try
            {
                string raw_message = Encoding.UTF8.GetString(data);
                string message     = string.Join(",", data);
                Console.WriteLine("[Python] Received: " + raw_message);

                using (MemoryStream ms = new MemoryStream(data))
                {
                    using (BinaryReader br = new BinaryReader(ms))
                    {
                        char   packet_type = br.ReadChar();
                        byte[] data_body   = br.ReadBytes(data.Length - 1);
                        string body        = Encoding.UTF8.GetString(data_body);

                        try
                        {
                            switch (packet_type)
                            {
                            case 'a':
                                string[] splited = body.Split('|');
                                string   target  = splited[0];
                                string   msg     = splited[1];
                                Console.WriteLine("[" + target + "] " + msg);
                                SendData.SendWhispper(Conn.ZoneServer.s, target, msg);
                                SendString(client, "received");
                                break;

                            case 'b':
                                SendData.SendGlobal(Conn.ZoneServer.s, body);
                                SendString(client, "received");
                                break;

                            default:
                                break;
                            }
                        }
                        catch
                        {
                            return;
                        }
                    }
                }
            }
            catch
            {
                return;
            }
        }
Esempio n. 4
0
        private static void ReceiveCallback(IAsyncResult ar)
        {
            XClient client = (XClient)ar.AsyncState;

            if (client == null)
            {
                return;
            }
            if (!client.s.Connected)
            {
                return;
            }
            try
            {
                int bytes_received = client.s.EndReceive(ar);
                if (bytes_received > 0)
                {
                    byte[] data = new byte[bytes_received];
                    Array.Copy(client.buffer, data, bytes_received);

                    // process data
                    ProcessData(client, data);

                    client.buffer = new byte[XClient.buffer_size];
                    client.s.BeginReceive(client.buffer, 0, XClient.buffer_size, SocketFlags.None, new AsyncCallback(ReceiveCallback), client);
                }
                else
                {
                    // client disconnected
                    ClientFunctions.DisconnectClient(client.id);
                }
            }
            catch
            {
                // disconnect client
                ClientFunctions.DisconnectClient(client.id);
                return;
            }
        }