void ProcessClient(Bluetooth.BluetoothClient client) { var stream = client.GetStream(); while (true) { var buffer = new byte[Message.HeaderSize]; if (stream.Read(buffer, 0, Message.HeaderSize) > 0 && isRunning) { var message = new MessageInfo(buffer); switch (message.Type) { case MessageType.MoveScreen: case MessageType.MouseButton: case MessageType.MouseWheel: case MessageType.MouseMove: case MessageType.KeyPress: messages.Enqueue(message); break; case MessageType.Clipboard: messages.Enqueue(MessagePacket.Parse(message, stream)); break; case MessageType.CheckIn: CheckInMessage checkIn = new CheckInMessage(MessagePacket.Parse(message, stream)); ScreenConfig(stream); messages.Enqueue(checkIn); break; } messageHandle.Set(); } } }
void ProcessClient(ISocketClient client) { var stream = client.GetStream(); while (isRunning) { var buffer = new byte[Message.HeaderSize]; if (stream.Read(buffer, 0, Message.HeaderSize) > 0 && isRunning) { switch ((MessageType)(buffer[0] & Message.TypeMask)) { case MessageType.MoveScreen: break; case MessageType.MouseWheel: OnMouseWheelFromServer(buffer); break; case MessageType.MouseButton: OnMouseButtonFromServer(buffer); break; case MessageType.MouseMove: OnMouseMoveFromServer(buffer); break; case MessageType.KeyPress: OnKeyPressFromServer(buffer); break; case MessageType.Clipboard: OnClipboardFromServer(new ClipboardMessage(MessagePacket.Parse(new MessageInfo(buffer), stream))); break; case MessageType.CheckIn: CheckInMessage checkIn = new CheckInMessage(MessagePacket.Parse(new MessageInfo(buffer), stream)); // Config OnScreenConfig(checkIn.Screens); ScreenConfig(stream); break; case MessageType.CheckOut: RemoveScreen(CheckOutMessage.Parse(new MessageInfo(buffer), stream)); client.Dispose(); return; } } } }
public async Task <CheckInMessage> WaitForCheckIn() { if (stream != null) { var bytes = new byte[Message.HeaderSize]; if (await stream.ReadAsync(bytes, 0, Message.HeaderSize) > 0) { var message = new MessageInfo(bytes); if (message.Type == MessageType.CheckIn) { return(new CheckInMessage(MessagePacket.Parse(message, stream))); } } } return(default);
private void Receive_Completed(object sender, SocketAsyncEventArgs e) { Socket clientSocket = (Socket)sender; try { var key = clientSocket.RemoteEndPoint.ToString(); if (clientSocket.Connected && e.BytesTransferred > 0) //클라이언트 소켓이 연결되어있고, 전송 바이트 확인 { var szData = e.Buffer.Take(e.BytesTransferred).ToArray(); //수신 데이터 string text = Encoding.UTF8.GetString(szData); var packet = MessagePacket.Parse(text); //json 파싱 _ClientList[key].NickName = packet.NickName; _OnMessageReceived?.Invoke(this, packet.NickName + " : " + packet.Message); MessageSend(packet.NickName.ToString() + " : " + packet.Message.ToString()); clientSocket.ReceiveAsync(e); } else { if (_ClientList.ContainsKey(key)) { var nickName = _ClientList[key].NickName; _ClientList.Remove(key); MessageSend(nickName + " : " + "의 연결이 끊어졌습니다."); _OnMessageReceived?.Invoke(this, nickName + " : " + "의 연결이 끊어졌습니다."); OnDisconnected?.Invoke(this, EventArgs.Empty); } } } catch (Exception ex) { WriteLog.WriteLogger(ex.ToString()); } }