asyncio.inc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. {
  2. $Id$
  3. This file is part of the Free Component Library (FCL)
  4. Copyright (c) 1999-2000 by the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. procedure TAsyncIOManager.CalcHighestHandle(max: Integer);
  12. var
  13. i: Integer;
  14. begin
  15. With IOData do
  16. begin
  17. HighestHandle := -1;
  18. for i := max downto 0 do
  19. if FD_IsSet(i, ReadMap) or FD_IsSet(i, WriteMap) then begin
  20. HighestHandle := i;
  21. break;
  22. end;
  23. end;
  24. end;
  25. function TAsyncIOManager.GetHandleAsync(AHandle: Integer): Boolean;
  26. begin
  27. Result := (fcntl(AHandle, F_GetFl) and Open_NonBlock) <> 0;
  28. end;
  29. procedure TAsyncIOManager.SetHandleAsync(AHandle: Integer; AValue: Boolean);
  30. var
  31. SavedBits: Integer;
  32. begin
  33. SavedBits := fcntl(AHandle, F_GetFl) and not Open_NonBlock;
  34. if AValue then
  35. fcntl(AHandle, F_SetFl, SavedBits or Open_NonBlock)
  36. else
  37. fcntl(AHandle, F_SetFl, SavedBits);
  38. end;
  39. constructor TAsyncIOManager.Create;
  40. begin
  41. inherited Create;
  42. With IOdata do
  43. begin
  44. FD_Zero(ReadMap);
  45. FD_Zero(WriteMap);
  46. end;
  47. HighestHandle := -1;
  48. end;
  49. procedure TAsyncIOManager.Run;
  50. var
  51. ThisReadMap, ThisWriteMap: TFDSet;
  52. i, res: Integer;
  53. begin
  54. DoBreak := False;
  55. With IOdata do
  56. begin
  57. while (not DoBreak) and ((HighestHandle >= 0) or (FTimeout > 0)) do begin
  58. ThisReadMap := ReadMap;
  59. ThisWriteMap := WriteMap;
  60. if FTimeout > 0 then
  61. res := Select(HighestHandle + 1, @ThisReadMap, @ThisWriteMap, nil, FTimeout)
  62. else
  63. res := Select(HighestHandle + 1, @ThisReadMap, @ThisWriteMap, nil, nil);
  64. if res < 0 then
  65. break;
  66. if res = 0 then
  67. ExecuteNotify(TimeoutNotify)
  68. else
  69. for i := 0 to HighestHandle do begin
  70. if FD_IsSet(i, ThisReadMap) and FD_IsSet(i, ReadMap)then
  71. ExecuteNotify(ReadNotifies[i]);
  72. if FD_IsSet(i, ThisWriteMap) and FD_IsSet(i, WriteMap) then
  73. ExecuteNotify(WriteNotifies[i]);
  74. end;
  75. end;
  76. end;
  77. end;
  78. procedure TAsyncIOManager.BreakRun;
  79. begin
  80. DoBreak := True;
  81. end;
  82. procedure TAsyncIOManager.SetReadHandler(AHandle: Integer;
  83. AMethod: TAsyncIONotify; AUserData: TObject);
  84. begin
  85. ASSERT((AHandle >= 0) and (AHandle <= MaxHandle) and Assigned(AMethod));
  86. if (AHandle < 0) or (AHandle > MaxHandle) then
  87. exit;
  88. if AHandle > HighestHandle then
  89. HighestHandle := AHandle;
  90. FD_Set(AHandle, IOdata.ReadMap);
  91. ReadNotifies[AHandle].Method := AMethod;
  92. ReadNotifies[AHandle].UserData := AUserData;
  93. end;
  94. procedure TAsyncIOManager.ClearReadHandler(AHandle: Integer);
  95. begin
  96. ASSERT((AHandle >= 0) and (AHandle <= MaxHandle));
  97. if (AHandle >= 0) and (AHandle <= MaxHandle) then begin
  98. FD_Clr(AHandle, IOdata.ReadMap);
  99. if AHandle = HighestHandle then
  100. CalcHighestHandle(AHandle);
  101. end;
  102. end;
  103. procedure TAsyncIOManager.SetWriteHandler(AHandle: Integer;
  104. AMethod: TAsyncIONotify; AUserData: TObject);
  105. begin
  106. ASSERT((AHandle >= 0) and (AHandle <= MaxHandle) and Assigned(AMethod));
  107. if (AHandle < 0) or (AHandle > MaxHandle) then
  108. exit;
  109. if AHandle > HighestHandle then
  110. HighestHandle := AHandle;
  111. FD_Set(AHandle, IOData.WriteMap);
  112. WriteNotifies[AHandle].Method := AMethod;
  113. WriteNotifies[AHandle].UserData := AUserData;
  114. end;
  115. procedure TAsyncIOManager.ClearWriteHandler(AHandle: Integer);
  116. begin
  117. ASSERT((AHandle >= 0) and (AHandle <= MaxHandle));
  118. if (AHandle >= 0) and (AHandle <= MaxHandle) then begin
  119. FD_Clr(AHandle, IOdata.WriteMap);
  120. if AHandle = HighestHandle then
  121. CalcHighestHandle(AHandle);
  122. end;
  123. end;
  124. {
  125. $Log$
  126. Revision 1.1 2000-02-18 23:14:10 michael
  127. + Initial implementation
  128. }