private void OnMessageMyNick(ref MyNickMessage arg) { var ea = new TransferAuthorizationEventArgs { UserNickname = arg.Nickname }; OnAuthorization(ea); if (!ea.Allowed || string.IsNullOrEmpty(ea.OwnNickname)) { Logger.Info(RemoteAddress + " connection is not allowed"); Dispose(); return; } Source = new Source { UserNickname = arg.Nickname, HubAddress = ea.HubAddress }; if (FirstMessages == null) { using (var transaction = new SendTransaction(this)) { transaction.Send(new MyNickMessage { Nickname = ea.OwnNickname }.Raw); transaction.Send(new LockMessage().Raw); } } }
protected override void ParseRaw(byte[] buffer, int offset, int length) { if (_disposed) { return; } if (_tail != null) { var newBuffer = new byte[_tail.Length + length]; Buffer.BlockCopy(_tail, 0, newBuffer, 0, _tail.Length); Buffer.BlockCopy(buffer, offset, newBuffer, _tail.Length, length); length = length + _tail.Length; buffer = newBuffer; offset = 0; _tail = null; } if (_binaryMode) { ParseBinary(buffer, offset, length); return; } int cmdEndIndex = offset; while (true) { var prevPos = cmdEndIndex == offset ? offset : cmdEndIndex + 1; cmdEndIndex = Array.IndexOf(buffer, (byte)'|', prevPos, length - (cmdEndIndex - offset)); if (cmdEndIndex == -1) { if (prevPos < length) { _tail = new byte[length - prevPos]; Buffer.BlockCopy(buffer, prevPos, _tail, 0, _tail.Length); } break; } var command = _encoding.GetString(buffer, prevPos, cmdEndIndex - prevPos); if (IncomingMessage != null) { OnIncomingMessage(new MessageEventArgs { Message = command }); } if (command.Length > 0 && command[0] == '$') { // command var spaceIndex = command.IndexOf(' '); var cmdName = spaceIndex == -1 ? command : command.Substring(0, spaceIndex); switch (cmdName) { case "$MyNick": { var arg = MyNickMessage.Parse(command); OnMessageMyNick(ref arg); } break; case "$Supports": { var arg = SupportsMessage.Parse(command); OnMessageSupports(ref arg); } break; case "$Lock": { var arg = LockMessage.Parse(command); OnMessageLock(ref arg); } break; case "$Direction": { var arg = DirectionMessage.Parse(command); OnMessageDirection(ref arg); } break; case "$Error": { var arg = ErrorMessage.Parse(command); OnMessageError(ref arg); } break; case "$Key": { var arg = KeyMessage.Parse(command); OnMessageKey(ref arg); } break; case "$ADCSND": { var arg = ADCSNDMessage.Parse(command); if (OnMessageAdcsnd(ref arg)) { prevPos = cmdEndIndex + 1; if (prevPos < length + offset) { _tail = new byte[length - (prevPos - offset)]; Buffer.BlockCopy(buffer, prevPos, _tail, 0, _tail.Length); } _binaryMode = true; return; } Dispose(); } break; case "$ADCGET": { var arg = ADCGETMessage.Parse(command); OnMessageAdcget(arg); } break; } } } }