Exemplo n.º 1
0
 public DataPacket[] GrabPackets()
 {
     DataPacket[] packets = new DataPacket[this.packets.Count];
     this.packets.CopyTo(packets);
     this.packets.Clear();
     return packets;
 }
Exemplo n.º 2
0
 public void Publish(DataPacket packet)
 {
     if(packet != null) {
         foreach(SocketClient client in this.subscribers.Values) {
             client.Send(packet);
         }
     }
 }
Exemplo 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);
     }
 }
Exemplo 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();
     }*/
 }
Exemplo 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);
                 }
             }
         }
     }
 }
Exemplo 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;
            }
        }
Exemplo 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());
        }
Exemplo n.º 8
0
 // Asynchronous publishing of packet using Thread from Pool
 public void PublishAsync(DataPacket packets)
 {
     ThreadPool.QueueUserWorkItem(new WaitCallback(PublishCallback), packets);
 }
Exemplo n.º 9
0
 // Blocking publishing of packet
 public void Publish(DataPacket packet)
 {
     this.publisher.Publish(packet);
 }
Exemplo 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());
            }
        }
Exemplo n.º 11
0
 public abstract void SendAsync(DataPacket packet);
Exemplo n.º 12
0
 public void AddPacket(DataPacket packet)
 {
     if (packet != null) {
         this.packets.Add(packet);
     }
 }
Exemplo 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;
     }
 }
Exemplo n.º 14
0
 public void Trigger(SocketClient client, DataPacket packet)
 {
     foreach(IPacketEvent evnt in this.events) {
         evnt.Process(client, packet);
     }
 }
Exemplo n.º 15
0
 public void Process(SocketClient client, DataPacket packet)
 {
     if(client.User != null) {
         client.User.Name = packet["n"].ToString();
     }
 }
Exemplo n.º 16
0
 // Send data using thread from pool
 public override void SendAsync(DataPacket packet)
 {
     if (packet != null) {
         ThreadPool.QueueUserWorkItem(new WaitCallback(SendAsync), packet);
     }
 }
Exemplo n.º 17
0
 public abstract void Send(DataPacket packet);