Esempio n. 1
0
 public DataPacket[] GrabPackets()
 {
     DataPacket[] packets = new DataPacket[this.packets.Count];
     this.packets.CopyTo(packets);
     this.packets.Clear();
     return packets;
 }
Esempio n. 2
0
 public void Publish(DataPacket packet)
 {
     if(packet != null) {
         foreach(SocketClient client in this.subscribers.Values) {
             client.Send(packet);
         }
     }
 }
Esempio n. 3
0
 public void Process(SocketClient client, DataPacket packet)
 {
     if(client.User != null) {
         UserInput input = new UserInput("shot");
         input["nX"] = packet["nX"];
         input["nY"] = packet["nY"];
         client.User.AddInput(input);
     }
 }
Esempio n. 4
0
 public void Process(SocketClient client, DataPacket packet)
 {
     if (client.User != null) {
         UserInput input = new UserInput("charMove");
         input["d"] = ((FieldData)packet["d"]).Value;
         client.User.AddInput(input);
         //client.User.Char.Target =
     }
     /*if(client.User != null) {
         client.User.Name = packet["n"].ToString();
     }*/
 }
Esempio n. 5
0
 public void Process(SocketClient client, DataPacket packet)
 {
     if(client.User != null && client.User.Char != null) {
         if(GameObject.Time - client.User.lastEmotion > User.emotionDelay) {
             client.User.lastEmotion = GameObject.Time;
             {
                 DataPacket pckt = PacketFactory.Make("emotionAdd");
                 if(pckt != null) {
                     pckt["id"] = client.User.Char.ID;
                     pckt["t"] = int.Parse(packet["t"].ToString());
                     SocketServer.inst.Publish(pckt);
                 }
             }
         }
     }
 }
Esempio n. 6
0
        public DataPacket Read(byte[] data)
        {
            string dataJSON = Encoding.UTF8.GetString(data).Replace(@"\""", "&quot;").Replace(@"<", "&lt;").Replace(@"<", "&gt;");

            string packetName = this.GetName(dataJSON);

            if(!string.IsNullOrEmpty(packetName)) {
                PacketType packet = PacketFactory.Get(packetName);

                if(packet != null) {
                    DataPacket dataPacket = new DataPacket(packet);

                    foreach(PacketField field in packet.Fields) {
                        string value = string.Empty;
                        if(field.DataType != null) {
                            IPacketDataTypeParser parser = field.DataType.GetParser(this.name);
                            if(parser != null) {
                                switch(field.DataType.Name) {
                                    case "bool":
                                        value = this.GetValueBool(dataJSON, field.Name);
                                        break;
                                    case "byte":
                                    case "sbyte":
                                    case "short":
                                    case "ushort":
                                    case "int":
                                    case "uint":
                                    case "long":
                                    case "ulong":
                                    case "float":
                                    case "double":
                                    case "timespan":
                                        value = this.GetValueNumber(dataJSON, field.Name);
                                        break;
                                    case "string":
                                        value = this.GetValueString(dataJSON, field.Name);
                                        break;
                                    default:
                                        Log.Add("unknown data type: " + field.DataType.Name);
                                        break;
                                }
                                if(!string.IsNullOrEmpty(value)) {
                                    dataPacket[field] = field.DataType.GetParser(this.name).Read(value);
                                }
                            } else {
                                Log.Add("no data type (" + field.DataType.Name + ") parser for " + this.name + " protocol");
                            }
                        } else {
                            Log.Add("unknown data type, field: " + field.Name);
                        }
                    }
                    return dataPacket;
                } else {
                    Log.Add("unknown packet: " + packetName);
                    return null;
                }
            } else {
                Log.Add("wrong packet (no type)");
                return null;
            }
        }
Esempio n. 7
0
        public byte[] Write(DataPacket data)
        {
            StringBuilder dataJSON = new StringBuilder(@"{""type"":""");

            dataJSON.Append(data.Name);

            dataJSON.Append(@"""");

            foreach(KeyValuePair<PacketField, FieldData> pair in data.Enumerator) {
                if(pair.Key.DataType != null) {
                    IPacketDataTypeParser parser = pair.Key.DataType.GetParser(this.name);
                    if(parser != null) {
                        if(pair.Value.Value != null) {
                            dataJSON.Append(@",""");
                            dataJSON.Append(pair.Key.Name);
                            dataJSON.Append(@""":");
                            dataJSON.Append(parser.Write(pair.Value.Value));
                        }
                    } else {
                        Log.Add("no data type (" + pair.Key.DataType.Name + ") parser for " + this.name + " protocol");
                    }
                } else {
                    Log.Add("unknown data type, field " + pair.Key.Name);
                }
            }

            dataJSON.Append(@"}");

            return Encoding.UTF8.GetBytes(dataJSON.ToString());
        }
Esempio n. 8
0
 // Asynchronous publishing of packet using Thread from Pool
 public void PublishAsync(DataPacket packets)
 {
     ThreadPool.QueueUserWorkItem(new WaitCallback(PublishCallback), packets);
 }
Esempio n. 9
0
 // Blocking publishing of packet
 public void Publish(DataPacket packet)
 {
     this.publisher.Publish(packet);
 }
Esempio n. 10
0
        // Blocking sending of data
        // It serializes packet into binary based on selected Data Protocol
        public override void Send(DataPacket packet)
        {
            try {
                if (packet != null) {
                    byte[] data = DataProtocol.Get("json").Write(packet);

                    byte[] binary = this.transportProtocol.Send(data);

                    SocketError error = SocketError.Fault;
                    int send = 0;

                    bool stop = false;
                    //lock(this.locker) {
                        if(this.socket != null && this.socket.Connected) {
                            send = this.socket.Send(binary, 0, binary.Length, SocketFlags.None, out error);
                        } else {
                            stop = true;
                        }
                    //}
                    if(stop || error != SocketError.Success || send != binary.Length) {
                        Log.Add("client[" + this.id + "] Send, stop");
                        this.Stop();
                    }

                    packet.FinishTracker();
                }
            } catch(SocketException exception) {
                Log.Add("client[" + this.id + "] Send exception: " + exception.ToString());
            }
        }
Esempio n. 11
0
 public abstract void SendAsync(DataPacket packet);
Esempio n. 12
0
 public void AddPacket(DataPacket packet)
 {
     if (packet != null) {
         this.packets.Add(packet);
     }
 }
Esempio n. 13
0
 public static DataPacket Make(string name, params object[] list)
 {
     PacketType packetType = Get(name);
     if(packetType != null) {
         DataPacket packet = new DataPacket(packetType);
         if(list != null && list.Length > 0) {
             for(int i = 0; i < list.Length / 2; ++i) {
                 packet[(string)list[i * 2]] = list[i * 2 + 1];
             }
         }
         return packet;
     } else {
         return null;
     }
 }
Esempio n. 14
0
 public void Trigger(SocketClient client, DataPacket packet)
 {
     foreach(IPacketEvent evnt in this.events) {
         evnt.Process(client, packet);
     }
 }
Esempio n. 15
0
 public void Process(SocketClient client, DataPacket packet)
 {
     if(client.User != null) {
         client.User.Name = packet["n"].ToString();
     }
 }
Esempio n. 16
0
 // Send data using thread from pool
 public override void SendAsync(DataPacket packet)
 {
     if (packet != null) {
         ThreadPool.QueueUserWorkItem(new WaitCallback(SendAsync), packet);
     }
 }
Esempio n. 17
0
 public abstract void Send(DataPacket packet);