public override void Clear(StreamBuffer source) { Int32 seqNo = SeqNo; base.Clear(source); SeqNo = seqNo; }
public SessionMethodAsyncResult(Session session) { _session = session; _receivedBuffer = new StreamBuffer(2048); _dispatchBuffer = new StreamBuffer(2048); _responseSelector = new ResponseSelector(_session); }
public static Int32 GetSeqNo(StreamBuffer source) { if (source.BufferSize < HeaderSize) return -1; return source.GetInt32(Packet.HeaderSize); }
public void Broadcast(StreamBuffer packet, User except = null) { foreach (User user in Users) { if (user != except) user.SendPacket(packet.Clone()); } }
public virtual StreamBuffer Clone() { StreamBuffer newStream = new StreamBuffer(this); newStream.ReadBytes = ReadBytes; newStream.WrittenBytes = WrittenBytes; return newStream; }
public StreamBuffer(StreamBuffer source) { ReadBytes = 0; WrittenBytes = 0; Capacity(source.WrittenBytes); Write(source.Buffer, 0, source.WrittenBytes); }
public StreamBuffer(StreamBuffer source, Int32 index, Int32 size) { ReadBytes = 0; WrittenBytes = 0; Capacity(size); Write(source.Buffer, index, size); }
public StreamBuffer(StreamBuffer source) { ReadBytes = 0; WrittenBytes = 0; Capacity(source.BufferSize); Write(source.Buffer); }
public override void SendPacket(StreamBuffer buffer, Action<StreamBuffer> onSent = null) { SecurePacket packet = (SecurePacket)buffer; packet.Encrypt(_aesIV, _aesKey); base.SendPacket(buffer, onSent); Statistics.SentBytes.Add(packet.Size); Statistics.SentCount.Add(1); }
/// <summary> /// 수신버퍼의 크기를 지정하여 Session 객체를 생성합니다. 수신버퍼의 크기는 패킷 하나의 크기 이상으로 설정하는 것이 좋습니다. /// </summary> /// <param name="recvBufferSize">수신버퍼의 크기(Byte)</param> public Session(Int32 recvBufferSize) { Interlocked.Increment(ref NextSessionId); SessionId = NextSessionId; _receivedBuffer = new StreamBuffer(recvBufferSize); _dispatchBuffer = new StreamBuffer(); Clear(); }
public SessionMethodAsyncEvent(Session session) { _session = session; _receivedBuffer = new StreamBuffer(2048); _dispatchBuffer = new StreamBuffer(2048); _saeaRecv = new SocketAsyncEventArgs(); _saeaRecv.Completed += OnComplete_Receive; _responseSelector = new ResponseSelector(_session); }
public static new Boolean IsValidPacket(StreamBuffer buffer, out int packetSize) { if (buffer.WrittenBytes < HeaderSize) { packetSize = 0; return false; } packetSize = buffer.GetUInt16(); return (packetSize > 0 && buffer.WrittenBytes >= packetSize); }
protected override void OnReceive(StreamBuffer buffer) { Packet packet = new Packet(buffer); AegisTask.Run(() => { packet.SkipHeader(); switch (packet.PID) { case 0x01: OnHello(packet); break; case 0x03: OnEcho_Res(packet); break; } }); }
protected override void OnReceive(StreamBuffer buffer) { Counter_ReceiveCount.Add(1); Counter_ReceiveBytes.Add(buffer.WrittenBytes); Packet packet = new Packet(buffer); AegisTask.Run(() => { packet.SkipHeader(); switch (packet.PID) { case 0x02: OnEcho_Req(packet); break; } }); }
private void OnReceived(Session session, StreamBuffer buffer) { PacketRequest packet = new PacketRequest(buffer); packet.SkipHeader(); try { packet.Dispatch(this, "On" + Protocol.GetName(packet.PacketId)); } catch (AegisException e) when (e.ResultCodeNo == AegisResult.BufferUnderflow) { Logger.Write(LogType.Err, 2, "Cannot read more data at PacketId(=0x{0:X}).", packet.PacketId); } catch (Exception e) { Logger.Write(LogType.Err, 2, e.ToString()); } }
/// <summary> /// 수신된 데이터가 유효한 패킷인지 여부를 확인합니다. /// 유효한 패킷으로 판단되면 packetSize에 이 패킷의 정확한 크기를 입력하고 true를 반환해야 합니다. /// </summary> /// <param name="buffer">수신된 데이터가 담긴 버퍼</param> /// <param name="packetSize">유효한 패킷의 크기</param> /// <returns>true를 반환하면 OnReceive함수를 통해 수신된 데이터가 전달됩니다.</returns> protected virtual Boolean IsValidPacket(StreamBuffer buffer, out Int32 packetSize) { if (buffer.WrittenBytes < 4) { packetSize = 0; return false; } // 최초 2바이트를 수신할 패킷의 크기로 처리 packetSize = buffer.GetUInt16(); return (packetSize > 0 && buffer.WrittenBytes >= packetSize); }
public static String GetStringFromUtf16(StreamBuffer source, Int32 readIndex) { Int32 i, stringBytes = 0; for (i = readIndex; i < source.WrittenBytes; i += 2) { if (source.Buffer[i + 0] == 0 && source.Buffer[i + 1] == 0) break; stringBytes += 2; if (readIndex + stringBytes + 2 > source.WrittenBytes) throw new AegisException(AegisResult.BufferUnderflow, "No more readable buffer."); } // String으로 변환할 때 Null terminate를 포함시켜서는 안된다. return Encoding.Unicode.GetString(source.Buffer, readIndex, stringBytes); }
public static Double GetDouble(StreamBuffer source, Int32 readIndex) { if (readIndex + sizeof(Double) > source.WrittenBytes) throw new AegisException(AegisResult.BufferUnderflow, "No more readable buffer."); return BitConverter.ToDouble(source.Buffer, readIndex); }
public static Byte GetByte(StreamBuffer source, Int32 readIndex) { if (readIndex + sizeof(Byte) > source.WrittenBytes) throw new AegisException(AegisResult.BufferUnderflow, "No more readable buffer."); return source.Buffer[readIndex]; }
public void SendPacket(StreamBuffer buffer, Action<StreamBuffer> onSent = null) { Session?.SendPacket(buffer, onSent); }
private void OnReceived(Session session, StreamBuffer buffer) { }
public void Write(StreamBuffer source, Int32 index, Int32 size) { if (index + size > source.WrittenBytes) throw new AegisException(AegisResult.BufferUnderflow, "The source buffer is small then requested."); Int32 copyBytes = size; if (WrittenBytes + copyBytes > BufferSize) Resize(BufferSize + copyBytes); Array.Copy(source.Buffer, index, Buffer, WrittenBytes, copyBytes); WrittenBytes += copyBytes; OnWritten(); }
public void Write(StreamBuffer source, Int32 index) { if (index >= source.WrittenBytes) throw new AegisException(AegisResult.BufferUnderflow, "The argument index(={0}) is larger then source size(={1}).", index, source.WrittenBytes); Int32 copyBytes = source.WrittenBytes - index; if (WrittenBytes + copyBytes > BufferSize) Resize(BufferSize + copyBytes); Array.Copy(source.Buffer, index, Buffer, WrittenBytes, copyBytes); WrittenBytes += copyBytes; OnWritten(); }
public void Write(StreamBuffer source) { Int32 srcSize = source.WrittenBytes; if (WrittenBytes + srcSize > BufferSize) Resize(BufferSize + srcSize); Array.Copy(source.Buffer, 0, Buffer, WrittenBytes, srcSize); WrittenBytes += srcSize; OnWritten(); }
public PacketRequest(StreamBuffer source) : base(source) { }
/// <summary> /// 패킷 하나가 완전히 수신되면 이 함수가 호출됩니다. /// 전달된 buffer 객체는 현재 Thread에서만 유효합니다. /// </summary> /// <param name="buffer">수신된 패킷이 담긴 StreamBuffer</param> protected virtual void OnReceive(StreamBuffer buffer) { }
private void OnReceived(Session session, StreamBuffer buffer) { SecurePacketRequest reqPacket = new SecurePacketRequest(buffer); reqPacket.Decrypt(_aesIV, _aesKey); reqPacket.SkipHeader(); Statistics.ReceivedBytes.Add(reqPacket.Size); Statistics.ReceivedCount.Add(1); try { if (reqPacket.PacketId == Protocol.GetID("CS_Login_Req")) OnCS_Login_Req(reqPacket); else { _user = UserManager.Find(reqPacket.UserNo); if (_user == null) { ForceClose("Invalid UserNo."); return; } if (reqPacket.SeqNo != _user.SeqNo + 1) { ForceClose("Invalid Sequence Number."); return; } _user.SeqNo = reqPacket.SeqNo; _user.Session = this; reqPacket.Dispatch(_user, "On" + Protocol.GetName(reqPacket.PacketId)); UserManager.HeartPulse(_user); } } catch (AegisException e) when (e.ResultCodeNo == AegisResult.BufferUnderflow) { Logger.Write(LogType.Err, 2, "Cannot read more data at PacketId(=0x{0:X}).", reqPacket.PacketId); } catch (Exception e) { Logger.Write(LogType.Err, 2, e.ToString()); } }
/// <summary> /// 패킷을 전송합니다. 패킷이 전송되면 OnSend함수가 호출됩니다. /// </summary> /// <param name="source">전송할 데이터가 담긴 StreamBuffer</param> public virtual void SendPacket(StreamBuffer source) { try { lock (this) { if (Socket != null) Socket.BeginSend(source.Buffer, 0, source.WrittenBytes, SocketFlags.None, OnSocket_Send, null); } } catch (SocketException) { } catch (Exception e) { Logger.Write(LogType.Err, 1, e.ToString()); } }
private void OnReceived(Session session, StreamBuffer buffer) { SecurePacketRequest reqPacket = new SecurePacketRequest(buffer); reqPacket.Decrypt(ServerMain.AES_IV, ServerMain.AES_Key); reqPacket.SkipHeader(); try { reqPacket.Dispatch(this, "On" + Protocol.GetName(reqPacket.PacketId)); } catch (AegisException e) when (e.ResultCodeNo == AegisResult.BufferUnderflow) { Logger.Write(LogType.Err, 2, "Cannot read more data at PacketId(=0x{0:X}).", reqPacket.PacketId); } catch (Exception e) { Logger.Write(LogType.Err, 2, e.ToString()); } }
public override void SendPacket(StreamBuffer buffer, Action<StreamBuffer> onSent = null) { SecurePacket packet = (SecurePacket)buffer; packet.Encrypt(ServerMain.AES_IV, ServerMain.AES_Key); base.SendPacket(buffer, onSent); }