asyncio.inc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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
  98. begin
  99. FD_Clr(AHandle, IOdata.ReadMap);
  100. if AHandle = HighestHandle then
  101. CalcHighestHandle(AHandle);
  102. end;
  103. end;
  104. function TAsyncIOManager.GetReadHandler(AHandle: Integer): TAsyncIONotify;
  105. begin
  106. ASSERT((AHandle >= 0) and (AHandle <= MaxHandle));
  107. if (AHandle < 0) or (AHandle > MaxHandle) then
  108. Result := nil
  109. else
  110. Result := ReadNotifies[AHandle].Method;
  111. end;
  112. procedure TAsyncIOManager.SetWriteHandler(AHandle: Integer;
  113. AMethod: TAsyncIONotify; AUserData: TObject);
  114. begin
  115. ASSERT((AHandle >= 0) and (AHandle <= MaxHandle) and Assigned(AMethod));
  116. if (AHandle < 0) or (AHandle > MaxHandle) then
  117. exit;
  118. if AHandle > HighestHandle then
  119. HighestHandle := AHandle;
  120. FD_Set(AHandle, IOData.WriteMap);
  121. WriteNotifies[AHandle].Method := AMethod;
  122. WriteNotifies[AHandle].UserData := AUserData;
  123. end;
  124. procedure TAsyncIOManager.ClearWriteHandler(AHandle: Integer);
  125. begin
  126. ASSERT((AHandle >= 0) and (AHandle <= MaxHandle));
  127. if (AHandle >= 0) and (AHandle <= MaxHandle) then begin
  128. FD_Clr(AHandle, IOdata.WriteMap);
  129. if AHandle = HighestHandle then
  130. CalcHighestHandle(AHandle);
  131. end;
  132. end;
  133. function TAsyncIOManager.GetWriteHandler(AHandle: Integer): TAsyncIONotify;
  134. begin
  135. ASSERT((AHandle >= 0) and (AHandle <= MaxHandle));
  136. if (AHandle < 0) or (AHandle > MaxHandle) then
  137. Result := nil
  138. else
  139. Result := WriteNotifies[AHandle].Method;
  140. end;
  141. {
  142. $Log$
  143. Revision 1.3 2002-09-07 15:15:28 peter
  144. * old logs removed and tabs fixed
  145. }